2016-07-24 11:47:55 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* FireflyConfig.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-07-24 11:47:55 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-07-24 11:47:55 -05:00
|
|
|
*/
|
|
|
|
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2016-07-24 11:47:55 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Support;
|
|
|
|
|
|
|
|
use Cache;
|
|
|
|
use FireflyIII\Models\Configuration;
|
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
|
|
|
|
{
|
2016-10-07 04:40:03 -05:00
|
|
|
$fullName = 'ff-config-' . $name;
|
2016-07-24 11:47:55 -05:00
|
|
|
if (Cache::has($fullName)) {
|
|
|
|
Cache::forget($fullName);
|
|
|
|
}
|
2016-10-07 04:40:03 -05:00
|
|
|
Configuration::where('name', $name)->delete();
|
2016-07-24 11:47:55 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
* @param null $default
|
|
|
|
*
|
2016-12-12 08:24:47 -06:00
|
|
|
* @return \FireflyIII\Models\Configuration|null
|
2016-07-24 11:47:55 -05:00
|
|
|
*/
|
|
|
|
public function get($name, $default = null)
|
|
|
|
{
|
|
|
|
$fullName = 'ff-config-' . $name;
|
|
|
|
if (Cache::has($fullName)) {
|
|
|
|
return Cache::get($fullName);
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = Configuration::where('name', $name)->first(['id', 'name', 'data']);
|
|
|
|
|
|
|
|
if ($config) {
|
|
|
|
Cache::forever($fullName, $config);
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
// no preference found and default is null:
|
|
|
|
if (is_null($default)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
/**
|
2016-12-12 10:17:36 -06:00
|
|
|
* @param string $name
|
|
|
|
* @param $value
|
2016-07-24 11:47:55 -05:00
|
|
|
*
|
|
|
|
* @return Configuration
|
|
|
|
*/
|
2016-12-12 10:17:36 -06:00
|
|
|
public function set(string $name, $value): Configuration
|
2016-07-24 11:47:55 -05:00
|
|
|
{
|
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;
|
|
|
|
}
|
2017-09-09 00:03:43 -05:00
|
|
|
Log::debug('Exists already ', ['name' => $name]);
|
|
|
|
$config->data = $value;
|
|
|
|
$config->save();
|
|
|
|
Cache::forget('ff-config-' . $name);
|
|
|
|
|
|
|
|
return $config;
|
2016-07-24 11:47:55 -05:00
|
|
|
}
|
|
|
|
}
|