Completed
Pull Request — master (#571)
by
unknown
02:54
created

AdminLte::menu()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7.3329

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 8
cts 12
cp 0.6667
rs 8.9137
c 0
b 0
f 0
cc 6
nc 10
nop 1
crap 7.3329
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte;
4
5
use Illuminate\Contracts\Container\Container;
6
use Illuminate\Contracts\Events\Dispatcher;
7
use Illuminate\Support\Facades\View;
8
use JeroenNoten\LaravelAdminLte\Events\BuildingMenu;
9
use JeroenNoten\LaravelAdminLte\Menu\Builder;
10
11
class AdminLte
12
{
13
    protected $menu;
14
15
    protected $filters;
16
17
    protected $events;
18
19
    protected $container;
20
21 1
    public function __construct(
22
        array $filters,
23
        Dispatcher $events,
24
        Container $container
25
    ) {
26 1
        $this->filters = $filters;
27 1
        $this->events = $events;
28 1
        $this->container = $container;
29 1
    }
30
31 1
    public function menu($filterOpt = null)
32
    {
33 1
        if (! $this->menu) {
34 1
            $this->menu = $this->buildMenu();
35
        }
36
37
        // Check for filter option.
38
39 1
        if ($filterOpt == "sidebar") {
40
            return array_filter($this->menu, [$this, 'sidebarFilter']);
41
        }
42 1
        elseif ($filterOpt == "navbar-left") {
43
            return array_filter($this->menu, [$this, 'navbarLeftFilter']);
44
        }
45 1
        elseif ($filterOpt == "navbar-right") {
46
            return array_filter($this->menu, [$this, 'navbarRightFilter']);
47
        }
48 1
        elseif ($filterOpt == "navbar-user") {
49
            return array_filter($this->menu, [$this, 'navbarUserMenuFilter']);
50
        }
51
        else {
52 1
            return $this->menu;
53
        }
54
    }
55
56
    public function getBodyClasses()
57
    {
58
        $body_classes = [];
59
        $screen_sizes = ['xs', 'sm', 'md', 'lg', 'xl'];
60
61
        // Add classes related to the "sidebar_mini" configuration.
62
63
        if (config('adminlte.sidebar_mini', true) === true) {
64
            $body_classes[] = 'sidebar-mini';
65
        } elseif (config('adminlte.sidebar_mini', true) == 'md') {
66
            $body_classes[] = 'sidebar-mini sidebar-mini-md';
67
        }
68
69
        // Add classes related to the "layout_topnav" configuration.
70
71
        if (config('adminlte.layout_topnav') || View::getSection('layout_topnav')) {
72
            $body_classes[] = 'layout-top-nav';
73
        }
74
75
        // Add classes related to the "layout_boxed" configuration.
76
77
        if (config('adminlte.layout_boxed')) {
78
            $body_classes[] = 'layout-boxed';
79
        }
80
81
        // Add classes related to the "sidebar_collapse" configuration.
82
83
        if (config('adminlte.sidebar_collapse') || View::getSection('sidebar_collapse')) {
84
            $body_classes[] = 'sidebar-collapse';
85
        }
86
87
        // Add classes related to the "right_sidebar" configuration.
88
89
        if (config('adminlte.right_sidebar') && config('adminlte.right_sidebar_push')) {
90
            $body_classes[] = 'control-sidebar-push';
91
        }
92
93
        // Add classes related to the fixed layout configuration, these are not
94
        // compatible with "layout_topnav".
95
96
        if (! config('adminlte.layout_topnav') && ! View::getSection('layout_topnav')) {
97
            // Check for fixed sidebar configuration.
98
99
            if (config('adminlte.layout_fixed_sidebar')) {
100
                $body_classes[] = 'layout-fixed';
101
            }
102
103
            // Check for fixed navbar configuration.
104
105
            $fixed_navbar_cfg = config('adminlte.layout_fixed_navbar');
106
107 View Code Duplication
            if ($fixed_navbar_cfg === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
                $body_classes[] = 'layout-navbar-fixed';
109
            } elseif (is_array($fixed_navbar_cfg)) {
110
                foreach ($fixed_navbar_cfg as $size => $enabled) {
111
                    if (in_array($size, $screen_sizes)) {
112
                        $size = $size == 'xs' ? '' : '-'.$size;
113
                        $body_classes[] = $enabled == true ?
114
                            'layout'.$size.'-navbar-fixed' :
115
                            'layout'.$size.'-navbar-not-fixed';
116
                    }
117
                }
118
            }
119
120
            // Check for fixed footer configuration.
121
122
            $fixed_footer_cfg = config('adminlte.layout_fixed_footer');
123
124 View Code Duplication
            if ($fixed_footer_cfg === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
                $body_classes[] = 'layout-footer-fixed';
126
            } elseif (is_array($fixed_footer_cfg)) {
127
                foreach ($fixed_footer_cfg as $size => $enabled) {
128
                    if (in_array($size, $screen_sizes)) {
129
                        $size = $size == 'xs' ? '' : '-'.$size;
130
                        $body_classes[] = $enabled == true ?
131
                            'layout'.$size.'-footer-fixed' :
132
                            'layout'.$size.'-footer-not-fixed';
133
                    }
134
                }
135
            }
136
        }
137
138
        $body_classes[] = config('adminlte.classes_body', '');
139
140
        // Add classes related to the "classes_body" configuration and return the
141
        // set of configured classes for the body tag.
142
143
        return trim(implode(' ', $body_classes));
144
    }
145
146
    public function getBodyData()
147
    {
148
        $body_data = [];
149
150
        // Add data related to the "sidebar_scrollbar_theme" configuration.
151
152
        $sb_theme_cfg = config('adminlte.sidebar_scrollbar_theme', 'os-theme-light');
153
154
        if ($sb_theme_cfg != 'os-theme-light') {
155
            $body_data[] = 'data-scrollbar-theme='.$sb_theme_cfg;
156
        }
157
158
        // Add data related to the "sidebar_scrollbar_auto_hide" configuration.
159
160
        $sb_auto_hide = config('adminlte.sidebar_scrollbar_auto_hide', 'l');
161
162
        if ($sb_auto_hide != 'l') {
163
            $body_data[] = 'data-scrollbar-auto-hide='.$sb_auto_hide;
164
        }
165
166
        return trim(implode(' ', $body_data));
167
    }
168
169 1
    protected function buildMenu()
170
    {
171 1
        $builder = new Builder($this->buildFilters());
172
173 1
        $this->events->dispatch(new BuildingMenu($builder));
174
175 1
        return $builder->menu;
176
    }
177
178 1
    protected function buildFilters()
179
    {
180 1
        return array_map([$this->container, 'make'], $this->filters);
181
    }
182
183
    /**
184
     * Filter method for sidebar menu items.
185
     */
186
    private function sidebarFilter($item)
187
    {
188
        if (isset($item['topnav']) && $item['topnav']) {
189
            return false;
190
        }
191
192
        if (isset($item['topnav_right']) && $item['topnav_right']) {
193
            return false;
194
        }
195
196
        if (isset($item['topnav_user']) && $item['topnav_user']) {
197
            return false;
198
        }
199
200
        return true;
201
    }
202
203
    /**
204
     * Filter method for navbar top left menu items.
205
     */
206
    private function navbarLeftFilter($item)
207
    {
208
        if (isset($item['topnav_right']) && $item['topnav_right']) {
209
            return false;
210
        }
211
212
        if (isset($item['topnav_user']) && $item['topnav_user']) {
213
            return false;
214
        }
215
216
        if (config('adminlte.layout_topnav') || (isset($item['topnav']) && $item['topnav'])) {
217
            return is_array($item) && ! isset($item['header']);
218
        }
219
220
        return false;
221
    }
222
223
    /**
224
     * Filter method for navbar top right menu items.
225
     */
226
    private function navbarRightFilter($item)
227
    {
228
        if (isset($item['topnav_right']) && $item['topnav_right']) {
229
            return true;
230
        }
231
232
        return false;
233
    }
234
235
    /**
236
     * Filter method for navbar dropdown user menu items.
237
     */
238
    private function navbarUserMenuFilter($item)
239
    {
240
        if (isset($item['topnav_user']) && $item['topnav_user']) {
241
            return true;
242
        }
243
244
        return false;
245
    }
246
}
247