2016-07-24 11:47:55 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* FireflyConfig.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* This software may be modified and distributed under the terms
|
|
|
|
* of the MIT license. See the LICENSE file for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Support;
|
|
|
|
|
|
|
|
use Auth;
|
|
|
|
use Cache;
|
|
|
|
use FireflyIII\Models\Configuration;
|
|
|
|
use FireflyIII\Models\Preference;
|
2016-07-24 12:24:02 -05:00
|
|
|
use Log;
|
2016-07-24 11:47:55 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class FireflyConfig
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Support
|
|
|
|
*/
|
|
|
|
class FireflyConfig
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function delete($name): bool
|
|
|
|
{
|
|
|
|
$fullName = 'preference' . Auth::user()->id . $name;
|
|
|
|
if (Cache::has($fullName)) {
|
|
|
|
Cache::forget($fullName);
|
|
|
|
}
|
|
|
|
Preference::where('user_id', Auth::user()->id)->where('name', $name)->delete();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
* @param null $default
|
|
|
|
*
|
|
|
|
* @return Configuration|null
|
|
|
|
*/
|
|
|
|
public function get($name, $default = null)
|
|
|
|
{
|
2016-07-24 12:24:02 -05:00
|
|
|
Log::debug('Now in FFConfig::get()', ['name' => $name]);
|
2016-07-24 11:47:55 -05:00
|
|
|
$fullName = 'ff-config-' . $name;
|
|
|
|
if (Cache::has($fullName)) {
|
2016-07-24 12:24:02 -05:00
|
|
|
Log::debug('Return cache.');
|
|
|
|
|
2016-07-24 11:47:55 -05:00
|
|
|
return Cache::get($fullName);
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = Configuration::where('name', $name)->first(['id', 'name', 'data']);
|
|
|
|
|
|
|
|
if ($config) {
|
|
|
|
Cache::forever($fullName, $config);
|
2016-07-24 12:24:02 -05:00
|
|
|
Log::debug('Return found one.');
|
2016-07-24 11:47:55 -05:00
|
|
|
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
// no preference found and default is null:
|
|
|
|
if (is_null($default)) {
|
|
|
|
// return NULL
|
2016-07-24 12:24:02 -05:00
|
|
|
Log::debug('Return null.');
|
|
|
|
|
2016-07-24 11:47:55 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-07-24 12:24:02 -05:00
|
|
|
Log::debug('Return this->set().');
|
|
|
|
|
2016-07-24 11:47:55 -05:00
|
|
|
return $this->set($name, $default);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-01 11:31:39 -05:00
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return Configuration
|
|
|
|
*/
|
|
|
|
public function put($name, $value): Configuration
|
|
|
|
{
|
|
|
|
return $this->set($name, $value);
|
|
|
|
}
|
|
|
|
|
2016-07-24 11:47:55 -05:00
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
* @param string $value
|
|
|
|
*
|
|
|
|
* @return Configuration
|
|
|
|
*/
|
|
|
|
public function set($name, $value): Configuration
|
|
|
|
{
|
2016-07-31 10:17:45 -05:00
|
|
|
Log::debug('Set new value for ', ['name' => $name]);
|
|
|
|
$config = Configuration::whereName($name)->first();
|
|
|
|
if (is_null($config)) {
|
|
|
|
Log::debug('Does not exist yet ', ['name' => $name]);
|
|
|
|
$item = new Configuration;
|
|
|
|
$item->name = $name;
|
|
|
|
$item->data = $value;
|
|
|
|
$item->save();
|
|
|
|
|
|
|
|
Cache::forget('ff-config-' . $name);
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
} else {
|
|
|
|
Log::debug('Exists already ', ['name' => $name]);
|
|
|
|
$config->data = $value;
|
|
|
|
$config->save();
|
|
|
|
Cache::forget('ff-config-' . $name);
|
2016-07-24 12:24:02 -05:00
|
|
|
|
2016-07-31 10:17:45 -05:00
|
|
|
return $config;
|
|
|
|
}
|
2016-07-24 11:47:55 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|