Skip to content

Commit

Permalink
Refactoring the Database store
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed May 12, 2017
1 parent 18d0b03 commit 2330416
Showing 1 changed file with 71 additions and 31 deletions.
102 changes: 71 additions & 31 deletions src/Stores/DatabaseStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,38 +220,11 @@ protected function read()
*/
protected function write(array $data)
{
$changes = [
'inserted' => Arr::dot($data),
'updated' => [],
'deleted' => [],
];

foreach ($this->newQuery()->pluck($this->keyColumn) as $key) {
if (Arr::has($changes['inserted'], $key))
$changes['updated'][$key] = $changes['inserted'][$key];
else
$changes['deleted'][] = $key;

Arr::forget($changes['inserted'], $key);
}

foreach ($changes['updated'] as $key => $value) {
$this->newQuery()
->where($this->keyColumn, '=', $key)
->update(array($this->valueColumn => $value));
}

if ($changes['inserted']) {
$this->newQuery(true)->insert(
$this->prepareInsertData($changes['inserted'])
);
}
$changes = $this->getChanges($data);

if ($changes['deleted']) {
$this->newQuery()
->whereIn($this->keyColumn, $changes['deleted'])
->delete();
}
$this->syncUpdated($changes['updated']);
$this->syncInserted($changes['inserted']);
$this->syncDeleted($changes['deleted']);
}

/* -----------------------------------------------------------------
Expand Down Expand Up @@ -316,4 +289,71 @@ protected function hasQueryConstraint()
{
return ! is_null($this->queryConstraint) && is_callable($this->queryConstraint);
}

/**
* Get the changed settings data.
*
* @param array $data
*
* @return array
*/
private function getChanges(array $data)
{
$changes = [
'inserted' => Arr::dot($data),
'updated' => [],
'deleted' => [],
];

foreach ($this->newQuery()->pluck($this->keyColumn) as $key) {
if (Arr::has($changes['inserted'], $key))
$changes['updated'][$key] = $changes['inserted'][$key];
else
$changes['deleted'][] = $key;

Arr::forget($changes['inserted'], $key);
}

return $changes;
}

/**
* Sync the updated records.
*
* @param array $updated
*/
private function syncUpdated(array $updated)
{
foreach ($updated as $key => $value) {
$this->newQuery()
->where($this->keyColumn, '=', $key)
->update([$this->valueColumn => $value]);
}
}

/**
* Sync the inserted records.
*
* @param array $inserted
*/
private function syncInserted(array $inserted)
{
if ( ! empty($inserted)) {
$this->newQuery(true)->insert(
$this->prepareInsertData($inserted)
);
}
}

/**
* Sync the deleted records.
*
* @param array $deleted
*/
private function syncDeleted(array $deleted)
{
if ( ! empty($deleted)) {
$this->newQuery()->whereIn($this->keyColumn, $deleted)->delete();
}
}
}

0 comments on commit 2330416

Please sign in to comment.