Browse Source

Set Options with fallback values and better boolean handling

Bubka 5 years ago
parent
commit
76ebf847df

+ 57 - 0
app/Classes/Options.php

@@ -0,0 +1,57 @@
+<?php
+
+namespace App\Classes;
+
+class Options
+{
+
+    /**
+     * Build a collection of options to apply
+     *
+     * @return Options collection
+     */
+    public static function get()
+    {
+        // Get a collection of user saved options
+        $userOptions = \Illuminate\Support\Facades\DB::table('options')->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]);
+        }
+    }
+
+
+}

+ 5 - 8
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 @@ class OptionController extends Controller
      */
     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);
     }
 }

+ 2 - 3
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());
     }
 }

+ 12 - 0
config/app.php

@@ -24,6 +24,18 @@ return [
 
     'version' => '1.0.0',
 
+    /*
+    |--------------------------------------------------------------------------
+    | Application fallback for user options
+    |--------------------------------------------------------------------------
+    |
+    */
+
+    'options' => [
+        'isDemoApp' => false,
+        'showTokenAsDot' => false,
+    ],
+
     /*
     |--------------------------------------------------------------------------
     | Application Environment

+ 1 - 1
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
             }
         },
 

+ 2 - 2
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' },

+ 2 - 10
tests/Unit/Settings/OptionTest.php

@@ -60,16 +60,8 @@ class OptionTest extends TestCase
             ->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'
                 ]
             ]);
     }