From 76ebf847df5c6429dfc13bdd588f146ecdf49864 Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Fri, 13 Mar 2020 22:10:36 +0100 Subject: [PATCH] Set Options with fallback values and better boolean handling --- app/Classes/Options.php | 57 +++++++++++++++++++ .../Controllers/Settings/OptionController.php | 13 ++--- app/Http/Controllers/SinglePageController.php | 5 +- config/app.php | 12 ++++ resources/js/components/TwofaccountShow.vue | 2 +- resources/js/views/settings/Options.vue | 4 +- tests/Unit/Settings/OptionTest.php | 12 +--- 7 files changed, 81 insertions(+), 24 deletions(-) create mode 100644 app/Classes/Options.php diff --git a/app/Classes/Options.php b/app/Classes/Options.php new file mode 100644 index 00000000..60c983ca --- /dev/null +++ b/app/Classes/Options.php @@ -0,0 +1,57 @@ +pluck('value', 'key'); + + // We replace patterned string that represent booleans with real booleans + $userOptions->transform(function ($item, $key) { + if( $item === '{{}}' ) { + return false; + } + else if( $item === '{{1}}' ) { + return true; + } + else { + return $item; + } + }); + + // Merge options from App configuration. It ensures we have a complete options collection with + // fallback values for every options + $options = collect(config('app.options'))->merge($userOptions); + + return $options; + } + + + /** + * Set user options + * + * @param array All options to store + * @return void + */ + public static function store($userOptions) + { + foreach($userOptions as $opt => $val) { + + // We replace boolean values by a patterned string in order to retrieve + // them later (as the Laravel Options package do not support var type) + // Not a beatufilly solution but, hey, it works ^_^ + option([$opt => is_bool($val) ? '{{' . $val . '}}' : $val]); + } + } + + +} diff --git a/app/Http/Controllers/Settings/OptionController.php b/app/Http/Controllers/Settings/OptionController.php index 929fa945..8f79ebf9 100644 --- a/app/Http/Controllers/Settings/OptionController.php +++ b/app/Http/Controllers/Settings/OptionController.php @@ -2,8 +2,8 @@ namespace App\Http\Controllers\Settings; +use App\Classes\Options; use Illuminate\Http\Request; -use Illuminate\Support\Facades\DB; use App\Http\Controllers\Controller; class OptionController extends Controller @@ -17,7 +17,7 @@ class OptionController extends Controller public function index() { // Fetch all setting values - $settings = DB::table('options')->get(); + $settings = Options::get(); return response()->json(['settings' => $settings], 200); } @@ -29,12 +29,9 @@ public function index() */ public function store(Request $request) { - // Store all setting values - foreach($request->all() as $opt => $val) { - option([$opt => $val]); - $settings[$opt] = option($opt); - } + // Store all options + Options::store($request->all()); - return response()->json(['message' => __('settings.forms.setting_saved'), 'settings' => $settings], 200); + return response()->json(['message' => __('settings.forms.setting_saved'), 'settings' => Options::get()], 200); } } diff --git a/app/Http/Controllers/SinglePageController.php b/app/Http/Controllers/SinglePageController.php index 3d68b0e8..74c272ba 100644 --- a/app/Http/Controllers/SinglePageController.php +++ b/app/Http/Controllers/SinglePageController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Classes\Options; use Illuminate\Http\Request; class SinglePageController extends Controller @@ -13,8 +14,6 @@ class SinglePageController extends Controller */ public function index() { - $appSettings = \Illuminate\Support\Facades\DB::table('options')->pluck('value', 'key')->toJson(); - - return view('landing')->with('appSettings', $appSettings); + return view('landing')->with('appSettings', Options::get()->toJson()); } } diff --git a/config/app.php b/config/app.php index 9b52acb5..b993fbb0 100644 --- a/config/app.php +++ b/config/app.php @@ -24,6 +24,18 @@ 'version' => '1.0.0', + /* + |-------------------------------------------------------------------------- + | Application fallback for user options + |-------------------------------------------------------------------------- + | + */ + + 'options' => [ + 'isDemoApp' => false, + 'showTokenAsDot' => false, + ], + /* |-------------------------------------------------------------------------- | Application Environment diff --git a/resources/js/components/TwofaccountShow.vue b/resources/js/components/TwofaccountShow.vue index cc01de4e..5494e4b3 100644 --- a/resources/js/components/TwofaccountShow.vue +++ b/resources/js/components/TwofaccountShow.vue @@ -42,7 +42,7 @@ computed: { displayedOtp() { - return Boolean(Number(appSettings.showTokenAsDot)) ? this.otp.replace(/[0-9]/g, '●') : this.otp + return appSettings.showTokenAsDot ? this.otp.replace(/[0-9]/g, '●') : this.otp } }, diff --git a/resources/js/views/settings/Options.vue b/resources/js/views/settings/Options.vue index ce2440ef..fb2df708 100644 --- a/resources/js/views/settings/Options.vue +++ b/resources/js/views/settings/Options.vue @@ -23,8 +23,8 @@ fail: '', form: new Form({ lang: this.$root.$i18n.locale, - showTokenAsDot: Boolean(Number(appSettings.showTokenAsDot)), - isDemoApp: Boolean(Number(appSettings.isDemoApp)), + showTokenAsDot: appSettings.showTokenAsDot, + isDemoApp: appSettings.isDemoApp, }), options: [ { text: this.$t('languages.en'), value: 'en' }, diff --git a/tests/Unit/Settings/OptionTest.php b/tests/Unit/Settings/OptionTest.php index 1b1c5075..5fbaba0b 100644 --- a/tests/Unit/Settings/OptionTest.php +++ b/tests/Unit/Settings/OptionTest.php @@ -60,16 +60,8 @@ public function testSettingsIndexListing() ->assertStatus(200) ->assertJson([ 'settings' => [ - [ - 'id' => '1', - 'key' => 'setting_1', - 'value' => 'value_1' - ], - [ - 'id' => '2', - 'key' => 'setting_2', - 'value' => 'value_2' - ] + 'setting_1' => 'value_1', + 'setting_2' => 'value_2' ] ]); }