diff --git a/src/DataStore/src/DataStore/Aspect/AspectAbstract.php b/src/DataStore/src/DataStore/Aspect/AspectAbstract.php index 2ae39995..97b84b68 100644 --- a/src/DataStore/src/DataStore/Aspect/AspectAbstract.php +++ b/src/DataStore/src/DataStore/Aspect/AspectAbstract.php @@ -7,7 +7,6 @@ namespace rollun\datastore\DataStore\Aspect; use rollun\datastore\DataStore\Interfaces\DataStoreInterface; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; use Xiag\Rql\Parser\Query; /** @@ -22,17 +21,17 @@ * @see AspectAbstractFactory * @package rollun\datastore\DataStore\Aspect */ -class AspectAbstract implements DataStoresInterface, DataStoreInterface +class AspectAbstract implements DataStoreInterface { - /** @var DataStoresInterface $dataStore */ + /** @var DataStoreInterface $dataStore */ protected $dataStore; /** * AspectDataStoreAbstract constructor. * - * @param DataStoresInterface $dataStore + * @param DataStoreInterface $dataStore */ - public function __construct(DataStoresInterface $dataStore) + public function __construct(DataStoreInterface $dataStore) { $this->dataStore = $dataStore; } @@ -77,10 +76,9 @@ protected function postGetIterator(\Traversable $iterator) * By default does nothing * * @param $itemData - * @param bool|false $rewriteIfExist * @return array */ - protected function preCreate($itemData, $rewriteIfExist = false) + protected function preCreate($itemData) { return $itemData; } @@ -90,12 +88,12 @@ protected function preCreate($itemData, $rewriteIfExist = false) * * {@inheritdoc} */ - public function create($itemData, $rewriteIfExist = false) + public function create($itemData) { - $newData = $this->preCreate($itemData, $rewriteIfExist); - $result = $this->dataStore->create($newData, $rewriteIfExist); + $newData = $this->preCreate($itemData); + $result = $this->dataStore->create($newData); - return $this->postCreate($result, $newData, $rewriteIfExist); + return $this->postCreate($result, $newData); } /** @@ -105,10 +103,9 @@ public function create($itemData, $rewriteIfExist = false) * * @param $result * @param $itemData - * @param $rewriteIfExist * @return mixed */ - protected function postCreate($result, $itemData, $rewriteIfExist) + protected function postCreate($result, $itemData) { return $result; } @@ -119,10 +116,9 @@ protected function postCreate($result, $itemData, $rewriteIfExist) * By default does nothing * * @param $itemData - * @param bool|false $createIfAbsent * @return array */ - protected function preUpdate($itemData, $createIfAbsent = false) + protected function preUpdate($itemData) { return $itemData; } @@ -132,12 +128,12 @@ protected function preUpdate($itemData, $createIfAbsent = false) * * {@inheritdoc} */ - public function update($itemData, $createIfAbsent = false) + public function update($itemData) { - $newData = $this->preUpdate($itemData, $createIfAbsent); - $result = $this->dataStore->update($newData, $createIfAbsent); + $newData = $this->preUpdate($itemData); + $result = $this->dataStore->update($newData); - return $this->postUpdate($result, $newData, $createIfAbsent); + return $this->postUpdate($result, $newData); } /** @@ -147,10 +143,9 @@ public function update($itemData, $createIfAbsent = false) * * @param mixed $result * @param $itemData - * @param $createIfAbsent * @return mixed */ - protected function postUpdate($result, $itemData, $createIfAbsent) + protected function postUpdate($result, $itemData) { return $result; } @@ -193,41 +188,6 @@ protected function postDelete($result, $id) return $result; } - /** - * The pre-aspect for "deleteAll". - * - * By default does nothing - */ - protected function preDeleteAll() - { - } - - /** - * {@inheritdoc} - * - * {@inheritdoc} - */ - public function deleteAll() - { - $this->preDeleteAll(); - $result = $this->dataStore->deleteAll(); - - return $this->postDeleteAll($result); - } - - /** - * The post-aspect for "deleteAll" - * - * By default does nothing - * - * @param mixed $result - * @return mixed - */ - protected function postDeleteAll($result) - { - return $result; - } - /** * The pre-aspect for "getIdentifier". * diff --git a/src/DataStore/src/DataStore/CsvBase.php b/src/DataStore/src/DataStore/CsvBase.php index 0e8db41f..3719c49d 100644 --- a/src/DataStore/src/DataStore/CsvBase.php +++ b/src/DataStore/src/DataStore/CsvBase.php @@ -119,12 +119,8 @@ public function getIterator() /** * {@inheritdoc} */ - public function create($itemData, $rewriteIfExist = false) + public function create($itemData) { - if ($rewriteIfExist) { - trigger_error("Option 'rewriteIfExist' is no more use", E_USER_DEPRECATED); - } - $identifier = $this->getIdentifier(); switch (true) { @@ -133,7 +129,7 @@ public function create($itemData, $rewriteIfExist = false) $item = $this->createNewItem($itemData); $item[$identifier] = $this->generatePrimaryKey(); break; - case (!$rewriteIfExist && !is_null($this->read($itemData[$identifier]))): + case (!is_null($this->read($itemData[$identifier]))): throw new DataStoreException("Item is already exist with id = $itemData[$identifier]"); break; default: @@ -152,12 +148,8 @@ public function create($itemData, $rewriteIfExist = false) /** * {@inheritdoc} */ - public function update($itemData, $createIfAbsent = false) + public function update($itemData) { - if ($createIfAbsent) { - trigger_error("Option 'createIfAbsent' is no more use.", E_USER_DEPRECATED); - } - $identifier = $this->getIdentifier(); if (!isset($itemData[$identifier])) { @@ -168,13 +160,8 @@ public function update($itemData, $createIfAbsent = false) $this->checkIdentifierType($id); $item = $this->read($id); - switch (true) { - case (is_null($item) && !$createIfAbsent): - throw new DataStoreException("Can't update item with id = $id: item does not exist."); - case (is_null($item) && $createIfAbsent): - // new item - $item = $this->createNewItem($itemData); - break; + if (is_null($item)) { + throw new DataStoreException("Can't update item with id = $id: item does not exist."); } foreach ($item as $key => $value) { @@ -207,30 +194,6 @@ public function delete($id) return null; } - /** - * {@inheritdoc} - * - * {@inheritdoc} - */ - public function deleteAll() - { - // Count rows - $count = $this->count(); - $tmpFile = tempnam("/tmp", uniqid() . '.tmp'); - $tempHandler = fopen($tmpFile, 'w'); - - // Write the headings only and right away closes file - fputcsv($tempHandler, $this->columns, $this->csvDelimiter); - fclose($tempHandler); - - // Changes the original file to a temporary one. - if (!rename($tmpFile, $this->filename)) { - throw new DataStoreException("Failed to write the results to a file."); - } - - return $count; - } - /** * Flushes all changes to temporary file which then will change the original one * diff --git a/src/DataStore/src/DataStore/DataStoreAbstract.php b/src/DataStore/src/DataStore/DataStoreAbstract.php index 352cd135..22a8a286 100644 --- a/src/DataStore/src/DataStore/DataStoreAbstract.php +++ b/src/DataStore/src/DataStore/DataStoreAbstract.php @@ -22,7 +22,7 @@ * Class DataStoreAbstract * @package rollun\datastore\DataStore */ -abstract class DataStoreAbstract implements DataStoresInterface, DataStoreInterface +abstract class DataStoreAbstract implements DataStoreInterface { /** * @var ConditionBuilderAbstract @@ -377,7 +377,7 @@ protected function querySelect($data, Query $query) /** * {@inheritdoc} */ - abstract public function create($itemData, $rewriteIfExist = false); + abstract public function create($itemData); /** * {@inheritdoc} @@ -405,7 +405,7 @@ public function multiCreate($records) /** * {@inheritdoc} */ - abstract public function update($itemData, $createIfAbsent = false); + abstract public function update($itemData); /** * {@inheritdoc} diff --git a/src/DataStore/src/DataStore/DbTable.php b/src/DataStore/src/DataStore/DbTable.php index 3a7f59a0..c69f6492 100644 --- a/src/DataStore/src/DataStore/DbTable.php +++ b/src/DataStore/src/DataStore/DbTable.php @@ -88,18 +88,13 @@ protected function getSqlQueryBuilder() /** * {@inheritdoc} */ - public function create($itemData, $rewriteIfExist = false) + public function create($itemData) { - if ($rewriteIfExist) { - // 'rewriteIfExist' do not work with multiply insert - trigger_error("Option 'rewriteIfExist' is no more use", E_USER_DEPRECATED); - } - $adapter = $this->dbTable->getAdapter(); $adapter->getDriver()->getConnection()->beginTransaction(); try { - $insertedItem = $this->insertItem($itemData, $rewriteIfExist); + $insertedItem = $this->insertItem($itemData); $adapter->getDriver()->getConnection()->commit(); } catch (\Throwable $e) { $adapter->getDriver()->getConnection()->rollback(); @@ -119,15 +114,10 @@ public function create($itemData, $rewriteIfExist = false) /** * @param $itemData - * @param bool $rewriteIfExist * @return array|mixed|null */ - protected function insertItem($itemData, $rewriteIfExist = false) + protected function insertItem($itemData) { - if ($rewriteIfExist && isset($itemData[$this->getIdentifier()])) { - $this->delete($itemData[$this->getIdentifier()]); - } - $start = microtime(true); $response = $this->dbTable->insert($itemData); $end = microtime(true); @@ -156,12 +146,8 @@ protected function insertItem($itemData, $rewriteIfExist = false) /** * {@inheritdoc} */ - public function update($itemData, $createIfAbsent = false) + public function update($itemData) { - if ($createIfAbsent) { - trigger_error("Option 'createIfAbsent' is no more use.", E_USER_DEPRECATED); - } - if (!isset($itemData[$this->getIdentifier()])) { throw new DataStoreException('Item must has primary key'); } @@ -170,7 +156,7 @@ public function update($itemData, $createIfAbsent = false) $adapter->getDriver()->getConnection()->beginTransaction(); try { - $result = $this->updateItem($itemData, $createIfAbsent); + $result = $this->updateItem($itemData); $adapter->getDriver()->getConnection()->commit(); } catch (\Throwable $e) { $adapter->getDriver()->getConnection()->rollback(); @@ -357,11 +343,10 @@ private function selectForUpdateWithQuery(Query $query) /** * @param $itemData - * @param bool $createIfAbsent * @return array|mixed|null - * @throws DataStoreException + * @throws DataStoreException|\Throwable */ - protected function updateItem($itemData, $createIfAbsent = false) + protected function updateItem($itemData) { $identifier = $this->getIdentifier(); $id = $itemData[$identifier]; @@ -373,13 +358,9 @@ protected function updateItem($itemData, $createIfAbsent = false) $start = microtime(true); - if (!$isExist && $createIfAbsent) { - $response = $this->dbTable->insert($itemData); - $loggedMethod = 'insert'; - } elseif ($isExist) { + if ($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"); } @@ -387,7 +368,7 @@ protected function updateItem($itemData, $createIfAbsent = false) $end = microtime(true); $logContext = [ - self::LOG_METHOD => $loggedMethod, + self::LOG_METHOD => 'update', self::LOG_TABLE => $this->dbTable->getTable(), self::LOG_REQUEST => $itemData, self::LOG_TIME => $this->getRequestTime($start, $end), @@ -527,15 +508,6 @@ public function read($id) return $response; } - /** - * {@inheritdoc} - */ - public function deleteAll() - { - $where = '1=1'; - return $this->dbTable->delete($where); - } - /** * {@inheritdoc} */ diff --git a/src/DataStore/src/DataStore/HttpClient.php b/src/DataStore/src/DataStore/HttpClient.php index 3506c3ce..ddc9f6d4 100644 --- a/src/DataStore/src/DataStore/HttpClient.php +++ b/src/DataStore/src/DataStore/HttpClient.php @@ -204,10 +204,9 @@ public function read($id) * * @param string $method ('GET', 'HEAD', 'POST', 'PUT', 'DELETE') * @param string $uri - * @param bool $ifMatch * @return Client */ - protected function initHttpClient(string $method, string $uri, $ifMatch = false) + protected function initHttpClient(string $method, string $uri) { $httpClient = clone $this->client; $httpClient->setUri($uri); @@ -218,10 +217,6 @@ protected function initHttpClient(string $method, string $uri, $ifMatch = false) $headers['X-Life-Cycle-Token'] = $this->lifeCycleToken->serialize(); $headers['LifeCycleToken'] = $this->lifeCycleToken->serialize(); - if ($ifMatch) { - $headers['If-Match'] = '*'; - } - $httpClient->setHeaders($headers); if (isset($this->login) && isset($this->password)) { @@ -282,13 +277,9 @@ public function query(Query $query) /** * {@inheritdoc} */ - public function create($itemData, $rewriteIfExist = false) + public function create($itemData) { - if ($rewriteIfExist) { - trigger_error("Option 'rewriteIfExist' is no more use", E_USER_DEPRECATED); - } - - $client = $this->initHttpClient(Request::METHOD_POST, $this->url, $rewriteIfExist); + $client = $this->initHttpClient(Request::METHOD_POST, $this->url); $json = Serializer::jsonSerialize($itemData); $client->setRawBody($json); $response = $client->send(); @@ -344,12 +335,8 @@ public function multiCreate($records) /** * {@inheritdoc} */ - public function update($itemData, $createIfAbsent = false) + public function update($itemData) { - if ($createIfAbsent) { - trigger_error("Option 'createIfAbsent' is no more use.", E_USER_DEPRECATED); - } - $identifier = $this->getIdentifier(); if (!isset($itemData[$identifier])) { @@ -361,7 +348,7 @@ public function update($itemData, $createIfAbsent = false) $uri = $this->createUri(null, $itemData[$identifier]); unset($itemData[$identifier]); - $client = $this->initHttpClient(Request::METHOD_PUT, $uri, $createIfAbsent); + $client = $this->initHttpClient(Request::METHOD_PUT, $uri); $client->setRawBody(Serializer::jsonSerialize($itemData)); $response = $client->send(); diff --git a/src/DataStore/src/DataStore/Interfaces/DateTimeInterface.php b/src/DataStore/src/DataStore/Interfaces/DateTimeInterface.php index ab5b5bcf..75544e19 100644 --- a/src/DataStore/src/DataStore/Interfaces/DateTimeInterface.php +++ b/src/DataStore/src/DataStore/Interfaces/DateTimeInterface.php @@ -10,7 +10,7 @@ * Interface DateTimeInterface * @package rollun\datastore\DataStore\Interfaces */ -interface DateTimeInterface extends DataStoresInterface +interface DateTimeInterface extends DataStoreInterface { public const FIELD_DATETIME = "datetime"; diff --git a/src/DataStore/src/DataStore/Memory.php b/src/DataStore/src/DataStore/Memory.php index d443d5ca..9a05d91b 100644 --- a/src/DataStore/src/DataStore/Memory.php +++ b/src/DataStore/src/DataStore/Memory.php @@ -76,18 +76,14 @@ public function read($id) /** * {@inheritdoc} */ - public function create($itemData, $rewriteIfExist = false) + public function create($itemData) { - if ($rewriteIfExist) { - trigger_error("Option 'rewriteIfExist' is no more use", E_USER_DEPRECATED); - } - $this->checkOnExistingColumns($itemData); $identifier = $this->getIdentifier(); $id = isset($itemData[$identifier]) ? $itemData[$identifier] : null; if ($id) { - if (isset($this->items[$id]) && !$rewriteIfExist) { + if (isset($this->items[$id])) { throw new DataStoreException("Item with id '{$itemData[$identifier]}' already exist"); } @@ -106,12 +102,8 @@ public function create($itemData, $rewriteIfExist = false) /** * {@inheritdoc} */ - public function update($itemData, $createIfAbsent = false) + public function update($itemData) { - if ($createIfAbsent) { - trigger_error("Option 'createIfAbsent' is no more use", E_USER_DEPRECATED); - } - $this->checkOnExistingColumns($itemData); $identifier = $this->getIdentifier(); @@ -129,11 +121,7 @@ public function update($itemData, $createIfAbsent = false) unset($itemData[$id]); } else { - if ($createIfAbsent) { - $this->items[$id] = $itemData; - } else { - throw new DataStoreException("Item doesn't exist with id = $id"); - } + throw new DataStoreException("Item doesn't exist with id = $id"); } return $this->items[$id]; @@ -156,17 +144,6 @@ public function delete($id) return null; } - /** - * {@inheritdoc} - */ - public function deleteAll() - { - $deletedItemsCount = count($this->items); - $this->items = []; - - return $deletedItemsCount; - } - /** * {@inheritdoc} */