2016-07-24 11:47:55 -05:00
|
|
|
<?php
|
2022-12-29 12:42:26 -06:00
|
|
|
|
2016-07-24 11:47:55 -05:00
|
|
|
/**
|
|
|
|
* FireflyConfig.php
|
2020-02-16 06:56:52 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-07-24 11:47:55 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 01:40:00 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://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;
|
|
|
|
|
2020-07-05 23:49:48 -05:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2016-07-24 11:47:55 -05:00
|
|
|
use FireflyIII\Models\Configuration;
|
2020-06-06 05:14:55 -05:00
|
|
|
use Illuminate\Database\QueryException;
|
2016-07-24 11:47:55 -05:00
|
|
|
|
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class FireflyConfig.
|
2016-07-24 11:47:55 -05:00
|
|
|
*/
|
|
|
|
class FireflyConfig
|
|
|
|
{
|
2018-07-14 08:21:05 -05:00
|
|
|
public function delete(string $name): void
|
2016-07-24 11:47:55 -05:00
|
|
|
{
|
2023-12-20 12:35:52 -06:00
|
|
|
$fullName = 'ff-config-'.$name;
|
|
|
|
if (\Cache::has($fullName)) {
|
|
|
|
\Cache::forget($fullName);
|
2016-07-24 11:47:55 -05:00
|
|
|
}
|
2022-12-30 13:38:54 -06:00
|
|
|
Configuration::where('name', $name)->forceDelete();
|
2016-07-24 11:47:55 -05:00
|
|
|
}
|
|
|
|
|
2023-06-21 05:34:58 -05:00
|
|
|
public function has(string $name): bool
|
|
|
|
{
|
2023-12-20 12:35:52 -06:00
|
|
|
return 1 === Configuration::where('name', $name)->count();
|
2023-06-21 05:34:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-20 12:35:52 -06:00
|
|
|
* @param null|bool|int|string $default
|
2016-07-24 11:47:55 -05:00
|
|
|
*
|
2021-03-21 03:15:40 -05:00
|
|
|
* @throws FireflyException
|
2016-07-24 11:47:55 -05:00
|
|
|
*/
|
2018-07-26 22:03:37 -05:00
|
|
|
public function get(string $name, $default = null): ?Configuration
|
2016-07-24 11:47:55 -05:00
|
|
|
{
|
2023-12-20 12:35:52 -06:00
|
|
|
$fullName = 'ff-config-'.$name;
|
|
|
|
if (\Cache::has($fullName)) {
|
|
|
|
return \Cache::get($fullName);
|
2016-07-24 11:47:55 -05:00
|
|
|
}
|
|
|
|
|
2020-06-06 05:14:55 -05:00
|
|
|
try {
|
2023-12-20 12:35:52 -06:00
|
|
|
/** @var null|Configuration $config */
|
2020-06-06 05:14:55 -05:00
|
|
|
$config = Configuration::where('name', $name)->first(['id', 'name', 'data']);
|
2023-12-20 12:35:52 -06:00
|
|
|
} catch (\Exception|QueryException $e) {
|
2022-12-30 13:25:04 -06:00
|
|
|
throw new FireflyException(sprintf('Could not poll the database: %s', $e->getMessage()), 0, $e);
|
2020-06-06 05:14:55 -05:00
|
|
|
}
|
2016-07-24 11:47:55 -05:00
|
|
|
|
2021-04-06 10:00:16 -05:00
|
|
|
if (null !== $config) {
|
2023-12-20 12:35:52 -06:00
|
|
|
\Cache::forever($fullName, $config);
|
2016-07-24 11:47:55 -05:00
|
|
|
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
// no preference found and default is null:
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null === $default) {
|
2016-07-24 11:47:55 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-03-07 14:01:46 -06:00
|
|
|
return $this->set($name, $default);
|
|
|
|
}
|
|
|
|
|
2023-05-29 06:56:55 -05:00
|
|
|
/**
|
2023-12-20 12:35:52 -06:00
|
|
|
* @param mixed $value
|
2021-09-18 03:26:12 -05:00
|
|
|
*/
|
|
|
|
public function set(string $name, $value): Configuration
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$config = Configuration::whereName($name)->whereNull('deleted_at')->first();
|
2022-12-30 13:25:04 -06:00
|
|
|
} catch (QueryException $e) {
|
2023-10-29 00:32:00 -05:00
|
|
|
app('log')->error($e->getMessage());
|
2022-10-30 08:24:37 -05:00
|
|
|
$item = new Configuration();
|
2021-09-18 03:26:12 -05:00
|
|
|
$item->name = $name;
|
|
|
|
$item->data = $value;
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null === $config) {
|
2022-10-30 08:24:37 -05:00
|
|
|
$item = new Configuration();
|
2021-09-18 03:26:12 -05:00
|
|
|
$item->name = $name;
|
|
|
|
$item->data = $value;
|
|
|
|
$item->save();
|
2023-12-20 12:35:52 -06:00
|
|
|
\Cache::forget('ff-config-'.$name);
|
2021-09-18 03:26:12 -05:00
|
|
|
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
$config->data = $value;
|
|
|
|
$config->save();
|
2023-12-20 12:35:52 -06:00
|
|
|
\Cache::forget('ff-config-'.$name);
|
2021-09-18 03:26:12 -05:00
|
|
|
|
|
|
|
return $config;
|
|
|
|
}
|
2023-06-21 05:34:58 -05:00
|
|
|
|
|
|
|
/**
|
2023-12-20 12:35:52 -06:00
|
|
|
* @param mixed $default
|
2023-06-21 05:34:58 -05:00
|
|
|
*/
|
|
|
|
public function getFresh(string $name, $default = null): ?Configuration
|
|
|
|
{
|
|
|
|
$config = Configuration::where('name', $name)->first(['id', 'name', 'data']);
|
2023-11-05 02:40:45 -06:00
|
|
|
if (null !== $config) {
|
2023-06-21 05:34:58 -05:00
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
// no preference found and default is null:
|
|
|
|
if (null === $default) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->set($name, $default);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-20 12:35:52 -06:00
|
|
|
* @param mixed $value
|
2023-06-21 05:34:58 -05:00
|
|
|
*/
|
|
|
|
public function put(string $name, $value): Configuration
|
|
|
|
{
|
|
|
|
return $this->set($name, $value);
|
|
|
|
}
|
2016-07-24 11:47:55 -05:00
|
|
|
}
|