Passed
Push — master ( d0ad7b...a7252f )
by Diego
03:00
created

DarkModeController::isEnabled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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