Files
firefly-iii/app/Support/FireflyConfig.php

160 lines
4.4 KiB
PHP
Raw Normal View History

<?php
/**
* FireflyConfig.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02: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
2017-12-17 14:44:05 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support;
use Cache;
2018-07-14 15:21:05 +02:00
use Exception;
use FireflyIII\Models\Configuration;
2016-07-24 19:24:02 +02:00
use Log;
/**
2017-11-15 12:25:49 +01:00
* Class FireflyConfig.
*/
class FireflyConfig
{
/**
2018-07-14 15:21:05 +02:00
* @param string $name
*/
2018-07-14 15:21:05 +02:00
public function delete(string $name): void
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
2016-10-07 11:40:03 +02:00
$fullName = 'ff-config-' . $name;
if (Cache::has($fullName)) {
Cache::forget($fullName);
}
2018-07-14 15:21:05 +02:00
try {
Configuration::where('name', $name)->delete();
} catch (Exception $e) {
Log::debug(sprintf('Could not delete config value: %s', $e->getMessage()));
2018-07-14 15:21:05 +02:00
}
}
/**
2018-07-14 15:21:05 +02:00
* @param string $name
* @param mixed $default
*
2016-12-12 15:24:47 +01:00
* @return \FireflyIII\Models\Configuration|null
*/
2018-07-27 05:03:37 +02:00
public function get(string $name, $default = null): ?Configuration
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
$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:
2017-11-15 12:25:49 +01:00
if (null === $default) {
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
* @param mixed $default
2018-03-07 21:01:46 +01:00
*
* @return \FireflyIII\Models\Configuration|null
*/
2018-07-14 15:21:05 +02:00
public function getFresh(string $name, $default = null): ?Configuration
2018-03-07 21:01:46 +01:00
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
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;
}
return $this->set($name, $default);
}
/**
2018-07-14 15:21:05 +02:00
* @param string $name
* @param $value
*
* @return Configuration
*/
2018-07-14 15:21:05 +02:00
public function put(string $name, $value): Configuration
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
return $this->set($name, $value);
}
/**
2016-12-12 17:17:36 +01:00
* @param string $name
* @param $value
*
* @return Configuration
*/
2016-12-12 17:17:36 +01:00
public function set(string $name, $value): Configuration
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
Log::debug('Set new value for ', ['name' => $name]);
/** @var Configuration $config */
$config = Configuration::whereName($name)->first();
2017-11-15 12:25:49 +01:00
if (null === $config) {
Log::debug('Does not exist yet ', ['name' => $name]);
/** @var Configuration $item */
$item = new Configuration;
$item->name = $name;
$item->data = $value;
$item->save();
Cache::forget('ff-config-' . $name);
return $item;
}
Log::debug('Exists already, overwrite value.', ['name' => $name]);
2017-09-09 07:03:43 +02:00
$config->data = $value;
$config->save();
Cache::forget('ff-config-' . $name);
return $config;
}
}