Completed
Pull Request — master (#598)
by
unknown
03:23
created

AdminLte   F

Complexity

Total Complexity 62

Size/Duplication

Total Lines 246
Duplicated Lines 9.76 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 62
lcom 1
cbo 3
dl 24
loc 246
ccs 97
cts 97
cp 1
rs 3.44
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A buildMenu() 0 8 1
A buildFilters() 0 4 1
B menu() 0 20 6
F getBodyClasses() 24 97 28
A getBodyData() 0 22 3
B sidebarFilter() 0 16 7
B navbarLeftFilter() 0 16 9
A navbarRightFilter() 0 8 3
A navbarUserMenuFilter() 0 8 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like AdminLte often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AdminLte, and based on these observations, apply Extract Interface, too.

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 7
    public function __construct(
22
        array $filters,
23
        Dispatcher $events,
24
        Container $container
25
    ) {
26 7
        $this->filters = $filters;
27 7
        $this->events = $events;
28 7
        $this->container = $container;
29 7
    }
30
31 5
    public function menu($filterOpt = null)
32
    {
33 5
        if (! $this->menu) {
34 5
            $this->menu = $this->buildMenu();
35
        }
36
37
        // Check for filter option.
38
39 5
        if ($filterOpt == 'sidebar') {
40 1
            return array_filter($this->menu, [$this, 'sidebarFilter']);
41 4
        } elseif ($filterOpt == 'navbar-left') {
42 1
            return array_filter($this->menu, [$this, 'navbarLeftFilter']);
43 3
        } elseif ($filterOpt == 'navbar-right') {
44 1
            return array_filter($this->menu, [$this, 'navbarRightFilter']);
45 2
        } elseif ($filterOpt == 'navbar-user') {
46 1
            return array_filter($this->menu, [$this, 'navbarUserMenuFilter']);
47
        } else {
48 1
            return $this->menu;
49
        }
50
    }
51
52
    /**
53
     * Gets the body classes, in relation to the config options.
54
     */
55 1
    public function getBodyClasses()
56
    {
57 1
        $body_classes = [];
58 1
        $screen_sizes = ['xs', 'sm', 'md', 'lg', 'xl'];
59
60
        // Add classes related to the "sidebar_mini" configuration.
61
62 1
        if (config('adminlte.sidebar_mini', true) === true) {
63 1
            $body_classes[] = 'sidebar-mini';
64 1
        } elseif (config('adminlte.sidebar_mini', true) == 'md') {
65 1
            $body_classes[] = 'sidebar-mini sidebar-mini-md';
66
        }
67
68
        // Add classes related to the "layout_topnav" configuration.
69
70 1
        if (config('adminlte.layout_topnav') || View::getSection('layout_topnav')) {
71 1
            $body_classes[] = 'layout-top-nav';
72
        }
73
74
        // Add classes related to the "layout_boxed" configuration.
75
76 1
        if (config('adminlte.layout_boxed') || View::getSection('layout_boxed')) {
77 1
            $body_classes[] = 'layout-boxed';
78
        }
79
80
        // Add classes related to the "sidebar_collapse" configuration.
81
82 1
        if (config('adminlte.sidebar_collapse') || View::getSection('sidebar_collapse')) {
83 1
            $body_classes[] = 'sidebar-collapse';
84
        }
85
86
        // Add classes related to the "right_sidebar" configuration.
87
88 1
        if (config('adminlte.right_sidebar') && config('adminlte.right_sidebar_push')) {
89 1
            $body_classes[] = 'control-sidebar-push';
90
        }
91
92
        // Add classes related to fixed sidebar, these are not compatible with
93
        // "layout_topnav".
94
95 1
        if (! config('adminlte.layout_topnav') && ! View::getSection('layout_topnav')) {
96
97
            // Check for fixed sidebar configuration.
98
99 1
            if (config('adminlte.layout_fixed_sidebar')) {
100 1
                $body_classes[] = 'layout-fixed';
101
            }
102
        }
103
104
        // Add classes related to fixed footer and navbar, these are not
105
        // compatible with "layout_boxed".
106
107 1
        if (! config('adminlte.layout_boxed') && ! View::getSection('layout_boxed')) {
108
109
            // Check for fixed navbar configuration.
110
111 1
            $fixed_navbar_cfg = config('adminlte.layout_fixed_navbar');
112
113 1 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...
114 1
                $body_classes[] = 'layout-navbar-fixed';
115 1
            } elseif (is_array($fixed_navbar_cfg)) {
116 1
                foreach ($fixed_navbar_cfg as $size => $enabled) {
117 1
                    if (in_array($size, $screen_sizes)) {
118 1
                        $size = $size == 'xs' ? '' : '-'.$size;
119 1
                        $body_classes[] = $enabled == true ?
120 1
                            'layout'.$size.'-navbar-fixed' :
121 1
                            'layout'.$size.'-navbar-not-fixed';
122
                    }
123
                }
124
            }
125
126
            // Check for fixed footer configuration.
127
128 1
            $fixed_footer_cfg = config('adminlte.layout_fixed_footer');
129
130 1 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...
131 1
                $body_classes[] = 'layout-footer-fixed';
132 1
            } elseif (is_array($fixed_footer_cfg)) {
133 1
                foreach ($fixed_footer_cfg as $size => $enabled) {
134 1
                    if (in_array($size, $screen_sizes)) {
135 1
                        $size = $size == 'xs' ? '' : '-'.$size;
136 1
                        $body_classes[] = $enabled == true ?
137 1
                            'layout'.$size.'-footer-fixed' :
138 1
                            'layout'.$size.'-footer-not-fixed';
139
                    }
140
                }
141
            }
142
        }
