2021-09-26 22:06:49 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2021-10-15 23:46:21 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2021-09-26 22:06:49 +02:00
|
|
|
|
|
|
|
class AppstractOptionsService implements SettingServiceInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function get(string $setting)
|
|
|
|
{
|
2021-10-08 23:27:15 +02:00
|
|
|
$value = option($setting, config('2fauth.options' . $setting));
|
2021-09-26 22:06:49 +02:00
|
|
|
$value = $this->restoreType($value);
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function all() : Collection
|
|
|
|
{
|
|
|
|
// Get a collection of user saved options
|
|
|
|
$userOptions = DB::table('options')->pluck('value', 'key');
|
|
|
|
$userOptions->transform(function ($item, $key) {
|
|
|
|
return $this->restoreType($item);
|
|
|
|
});
|
2021-10-08 23:27:15 +02:00
|
|
|
$userOptions = collect(config('2fauth.options'))->merge($userOptions);
|
2021-09-26 22:06:49 +02:00
|
|
|
|
|
|
|
return $userOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function set($setting, $value = null) : void
|
|
|
|
{
|
|
|
|
$settings = is_array($setting) ? $setting : [$setting => $value];
|
|
|
|
|
|
|
|
foreach ($settings as $setting => $value) {
|
|
|
|
$settings[$setting] = $this->replaceBoolean($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
option($settings);
|
2021-10-15 23:46:21 +02:00
|
|
|
|
|
|
|
foreach ($settings as $setting => $value) {
|
|
|
|
Log::info(sprintf('Setting %s is now %s', var_export($setting, true), var_export($this->restoreType($value), true)));
|
|
|
|
}
|
2021-09-26 22:06:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function delete(string $name) : void
|
|
|
|
{
|
|
|
|
option()->remove($name);
|
2021-10-15 23:46:21 +02:00
|
|
|
|
|
|
|
Log::info(sprintf('Setting %s deleted', var_export($name, true)));
|
2021-09-26 22:06:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces boolean by a patterned string as appstrack/laravel-options package does not support var type
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Support\Collection $settings
|
|
|
|
* @return \Illuminate\Support\Collection
|
|
|
|
*/
|
|
|
|
private function replaceBoolean($value)
|
|
|
|
{
|
|
|
|
return is_bool($value) ? '{{' . $value . '}}' : $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces patterned string that represent booleans with real booleans
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Support\Collection $settings
|
|
|
|
* @return \Illuminate\Support\Collection
|
|
|
|
*/
|
|
|
|
private function restoreType($value)
|
|
|
|
{
|
|
|
|
$value = is_numeric($value) ? (float) $value : $value;
|
|
|
|
|
|
|
|
if( $value === '{{}}' ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if( $value === '{{1}}' ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|