Skip to content

Commit

Permalink
More refactoring + Updating doc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed Jan 12, 2016
1 parent 2dc251e commit 31fd01a
Showing 1 changed file with 62 additions and 40 deletions.
102 changes: 62 additions & 40 deletions src/SettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SettingsManager implements Contracts\Settings
/**
* The Setting model.
*
* @var Setting
* @var \Arcanesoft\Settings\Models\Setting
*/
private $model;

Expand Down Expand Up @@ -245,32 +245,17 @@ public function reset($domain = null)
*/
public function save()
{
$saved = $this->model->all();
$changes = $this->getChanges($saved);
$changes = $this->getChanges(
$saved = $this->model->all()
);

foreach ($changes['inserted'] as $domain => $values) {
foreach ($values as $key => $value) {
$this->model->createOne($domain, $key, $value);
}
}
$this->saveInserted($changes['inserted']);
$this->saveUpdated($saved, $changes['updated']);
$this->saveDeleted($saved, $changes['deleted']);

foreach ($changes['updated'] as $domain => $values) {
foreach ($values as $key => $value) {
/** @var Setting $model */
$model = $saved->groupBy('domain')->get($domain)->where('key', $key)->first();
$model->updateValue($value);
$model->save();
}
}

foreach ($changes['deleted'] as $domain => $values) {
foreach ($values as $key) {
$model = $saved->groupBy('domain')->get($domain)->where('key', $key)->first();
$model->delete();
}
if ($this->isCached()) {
$this->cache->forget($this->getCacheKey());
}

$this->flushCachedSettings();
}

/**
Expand All @@ -293,6 +278,55 @@ private function getChanges($saved)
);
}

/**
* Save the inserted entries.
*
* @param array $inserted
*/
private function saveInserted(array $inserted)
{
foreach ($inserted as $domain => $values) {
foreach ($values as $key => $value) {
$this->model->createOne($domain, $key, $value);
}
}
}

/**
* Save the updated entries.
*
* @param \Illuminate\Database\Eloquent\Collection $saved
* @param array $updated
*/
private function saveUpdated($saved, array $updated)
{
foreach ($updated as $domain => $values) {
foreach ($values as $key => $value) {
/** @var \Arcanesoft\Settings\Models\Setting $model */
$model = $saved->groupBy('domain')->get($domain)->where('key', $key)->first();
$model->updateValue($value);
$model->save();
}
}
}

/**
* Save the deleted entries.
*
* @param \Illuminate\Database\Eloquent\Collection $saved
* @param array $deleted
*/
private function saveDeleted($saved, array $deleted)
{
foreach ($deleted as $domain => $values) {
foreach ($values as $key) {
/** @var \Arcanesoft\Settings\Models\Setting $model */
$model = $saved->groupBy('domain')->get($domain)->where('key', $key)->first();
$model->delete();
}
}
}

/**
* Grab the settings domain name from the key.
*
Expand Down Expand Up @@ -335,7 +369,7 @@ private function checkLoaded()
private function loadData()
{
foreach ($this->getCachedSettings() as $setting) {
/** @var Setting $setting */
/** @var \Arcanesoft\Settings\Models\Setting $setting */
$data = $this->data->get($setting->domain, []);

Arr::set($data, $setting->key, $setting->casted_value);
Expand All @@ -351,22 +385,10 @@ private function loadData()
*/
private function getCachedSettings()
{
if ($this->isCached()) {
return $this->cache->rememberForever($this->getCacheKey(), function() {
return ! $this->isCached()
? $this->model->all()
: $this->cache->rememberForever($this->getCacheKey(), function() {
return $this->model->all();
});
}

return $this->model->all();
}

/**
* Flush the cached settings.
*/
private function flushCachedSettings()
{
if ($this->isCached()) {
$this->cache->forget($this->getCacheKey());
}
}
}

0 comments on commit 31fd01a

Please sign in to comment.