rbrecruiter/app/Helpers/Options.php

106 lines
3.3 KiB
PHP
Raw Normal View History

2020-08-30 22:06:01 +00:00
<?php
2020-10-10 16:30:26 +00:00
/*
* Copyright © 2020 Miguel Nogueira
*
* This file is part of Raspberry Staff Manager.
*
* Raspberry Staff Manager 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.
*
* Raspberry Staff Manager 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 Raspberry Staff Manager. If not, see <https://www.gnu.org/licenses/>.
*/
2020-08-30 22:06:01 +00:00
namespace App\Helpers;
use App\Options as Option;
use Illuminate\Support\Facades\Cache;
2020-08-31 18:47:27 +00:00
use Illuminate\Support\Facades\Log;
2020-08-30 22:06:01 +00:00
class Options
{
public function getOption(string $option): string
{
$value = Cache::get($option);
2020-10-10 16:30:26 +00:00
if (is_null($value)) {
Log::debug('Option '.$option.'not found in cache, refreshing from database');
2020-08-30 22:06:01 +00:00
$value = Option::where('option_name', $option)->first();
2020-10-10 16:30:26 +00:00
if (is_null($value)) {
2020-08-30 22:06:01 +00:00
throw new \Exception('This option does not exist.');
2020-10-10 16:30:26 +00:00
}
2020-08-30 22:06:01 +00:00
Cache::put($option, $value);
2020-10-10 16:30:26 +00:00
Cache::put($option.'_desc', 'Undefined description');
2020-08-30 22:06:01 +00:00
}
return $value->option_value;
2020-08-30 22:06:01 +00:00
}
public function setOption(string $option, string $value, string $description)
{
2020-10-10 16:30:26 +00:00
Option::create([
'option_name' => $option,
'option_value' => $value,
'friendly_name' => $description,
]);
Cache::put($option, $value, now()->addDay());
Cache::put($option.'_desc', $description, now()->addDay());
2020-08-30 22:06:01 +00:00
}
public function pullOption($option): array
{
$oldOption = Option::where('option_name', $option)->first();
Option::find($oldOption->id)->delete();
// putMany is overkill here
return [
Cache::pull($option),
2020-10-10 16:30:26 +00:00
Cache::pull($option.'_desc'),
2020-08-30 22:06:01 +00:00
];
}
public function changeOption($option, $newValue)
{
$dbOption = Option::where('option_name', $option);
2020-10-10 16:30:26 +00:00
if ($dbOption->first()) {
$dbOptionInstance = Option::find($dbOption->first()->id);
2020-08-30 22:06:01 +00:00
Cache::forget($option);
2020-08-31 19:02:30 +00:00
Log::debug('Changing db configuration option', [
2020-08-31 18:47:27 +00:00
'old_value' => $dbOptionInstance->option_value,
2020-10-10 16:30:26 +00:00
'new_value' => $newValue,
2020-08-31 18:47:27 +00:00
]);
2020-08-30 22:06:01 +00:00
$dbOptionInstance->option_value = $newValue;
$dbOptionInstance->save();
2020-08-31 19:02:30 +00:00
Log::debug('New db configuration option saved',
2020-08-31 18:47:27 +00:00
[
2020-10-10 16:30:26 +00:00
'option' => $dbOptionInstance->option_value,
2020-08-31 18:47:27 +00:00
]);
2020-08-30 22:06:01 +00:00
Cache::put('option_name', $newValue, now()->addDay());
2020-10-10 16:30:26 +00:00
} else {
2020-08-30 22:06:01 +00:00
throw new \Exception('This option does not exist.');
}
}
public function optionExists(string $option): bool
{
$dbOption = Option::where('option_name', $option)->first();
$locallyCachedOption = Cache::get($option);
2020-10-10 16:30:26 +00:00
return ! is_null($dbOption) || ! is_null($locallyCachedOption);
2020-08-30 22:06:01 +00:00
}
}