Skip to content

Commit

Permalink
start remove old datastore interface
Browse files Browse the repository at this point in the history
  • Loading branch information
maria-rollun committed Oct 25, 2021
1 parent cae3fb8 commit 0fc33d3
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 184 deletions.
72 changes: 16 additions & 56 deletions src/DataStore/src/DataStore/Aspect/AspectAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}

/**
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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);
}

/**
Expand All @@ -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;
}
Expand Down Expand Up @@ -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".
*
Expand Down
47 changes: 5 additions & 42 deletions src/DataStore/src/DataStore/CsvBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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:
Expand All @@ -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])) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
*
Expand Down
6 changes: 3 additions & 3 deletions src/DataStore/src/DataStore/DataStoreAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* Class DataStoreAbstract
* @package rollun\datastore\DataStore
*/
abstract class DataStoreAbstract implements DataStoresInterface, DataStoreInterface
abstract class DataStoreAbstract implements DataStoreInterface
{
/**
* @var ConditionBuilderAbstract
Expand Down Expand Up @@ -377,7 +377,7 @@ protected function querySelect($data, Query $query)
/**
* {@inheritdoc}
*/
abstract public function create($itemData, $rewriteIfExist = false);
abstract public function create($itemData);

/**
* {@inheritdoc}
Expand Down Expand Up @@ -405,7 +405,7 @@ public function multiCreate($records)
/**
* {@inheritdoc}
*/
abstract public function update($itemData, $createIfAbsent = false);
abstract public function update($itemData);

/**
* {@inheritdoc}
Expand Down
46 changes: 9 additions & 37 deletions src/DataStore/src/DataStore/DbTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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');
}
Expand All @@ -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();
Expand Down Expand Up @@ -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];
Expand All @@ -373,21 +358,17 @@ 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");
}

$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),
Expand Down Expand Up @@ -527,15 +508,6 @@ public function read($id)
return $response;
}

/**
* {@inheritdoc}
*/
public function deleteAll()
{
$where = '1=1';
return $this->dbTable->delete($where);
}

/**
* {@inheritdoc}
*/
Expand Down
Loading

0 comments on commit 0fc33d3

Please sign in to comment.