Passed
Pull Request — master (#908)
by Diego
04:52 queued 01:31
created

DarkModeController::disable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Http\Controllers;
4
5
class DarkModeController extends Controller
6
{
7
    /**
8
     * The key to use for save dark mode status on the session.
9
     *
10
     * @var string
11
     */
12
    protected $sessionKey = 'adminlte_dark_mode';
13
14
    /**
15
     * Toggle the dark mode status.
16
     *
17
     * @return void
18
     */
19
    public function toggle()
20
    {
21
        // Store the new darkmode status on the session. This way, we can keep
22
        // the dark mode preference over multiple requests.
23
24
        session([$this->sessionKey => ! $this->isEnabled()]);
25
26
        // TODO: We can trigger/dispatch an event so the end user may have a
27
        // way to catch the new darkmode status and update the preference on a
28
        // database, for example.
29
    }
30
31
    /**
32
     * Check if the dark mode is enabled or not.
33
     *
34
     * @return bool
35
     */
36 12
    public function isEnabled()
37
    {
38
        // First, check if dark mode status was previously saved on the session.
39
40 12
        if (! is_null(session($this->sessionKey, null))) {
41
            return session($this->sessionKey);
42
        }
43
44
        // Otherwise, fallback to the default package configuration value.
45
46 12
        return (bool) config('adminlte.layout_dark_mode', false);
47
    }
48
49
    /**
50
     * Enables the dark mode.
51
     *
52
     * @return void
53
     */
54
    public function enable()
55
    {
56
        session([$this->sessionKey => true]);
57
    }
58
59
    /**
60
     * Disables the dark mode.
61
     *
62
     * @return void
63
     */
64
    public function disable()
65
    {
66
        session([$this->sessionKey => false]);
67
    }
68
}
69