2016-07-24 18:47:55 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* FireflyConfig.php
|
2020-02-16 13:56:52 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-07-24 18:47:55 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02: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 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 08:40:00 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02: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 18:47:55 +02:00
|
|
|
*/
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2016-07-24 18:47:55 +02:00
|
|
|
|
|
|
|
namespace FireflyIII\Support;
|
|
|
|
|
|
|
|
use Cache;
|
2018-07-14 15:21:05 +02:00
|
|
|
use Exception;
|
2020-07-06 06:49:48 +02:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2016-07-24 18:47:55 +02:00
|
|
|
use FireflyIII\Models\Configuration;
|
2020-06-06 12:14:55 +02:00
|
|
|
use Illuminate\Database\QueryException;
|
2016-07-24 18:47:55 +02:00
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class FireflyConfig.
|
2021-03-21 09:15:40 +01:00
|
|
|
*
|
2019-07-31 16:53:09 +02:00
|
|
|
* @codeCoverageIgnore
|
2016-07-24 18:47:55 +02:00
|
|
|
*/
|
|
|
|
class FireflyConfig
|
|
|
|
{
|
2019-06-22 10:25:57 +02:00
|
|
|
|
2016-07-24 18:47:55 +02:00
|
|
|
/**
|
2018-07-14 15:21:05 +02:00
|
|
|
* @param string $name
|
2016-07-24 18:47:55 +02:00
|
|
|
*/
|
2018-07-14 15:21:05 +02:00
|
|
|
public function delete(string $name): void
|
2016-07-24 18:47:55 +02:00
|
|
|
{
|
2016-10-07 11:40:03 +02:00
|
|
|
$fullName = 'ff-config-' . $name;
|
2016-07-24 18:47:55 +02:00
|
|
|
if (Cache::has($fullName)) {
|
|
|
|
Cache::forget($fullName);
|
|
|
|
}
|
2018-07-14 15:21:05 +02:00
|
|
|
try {
|
2021-06-08 19:19:56 +02:00
|
|
|
Configuration::where('name', $name)->forceDelete();
|
2021-04-06 17:00:16 +02:00
|
|
|
} catch (Exception $e) { // @phpstan-ignore-line
|
|
|
|
// @ignoreException
|
2018-07-14 15:21:05 +02:00
|
|
|
}
|
2016-07-24 18:47:55 +02:00
|
|
|
}
|
|
|
|
|
2019-09-21 11:03:00 +02:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function has(string $name): bool
|
|
|
|
{
|
|
|
|
return Configuration::where('name', $name)->count() === 1;
|
|
|
|
}
|
|
|
|
|
2016-07-24 18:47:55 +02:00
|
|
|
/**
|
2021-04-06 17:00:16 +02:00
|
|
|
* @param string $name
|
|
|
|
* @param bool|string|int|null $default
|
2016-07-24 18:47:55 +02:00
|
|
|
*
|
2020-07-06 06:49:48 +02:00
|
|
|
* @return Configuration|null
|
2021-03-21 09:15:40 +01:00
|
|
|
* @throws FireflyException
|
2016-07-24 18:47:55 +02:00
|
|
|
*/
|
2018-07-27 05:03:37 +02:00
|
|
|
public function get(string $name, $default = null): ?Configuration
|
2016-07-24 18:47:55 +02:00
|
|
|
{
|
|
|
|
$fullName = 'ff-config-' . $name;
|
|
|
|
if (Cache::has($fullName)) {
|
|
|
|
return Cache::get($fullName);
|
|
|
|
}
|
|
|
|
|
2020-06-06 12:14:55 +02:00
|
|
|
try {
|
2021-04-06 17:00:16 +02:00
|
|
|
/** @var Configuration|null $config */
|
2020-06-06 12:14:55 +02:00
|
|
|
$config = Configuration::where('name', $name)->first(['id', 'name', 'data']);
|
2021-04-06 17:00:16 +02:00
|
|
|
} catch (QueryException | Exception $e) { // @phpstan-ignore-line
|
2020-07-06 06:49:48 +02:00
|
|
|
throw new FireflyException(sprintf('Could not poll the database: %s', $e->getMessage()));
|
2020-06-06 12:14:55 +02:00
|
|
|
}
|
2016-07-24 18:47:55 +02:00
|
|
|
|
2021-04-06 17:00:16 +02:00
|
|
|
if (null !== $config) {
|
2016-07-24 18:47:55 +02:00
|
|
|
Cache::forever($fullName, $config);
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
// no preference found and default is null:
|
2017-11-15 12:25:49 +01:00
|
|
|
if (null === $default) {
|
2016-07-24 18:47:55 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-03-07 21:01:46 +01:00
|
|
|
return $this->set($name, $default);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-14 15:21:05 +02:00
|
|
|
* @param string $name
|
2021-03-21 09:15:40 +01:00
|
|
|
* @param mixed $default
|
2018-03-07 21:01:46 +01:00
|
|
|
*
|
2021-05-24 08:57:02 +02:00
|
|
|
* @return Configuration|null
|
2018-03-07 21:01:46 +01:00
|
|
|
*/
|
2018-07-14 15:21:05 +02:00
|
|
|
public function getFresh(string $name, $default = null): ?Configuration
|
2018-03-07 21:01:46 +01:00
|
|
|
{
|
2021-03-13 12:01:01 +01:00
|
|
|
|
2018-03-07 21:01:46 +01:00
|
|
|
$config = Configuration::where('name', $name)->first(['id', 'name', 'data']);
|
|
|
|
if ($config) {
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
// no preference found and default is null:
|
|
|
|
if (null === $default) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-07-24 18:47:55 +02:00
|
|
|
return $this->set($name, $default);
|
|
|
|
}
|
|
|
|
|
2016-09-01 18:31:39 +02:00
|
|
|
/**
|
2018-07-14 15:21:05 +02:00
|
|
|
* @param string $name
|
2021-04-06 17:00:16 +02:00
|
|
|
* @param mixed $value
|
2016-09-01 18:31:39 +02:00
|
|
|
*
|
|
|
|
* @return Configuration
|
|
|
|
*/
|
2018-07-14 15:21:05 +02:00
|
|
|
public function put(string $name, $value): Configuration
|
2016-09-01 18:31:39 +02:00
|
|
|
{
|
|
|
|
return $this->set($name, $value);
|
|
|
|
}
|
|
|
|
|
2016-07-24 18:47:55 +02:00
|
|
|
/**
|
2021-04-06 17:00:16 +02:00
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
2016-07-24 18:47:55 +02:00
|
|
|
*
|
|
|
|
* @return Configuration
|
|
|
|
*/
|
2016-12-12 17:17:36 +01:00
|
|
|
public function set(string $name, $value): Configuration
|
2016-07-24 18:47:55 +02:00
|
|
|
{
|
2020-06-06 12:14:55 +02:00
|
|
|
try {
|
2021-06-08 19:19:56 +02:00
|
|
|
$config = Configuration::whereName($name)->whereNull('deleted_at')->first();
|
2021-04-06 17:00:16 +02:00
|
|
|
} catch (QueryException | Exception $e) { // @phpstan-ignore-line
|
2020-06-06 12:14:55 +02:00
|
|
|
$item = new Configuration;
|
|
|
|
$item->name = $name;
|
|
|
|
$item->data = $value;
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
}
|
2021-06-08 19:19:56 +02:00
|
|
|
|
2017-11-15 12:25:49 +01:00
|
|
|
if (null === $config) {
|
2016-07-31 17:17:45 +02:00
|
|
|
$item = new Configuration;
|
|
|
|
$item->name = $name;
|
|
|
|
$item->data = $value;
|
|
|
|
$item->save();
|
|
|
|
Cache::forget('ff-config-' . $name);
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
}
|
2017-09-09 07:03:43 +02:00
|
|
|
$config->data = $value;
|
|
|
|
$config->save();
|
|
|
|
Cache::forget('ff-config-' . $name);
|
|
|
|
|
|
|
|
return $config;
|
2016-07-24 18:47:55 +02:00
|
|
|
}
|
|
|
|
}
|