Skip to content

Commit

Permalink
remove $logContext parameter from insertItem and updateItem
Browse files Browse the repository at this point in the history
  • Loading branch information
maria-rollun committed Sep 27, 2021
1 parent 66c74bc commit a971feb
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 53 deletions.
127 changes: 76 additions & 51 deletions src/DataStore/src/DataStore/DbTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,23 @@ public function create($itemData, $rewriteIfExist = false)
trigger_error("Option 'rewriteIfExist' is no more use", E_USER_DEPRECATED);
}

$logContext = [
self::LOG_METHOD => __FUNCTION__,
self::LOG_TABLE => $this->dbTable->getTable(),
self::LOG_REQUEST => $itemData,
];

$adapter = $this->dbTable->getAdapter();
$adapter->getDriver()->getConnection()->beginTransaction();

try {
$insertedItem = $this->insertItem($itemData, $rewriteIfExist, $logContext);
$insertedItem = $this->insertItem($itemData, $rewriteIfExist);
$adapter->getDriver()->getConnection()->commit();
} catch (\Throwable $e) {
$adapter->getDriver()->getConnection()->rollback();
$logContext[self::LOG_ROLLBACK] = true;
$logContext = [
self::LOG_METHOD => __METHOD__,
self::LOG_TABLE => $this->dbTable->getTable(),
self::LOG_REQUEST => $itemData,
self::LOG_ROLLBACK => true,
'exception' => $e,
];
$this->writeLogsIfNeeded($logContext, "Request to db table '{$this->dbTable->getTable()}' failed");
throw new DataStoreException("[{$this->dbTable->getTable()}]Can't insert item. {$e->getMessage()}", 0, $e);
} finally {
$this->writeLogsIfNeeded($logContext);
}

return $insertedItem;
Expand All @@ -121,10 +120,9 @@ public function create($itemData, $rewriteIfExist = false)
/**
* @param $itemData
* @param bool $rewriteIfExist
* @param array $logContext
* @return array|mixed|null
*/
protected function insertItem($itemData, $rewriteIfExist = false, array &$logContext = [])
protected function insertItem($itemData, $rewriteIfExist = false)
{
if ($rewriteIfExist && isset($itemData[$this->getIdentifier()])) {
$this->delete($itemData[$this->getIdentifier()]);
Expand All @@ -134,8 +132,15 @@ protected function insertItem($itemData, $rewriteIfExist = false, array &$logCon
$response = $this->dbTable->insert($itemData);
$end = microtime(true);

$logContext[self::LOG_TIME] = $this->getRequestTime($start, $end);
$logContext[self::LOG_RESPONSE] = $response;
$logContext = [
self::LOG_METHOD => 'insert',
self::LOG_TABLE => $this->dbTable->getTable(),
self::LOG_REQUEST => $itemData,
self::LOG_TIME => $this->getRequestTime($start, $end),
self::LOG_RESPONSE => $response,
];

$this->writeLogsIfNeeded($logContext);

if (isset($itemData[$this->getIdentifier()])) {
$insertedItem = $this->read($itemData[$this->getIdentifier()]);
Expand All @@ -161,24 +166,23 @@ public function update($itemData, $createIfAbsent = false)
throw new DataStoreException('Item must has primary key');
}

$logContext = [
self::LOG_METHOD => __FUNCTION__,
self::LOG_TABLE => $this->dbTable->getTable(),
self::LOG_REQUEST => $itemData,
];

$adapter = $this->dbTable->getAdapter();
$adapter->getDriver()->getConnection()->beginTransaction();

try {
$result = $this->updateItem($itemData, $createIfAbsent, $logContext);
$result = $this->updateItem($itemData, $createIfAbsent);
$adapter->getDriver()->getConnection()->commit();
} catch (\Throwable $e) {
$adapter->getDriver()->getConnection()->rollback();
$logContext[self::LOG_ROLLBACK] = true;
$logContext = [
self::LOG_METHOD => __METHOD__,
self::LOG_TABLE => $this->dbTable->getTable(),
self::LOG_REQUEST => $itemData,
self::LOG_ROLLBACK => true,
'exception' => $e,
];
$this->writeLogsIfNeeded($logContext, "Request to db table '{$this->dbTable->getTable()}' failed");
throw new DataStoreException("[{$this->dbTable->getTable()}]Can't update item. {$e->getMessage()}", 0, $e);
} finally {
$this->writeLogsIfNeeded($logContext);
}

return $result;
Expand Down Expand Up @@ -357,7 +361,7 @@ private function selectForUpdateWithQuery(Query $query)
* @return array|mixed|null
* @throws DataStoreException
*/
protected function updateItem($itemData, $createIfAbsent = false, &$logContext = [])
protected function updateItem($itemData, $createIfAbsent = false)
{
$identifier = $this->getIdentifier();
$id = $itemData[$identifier];
Expand All @@ -371,17 +375,26 @@ protected function updateItem($itemData, $createIfAbsent = false, &$logContext =

if (!$isExist && $createIfAbsent) {
$response = $this->dbTable->insert($itemData);
$loggedMethod = 'insert';
} elseif ($isExist) {
unset($itemData[$identifier]);
$response = $this->dbTable->update($itemData, [$identifier => $id]);
$loggedMethod = 'update';
} else {
throw new DataStoreException("[{$this->dbTable->getTable()}]Can't update item with id = $id");
}

$end = microtime(true);

$logContext[self::LOG_TIME] = $this->getRequestTime($start, $end);
$logContext[self::LOG_RESPONSE] = $response;
$logContext = [
self::LOG_METHOD => $loggedMethod,
self::LOG_TABLE => $this->dbTable->getTable(),
self::LOG_REQUEST => $itemData,
self::LOG_TIME => $this->getRequestTime($start, $end),
self::LOG_RESPONSE => $response,
];

$this->writeLogsIfNeeded($logContext);

return $this->read($id);
}
Expand All @@ -406,14 +419,16 @@ public function query(Query $query)
$resultSet = $statement->execute();
$end = microtime(true);
} catch (\PDOException $exception) {
$this->writeLogsIfNeeded($logContext);
$logContext['exception'] = $exception;
$this->writeLogsIfNeeded($logContext, "Request to db table '{$this->dbTable->getTable()}' failed");
throw new DataStoreException(
"Error by execute '$sqlString' query to {$this->getDbTable()->getTable()}.",
500,
$exception
);
} catch (\Exception $e) {
$this->writeLogsIfNeeded($logContext);
} catch (\Throwable $e) {
$logContext['exception'] = $e;
$this->writeLogsIfNeeded($logContext, "Request to db table '{$this->dbTable->getTable()}' failed");
throw $e;
}

Expand Down Expand Up @@ -457,15 +472,17 @@ public function delete($id)
$start = microtime(true);
$response = $this->dbTable->delete($request);
$end = microtime(true);

$logContext[self::LOG_TIME] = $this->getRequestTime($start, $end);
$logContext[self::LOG_RESPONSE] = $response;
} catch (\Exception $e) {
} catch (\Throwable $e) {
$logContext['exception'] = $e;
$this->writeLogsIfNeeded($logContext, "Request to db table '{$this->dbTable->getTable()}' failed");
throw $e;
} finally {
$this->writeLogsIfNeeded($logContext);
}

$logContext[self::LOG_TIME] = $this->getRequestTime($start, $end);
$logContext[self::LOG_RESPONSE] = $response;

$this->writeLogsIfNeeded($logContext);

return $element;
}

Expand All @@ -489,22 +506,24 @@ public function read($id)
$start = microtime(true);
$rowSet = $this->dbTable->select($request);
$end = microtime(true);
} catch (\Throwable $e) {
$logContext['exception'] = $e;
$this->writeLogsIfNeeded($logContext, "Request to db table '{$this->dbTable->getTable()}' failed");
throw $e;
}

