2017-12-10 11:56:26 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Exceptions\SettingNotFound;
|
2018-03-20 09:50:40 +08:00
|
|
|
use App\Interfaces\Repository;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Models\Setting;
|
2017-12-13 02:43:58 +08:00
|
|
|
use Illuminate\Support\Carbon;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Log;
|
2017-12-13 02:43:58 +08:00
|
|
|
use Prettus\Repository\Contracts\CacheableInterface;
|
2018-02-02 06:46:37 +08:00
|
|
|
use Prettus\Repository\Traits\CacheableRepository;
|
2017-12-13 07:09:26 +08:00
|
|
|
use Prettus\Validator\Exceptions\ValidatorException;
|
2017-12-10 11:56:26 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* Class SettingRepository
|
|
|
|
*/
|
|
|
|
class SettingRepository extends Repository implements CacheableInterface
|
2017-12-10 11:56:26 +08:00
|
|
|
{
|
|
|
|
use CacheableRepository;
|
|
|
|
|
2017-12-26 08:10:24 +08:00
|
|
|
public $cacheMinutes = 1;
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
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
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-13 02:43:58 +08:00
|
|
|
* @param string $key
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-20 01:46:30 +08:00
|
|
|
* @throws SettingNotFound
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return mixed
|
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
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if (!$setting) {
|
|
|
|
throw new SettingNotFound($key.' not found');
|
2017-12-13 02:43:58 +08:00
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// cast some types
|
2018-03-20 09:50:40 +08:00
|
|
|
switch ($setting->type) {
|
2017-12-13 02:43:58 +08:00
|
|
|
case 'bool':
|
|
|
|
case 'boolean':
|
2018-01-20 05:02:49 +08:00
|
|
|
$value = $setting->value;
|
2018-09-02 22:20:52 +08:00
|
|
|
return $value === 'true' || $value === '1' || $value === 1 || $value === 'on';
|
2017-12-13 02:43:58 +08:00
|
|
|
case 'date':
|
|
|
|
return Carbon::parse($setting->value);
|
|
|
|
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;
|
2018-01-01 01:09:56 +08:00
|
|
|
case 'float':
|
|
|
|
return (float) $setting->value;
|
2017-12-13 02:43:58 +08:00
|
|
|
default:
|
|
|
|
return $setting->value;
|
|
|
|
}
|
2017-12-10 11:56:26 +08:00
|
|
|
}
|
|
|
|
|
2018-02-10 04:26:14 +08:00
|
|
|
/**
|
|
|
|
* @alias store($key,$value)
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @param mixed $key
|
|
|
|
* @param mixed $value
|
2018-08-27 02:51:47 +08:00
|
|
|
*
|
2018-08-27 02:50:08 +08:00
|
|
|
* @return null
|
2018-02-10 04:26:14 +08:00
|
|
|
*/
|
|
|
|
public function save($key, $value)
|
|
|
|
{
|
|
|
|
return $this->store($key, $value);
|
|
|
|
}
|
|
|
|
|
2017-12-13 07:09:26 +08:00
|
|
|
/**
|
|
|
|
* Update an existing setting with a new value. Doesn't create
|
|
|
|
* a new setting
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-13 07:09:26 +08:00
|
|
|
* @param $key
|
|
|
|
* @param $value
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-13 07:09:26 +08:00
|
|
|
* @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],
|
2018-08-27 00:40:04 +08:00
|
|
|
['id', 'value'] // only get these columns
|
2018-01-20 01:46:30 +08:00
|
|
|
)->first();
|
|
|
|
|
2017-12-13 02:43:58 +08:00
|
|
|
if (!$setting) {
|
2018-08-27 00:40:04 +08:00
|
|
|
return;
|
2017-12-13 02:43:58 +08:00
|
|
|
}
|
2017-12-10 11:56:26 +08:00
|
|
|
|
2017-12-13 07:09:26 +08:00
|
|
|
try {
|
2018-03-20 09:50:40 +08:00
|
|
|
if (\is_bool($value)) {
|
2018-01-20 01:46:30 +08:00
|
|
|
$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
|
|
|
}
|
|
|
|
}
|