feat(overrides): add override functionality to Options
Signed-off-by: miguel456 <me@nogueira.codes>
This commit is contained in:
23
app/Enums/Overrides.php
Normal file
23
app/Enums/Overrides.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
enum Overrides:int
|
||||
{
|
||||
|
||||
/**
|
||||
* Pass control to app settings
|
||||
*/
|
||||
case control = 0;
|
||||
|
||||
/**
|
||||
* Forcefully enable feature
|
||||
*/
|
||||
case forceEnable = 1;
|
||||
|
||||
/**
|
||||
* Forcefully disable feature
|
||||
*/
|
||||
case killSwitch = 2;
|
||||
|
||||
}
|
@@ -21,18 +21,19 @@
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
use App\Enums\Overrides;
|
||||
use App\Exceptions\EmptyOptionsException;
|
||||
use App\Exceptions\OptionNotFoundException;
|
||||
use App\Options as Option;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use LogicException;
|
||||
|
||||
|
||||
/**
|
||||
* The options class. A simple wrapper around the model. Could be a repository, but we're not using that design pattern just yet
|
||||
*/
|
||||
class Options
|
||||
{
|
||||
|
||||
/**
|
||||
* Returns an assortment of settings found in the mentioned category
|
||||
*
|
||||
@@ -70,7 +71,33 @@ class Options
|
||||
return $value;
|
||||
}
|
||||
|
||||
// Null categories are settings without categories and will appear ungrouped
|
||||
|
||||
/**
|
||||
* CHeck if given option is locallu overriden
|
||||
*
|
||||
* @param string $option Option to check
|
||||
* @throws LogicException Thrown when feature is not locally overridable, e.g. not defined in the settings file
|
||||
* @return bool
|
||||
*/
|
||||
public function isOverriden(string $option): bool {
|
||||
|
||||
if (!array_key_exists($option, config('local-overrides.features'))) {
|
||||
throw new LogicException('Feature does not exist or is not locally overridable');
|
||||
}
|
||||
$feature = config("local-overrides.features.$option");
|
||||
|
||||
return ($feature == Overrides::forceEnable->value || $feature == Overrides::killSwitch->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines an option to store in the database.
|
||||
*
|
||||
* @param string $option Option name.
|
||||
* @param string $value Option value.
|
||||
* @param string $description Description for this, can be null
|
||||
* @param string|null $category Option category
|
||||
* @return void
|
||||
*/
|
||||
public function setOption(string $option, string $value, string $description, string $category = null)
|
||||
{
|
||||
Option::create([
|
||||
@@ -84,6 +111,12 @@ class Options
|
||||
Cache::put($option.'_desc', $description, now()->addDay());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets and deletes option
|
||||
*
|
||||
* @param string $option Option to pull
|
||||
* @return array
|
||||
*/
|
||||
public function pullOption($option): array
|
||||
{
|
||||
$oldOption = Option::where('option_name', $option)->first();
|
||||
@@ -96,6 +129,14 @@ class Options
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates an option
|
||||
*
|
||||
* @param $option
|
||||
* @param $newValue
|
||||
* @return void
|
||||
* @throws OptionNotFoundException
|
||||
*/
|
||||
public function changeOption($option, $newValue)
|
||||
{
|
||||
$dbOption = Option::where('option_name', $option);
|
||||
@@ -123,6 +164,12 @@ class Options
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if option exists
|
||||
*
|
||||
* @param string $option
|
||||
* @return bool
|
||||
*/
|
||||
public function optionExists(string $option): bool
|
||||
{
|
||||
$dbOption = Option::where('option_name', $option)->first();
|
||||
@@ -130,4 +177,16 @@ class Options
|
||||
|
||||
return ! is_null($dbOption) || ! is_null($locallyCachedOption);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the actual status for the override in question
|
||||
*
|
||||
* @param string $option
|
||||
* @return bool|Overrides
|
||||
*/
|
||||
public function getOverride(string $option): bool|string {
|
||||
return config("local-overrides.features.$option");
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user