2017-12-10 11:56:26 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2017-12-13 07:09:26 +08:00
|
|
|
use Log;
|
2017-12-13 02:43:58 +08:00
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
use Prettus\Repository\Contracts\CacheableInterface;
|
|
|
|
|
2017-12-10 11:56:26 +08:00
|
|
|
use App\Models\Setting;
|
2018-02-02 06:46:37 +08:00
|
|
|
use Prettus\Repository\Traits\CacheableRepository;
|
2018-01-20 01:46:30 +08:00
|
|
|
use App\Exceptions\SettingNotFound;
|
|
|
|
|
2017-12-13 07:09:26 +08:00
|
|
|
use Prettus\Validator\Exceptions\ValidatorException;
|
2017-12-10 11:56:26 +08:00
|
|
|
|
|
|
|
class SettingRepository extends BaseRepository implements CacheableInterface
|
|
|
|
{
|
|
|
|
use CacheableRepository;
|
|
|
|
|
2017-12-26 08:10:24 +08:00
|
|
|
public $cacheMinutes = 1;
|
|
|
|
|
2017-12-10 11:56:26 +08:00
|
|
|
public function model()
|
|
|
|
{
|
|
|
|
return Setting::class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-13 02:43:58 +08:00
|
|
|
* Get a setting, reading it from the cache possibly
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed
|
2018-01-20 01:46:30 +08:00
|
|
|
* @throws SettingNotFound
|
2017-12-10 11:56:26 +08:00
|
|
|
*/
|
2017-12-13 02:43:58 +08:00
|
|
|
public function retrieve($key)
|
2017-12-10 11:56:26 +08:00
|
|
|
{
|
2018-01-01 01:19:18 +08:00
|
|
|
$key = Setting::formatKey($key);
|
2018-01-01 01:09:56 +08:00
|
|
|
$setting = $this->findWhere(['id' => $key], ['type', 'value'])->first();
|
2017-12-13 02:43:58 +08:00
|
|
|
|
|
|
|
if(!$setting) {
|
2018-01-20 01:46:30 +08:00
|
|
|
throw new SettingNotFound($key . ' not found');
|
2017-12-13 02:43:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# cast some types
|
|
|
|
switch($setting->type) {
|
|
|
|
case 'bool':
|
|
|
|
case 'boolean':
|
2018-01-20 05:02:49 +08:00
|
|
|
$value = $setting->value;
|
|
|
|
if($value === 'true' || $value === '1') {
|
|
|
|
$value = true;
|
|
|
|
} elseif($value === 'false' || $value === '0') {
|
|
|
|
$value = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (bool) $value;
|
2017-12-13 02:43:58 +08:00
|
|
|
break;
|
|
|
|
case 'date':
|
|
|
|
return Carbon::parse($setting->value);
|
|
|
|
break;
|
|
|
|
case 'int':
|
|
|
|
case 'integer':
|
2018-01-01 01:09:56 +08:00
|
|
|
case 'number':
|
2017-12-13 02:43:58 +08:00
|
|
|
return (int) $setting->value;
|
|
|
|
break;
|
2018-01-01 01:09:56 +08:00
|
|
|
case 'float':
|
|
|
|
return (float) $setting->value;
|
|
|
|
break;
|
2017-12-13 02:43:58 +08:00
|
|
|
default:
|
|
|
|
return $setting->value;
|
|
|
|
}
|
2017-12-10 11:56:26 +08:00
|
|
|
}
|
|
|
|
|
2017-12-13 07:09:26 +08:00
|
|
|
/**
|
|
|
|
* Update an existing setting with a new value. Doesn't create
|
|
|
|
* a new setting
|
|
|
|
* @param $key
|
|
|
|
* @param $value
|
|
|
|
* @return null
|
|
|
|
*/
|
2017-12-13 02:43:58 +08:00
|
|
|
public function store($key, $value)
|
2017-12-10 11:56:26 +08:00
|
|
|
{
|
2018-01-01 01:19:18 +08:00
|
|
|
$key = Setting::formatKey($key);
|
2018-01-20 01:46:30 +08:00
|
|
|
$setting = $this->findWhere(
|
|
|
|
['id' => $key],
|
|
|
|
['id', 'value'] # only get these columns
|
|
|
|
)->first();
|
|
|
|
|
2017-12-13 02:43:58 +08:00
|
|
|
if (!$setting) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-12-10 11:56:26 +08:00
|
|
|
|
2017-12-13 07:09:26 +08:00
|
|
|
try {
|
2018-01-20 01:46:30 +08:00
|
|
|
if(\is_bool($value)) {
|
|
|
|
$value = $value === true ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2017-12-13 07:09:26 +08:00
|
|
|
$this->update(['value' => $value], $setting->id);
|
|
|
|
} catch (ValidatorException $e) {
|
|
|
|
Log::error($e->getMessage(), $e->getTrace());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
2017-12-10 11:56:26 +08:00
|
|
|
}
|
|
|
|
}
|