143
144
        // Add custom classes, related to the "classes_body" configuration.
145
146 1
        $body_classes[] = config('adminlte.classes_body', '');
147
148
        // Return the set of configured classes for the body tag.
149
150 1
        return trim(implode(' ', $body_classes));
151
    }
152
153
    /**
154
     * Gets the body data attributes, in relation to the config options.
155
     */
156 1
    public function getBodyData()
157
    {
158 1
        $body_data = [];
159
160
        // Add data related to the "sidebar_scrollbar_theme" configuration.
161
162 1
        $sb_theme_cfg = config('adminlte.sidebar_scrollbar_theme', 'os-theme-light');
163
164 1
        if ($sb_theme_cfg != 'os-theme-light') {
165 1
            $body_data[] = 'data-scrollbar-theme='.$sb_theme_cfg;
166
        }
167
168
        // Add data related to the "sidebar_scrollbar_auto_hide" configuration.
169
170 1
        $sb_auto_hide = config('adminlte.sidebar_scrollbar_auto_hide', 'l');
171
172 1
        if ($sb_auto_hide != 'l') {
173 1
            $body_data[] = 'data-scrollbar-auto-hide='.$sb_auto_hide;
174
        }
175
176 1
        return trim(implode(' ', $body_data));
177
    }
178
179 5
    protected function buildMenu()
180
    {
181 5
        $builder = new Builder($this->buildFilters());
182
183 5
        $this->events->dispatch(new BuildingMenu($builder));
184
185 5
        return $builder->menu;
186
    }
187
188 5
    protected function buildFilters()
189
    {
190 5
        return array_map([$this->container, 'make'], $this->filters);
191
    }
192
193
    /**
194
     * Filter method for sidebar menu items.
195
     */
196 1
    private function sidebarFilter($item)
197
    {
198 1
        if (isset($item['topnav']) && $item['topnav']) {
199 1
            return false;
200
        }
201
202 1
        if (isset($item['topnav_right']) && $item['topnav_right']) {
203 1
            return false;
204
        }
205
206 1
        if (isset($item['topnav_user']) && $item['topnav_user']) {
207 1
            return false;
208
        }
209
210 1
        return true;
211
    }
212
213
    /**
214
     * Filter method for navbar top left menu items.
215
     */
216 1
    private function navbarLeftFilter($item)
217
    {
218 1
        if (isset($item['topnav_right']) && $item['topnav_right']) {
219 1
            return false;
220
        }
221
222 1
        if (isset($item['topnav_user']) && $item['topnav_user']) {
223 1
            return false;
224
        }
225
226 1
        if (config('adminlte.layout_topnav') || (isset($item['topnav']) && $item['topnav'])) {
227 1
            return is_array($item) && ! isset($item['header']);
228
        }
229
230 1
        return false;
231
    }
232
233
    /**
234
     * Filter method for navbar top right menu items.
235
     */
236 1
    private function navbarRightFilter($item)
237
    {
238 1
        if (isset($item['topnav_right']) && $item['topnav_right']) {
239 1
            return true;
240
        }
241
242 1
        return false;
243
    }
244
245
    /**
246
     * Filter method for navbar dropdown user menu items.
247
     */
248 1
    private function navbarUserMenuFilter($item)
249
    {
250 1
        if (isset($item['topnav_user']) && $item['topnav_user']) {
251 1
            return true;
252
        }
253
254 1
        return false;
255
    }
256
}
257