Skip to content

Commit

Permalink
Refactoring the Settings Manager (Again)
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed Jan 12, 2016
1 parent 31fd01a commit 7575c04
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 116 deletions.
128 changes: 12 additions & 116 deletions src/SettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Arcanedev\Support\Collection;
use Arcanesoft\Settings\Helpers\Arr;
use Arcanesoft\Settings\Helpers\Comparator;
use Arcanesoft\Settings\Models\Setting;
use Illuminate\Contracts\Cache\Repository as Cache;

Expand Down Expand Up @@ -38,19 +39,8 @@ class SettingsManager implements Contracts\Settings
*/
protected $loaded = false;

/**
* The Setting model.
*
* @var \Arcanesoft\Settings\Models\Setting
*/
private $model;

/**
* The cache repository
*
* @var \Illuminate\Contracts\Cache\Repository
*/
private $cache;
/** @var \Arcanesoft\Settings\Stores\EloquentStore */
private $store;

/* ------------------------------------------------------------------------------------------------
| Constructor
Expand All @@ -64,8 +54,7 @@ class SettingsManager implements Contracts\Settings
*/
public function __construct(Setting $model, Cache $cache)
{
$this->model = $model;
$this->cache = $cache;
$this->store = new Stores\EloquentStore($model, $cache);
$this->data = new Collection;
}

Expand All @@ -83,26 +72,6 @@ protected function getDefaultDomain()
return $this->config('default-domain', 'default');
}

/**
* Get the cache key.
*
* @return string
*/
protected function getCacheKey()
{
return $this->config('cache.key', 'cached_settings');
}

/**
* Check if cache is enabled.
*
* @return bool
*/
protected function isCached()
{
return $this->config('cache.enabled', false);
}

/**
* Get the config value by key.
*
Expand Down Expand Up @@ -246,16 +215,10 @@ public function reset($domain = null)
public function save()
{
$changes = $this->getChanges(
$saved = $this->model->all()
$saved = $this->store->all()
);

$this->saveInserted($changes['inserted']);
$this->saveUpdated($saved, $changes['updated']);
$this->saveDeleted($saved, $changes['deleted']);

if ($this->isCached()) {
$this->cache->forget($this->getCacheKey());
}
$this->store->save($saved, $changes);
}

/**
Expand All @@ -267,7 +230,7 @@ public function save()
*/
private function getChanges($saved)
{
return Helpers\Comparator::compare(
return Comparator::compare(
$this->data->map(function (array $settings) {
return Arr::dot($settings);
})->toArray(),
Expand All @@ -278,55 +241,6 @@ 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 @@ -354,41 +268,23 @@ private function grabDomain(&$key)
*/
private function checkLoaded()
{
if ($this->loaded) {
return;
if ( ! $this->loaded) {
$this->data->reset();
$this->loadData();
$this->loaded = true;
}

$this->data->reset();
$this->loadData();
$this->loaded = true;
}

/**
* Load the data.
*/
private function loadData()
{
foreach ($this->getCachedSettings() as $setting) {
foreach ($this->store->all() as $setting) {
/** @var \Arcanesoft\Settings\Models\Setting $setting */
$data = $this->data->get($setting->domain, []);

Arr::set($data, $setting->key, $setting->casted_value);

$this->data->put($setting->domain, $data);
}
}

/**
* Get cached settings.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
private function getCachedSettings()
{
return ! $this->isCached()
? $this->model->all()
: $this->cache->rememberForever($this->getCacheKey(), function() {
return $this->model->all();
});
}
}
154 changes: 154 additions & 0 deletions src/Stores/EloquentStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php namespace Arcanesoft\Settings\Stores;

use Arcanesoft\Settings\Models\Setting;
use Illuminate\Contracts\Cache\Repository as Cache;

/**
* Class EloquentStore
*
* @package Arcanesoft\Settings\Stores
* @author ARCANEDEV <[email protected]>
*/
class EloquentStore
{
/* ------------------------------------------------------------------------------------------------
| Properties
| ------------------------------------------------------------------------------------------------
*/
/**
* The Setting Eloquent Model.
*
* @var \Arcanesoft\Settings\Models\Setting
*/
protected $model;

/* ------------------------------------------------------------------------------------------------
| Constructor
| ------------------------------------------------------------------------------------------------
*/
/**
* SettingsManager constructor.
*
* @param \Arcanesoft\Settings\Models\Setting $model
* @param \Illuminate\Contracts\Cache\Repository $cache
*/
public function __construct(Setting $model, Cache $cache)
{
$this->model = $model;
$this->cache = $cache;
}

/* ------------------------------------------------------------------------------------------------
| Getters & Setters
| ------------------------------------------------------------------------------------------------
*/
/**
* Get the cache key.
*
* @return string
*/
protected function getCacheKey()
{
return $this->config('cache.key', 'cached_settings');
}

/**
* Check if cache is enabled.
*
* @return bool
*/
protected function isCached()
{
return $this->config('cache.enabled', false);
}

/**
* Get the config value by key.
*
* @param string $key
* @param mixed $default
*
* @return mixed
*/
private function config($key, $default = null)
{
return config("arcanesoft.settings.$key", $default);
}

/* ------------------------------------------------------------------------------------------------
| Main Functions
| ------------------------------------------------------------------------------------------------
*/
public function all()
{
return ! $this->isCached()
? $this->model->all()
: $this->cache->rememberForever($this->getCacheKey(), function() {
return $this->model->all();
});
}

public function save($saved, $changes)
{
$this->saveInserted($changes['inserted']);
$this->saveUpdated($saved, $changes['updated']);
$this->saveDeleted($saved, $changes['deleted']);

if ($this->isCached()) {
$this->cache->forget($this->getCacheKey());
}
}

/* ------------------------------------------------------------------------------------------------
| Other Functions
| ------------------------------------------------------------------------------------------------
*/
/**
* 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();
}
}
}
}

0 comments on commit 7575c04

Please sign in to comment.