$row = $rowSet->current();
$response = null;

if (isset($row)) {
$response = $row->getArrayCopy();
}
$row = $rowSet->current();
$response = null;

$logContext[self::LOG_TIME] = $this->getRequestTime($start, $end);
$logContext[self::LOG_RESPONSE] = $response;
} catch (\Exception $e) {
throw $e;
} finally {
$this->writeLogsIfNeeded($logContext);
if (isset($row)) {
$response = $row->getArrayCopy();
}

$logContext[self::LOG_TIME] = $this->getRequestTime($start, $end);
$logContext[self::LOG_RESPONSE] = $response;

$this->writeLogsIfNeeded($logContext);

return $response;
}

Expand Down Expand Up @@ -593,11 +612,17 @@ protected function getKeys()
return $keys;
}

protected function writeLogsIfNeeded(array $logContext = [])
protected function writeLogsIfNeeded(array $logContext = [], string $message = null)
{
if ($this->writeLogs) {
$this->logger->debug("Request to db table '{$this->dbTable->getTable()}'", $logContext);
if (!$this->writeLogs) {
return;
}

if (is_null($message)) {
$message = "Request to db table '{$this->dbTable->getTable()}'";
}

$this->logger->debug($message, $logContext);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/functional/DataStore/DataStore/DbTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ public function testWriteLog()
$loggerMock->expects($this->atLeast(7))
->method('debug')
->withConsecutive(
[$this->isType('string'), $this->contains('read')],
[$this->isType('string'), $this->contains('create')],
[$this->isType('string'), $this->contains('insert')],
[$this->isType('string'), $this->contains('read')],
[$this->isType('string'), $this->contains('update')],
[$this->isType('string'), $this->contains('read')],
[$this->isType('string'), $this->contains('query')],
[$this->isType('string'), $this->contains('read')],
[$this->isType('string'), $this->contains('read')],
Expand Down

0 comments on commit a971feb

Please sign in to comment.