Completed
Push — master ( 049419...84a893 )
by Florian
05:28 queued 02:33
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 19.59%

Importance

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

10 Methods

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