2020-08-30 22:06:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (is_null($value))
|
|
|
|
{
|
2020-08-31 18:47:27 +00:00
|
|
|
Log::info('Option ' . $option . 'not found in cache, refreshing from database');
|
2020-08-30 22:06:01 +00:00
|
|
|
$value = Option::where('option_name', $option)->first();
|
|
|
|
if (is_null($value))
|
|
|
|
throw new \Exception('This option does not exist.');
|
|
|
|
|
|
|
|
Cache::put($option, $value);
|
|
|
|
Cache::put($option . '_desc', 'Undefined description');
|
|
|
|
}
|
|
|
|
|
2020-08-31 17:54:33 +00:00
|
|
|
return $value->option_value;
|
2020-08-30 22:06:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setOption(string $option, string $value, string $description)
|
|
|
|
{
|
|
|
|
Option::create([
|
|
|
|
'option_name' => $option,
|
|
|
|
'option_value' => $value,
|
|
|
|
'friendly_name' => $description
|
|
|
|
]);
|
|
|
|
|
|
|
|
Cache::put($option, $value, now()->addDay());
|
|
|
|
Cache::put($option . '_desc', $description, now()->addDay());
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
Cache::pull($option . '_desc')
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function changeOption($option, $newValue)
|
|
|
|
{
|
|
|
|
$dbOption = Option::where('option_name', $option);
|
|
|
|
|
|
|
|
if ($dbOption->first())
|
|
|
|
{
|
2020-08-31 17:51:35 +00:00
|
|
|
$dbOptionInstance = Option::find($dbOption->first()->id);
|
2020-08-30 22:06:01 +00:00
|
|
|
Cache::forget($option);
|
|
|
|
|
2020-08-31 18:47:27 +00:00
|
|
|
Log::warning('Changing db configuration option', [
|
|
|
|
'old_value' => $dbOptionInstance->option_value,
|
|
|
|
'new_value' => $newValue
|
|
|
|
]);
|
2020-08-30 22:06:01 +00:00
|
|
|
|
|
|
|
$dbOptionInstance->option_value = $newValue;
|
|
|
|
$dbOptionInstance->save();
|
|
|
|
|
2020-08-31 18:47:27 +00:00
|
|
|
Log::warning('New db configuration option saved',
|
|
|
|
[
|
|
|
|
'option' => $dbOptionInstance->option_value
|
|
|
|
]);
|
|
|
|
|
2020-08-30 22:06:01 +00:00
|
|
|
Cache::put('option_name', $newValue, now()->addDay());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
return !is_null($dbOption) || !is_null($locallyCachedOption);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|