Passed
Push — main ( 0290e9...b434a8 )
by PRATIK
03:39 queued 11s
created

Adminetic::getHeaderData()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
namespace Pratiksh\Adminetic\Services;
4
5
use Exception;
6
use Illuminate\Support\Str;
7
use Pratiksh\Adminetic\Traits\SidebarHelper;
8
9
class Adminetic
10
{
11
    use SidebarHelper;
12
13
    public function user()
14
    {
15
        $user = config('auth.providers.users.model');
16
17
        return new $user;
18
    }
19
20
    public function assets(): array
21
    {
22
        $client_assets = $this->getClientAssets();
23
        $default_assets = $this->getDefaultAssets();
24
        $allAssets = array_merge($default_assets, $client_assets);
25
26
        $assets = array_unique($allAssets, SORT_REGULAR);
27
28
        return $assets;
29
    }
30
31
    public function menus(): array
32
    {
33
        $client_menus = $this->clientMenus();
34
        $adminetic_menus = $this->admineticMenus();
35
        $plugin_menus = $this->pluginMenus();
36
        $allmenus = array_merge($adminetic_menus, $client_menus, $plugin_menus);
37
38
        return $allmenus;
39
    }
40
41
    public function getClientAssets(): array
42
    {
43
        return config('adminetic.assets', []);
44
    }
45
46
    public function getDefaultAssets(): array
47
    {
48
        return [
49
            [
50
                'name' => 'Datatables',
51
                'active' => true,
52
                'files' => [
53
                    [
54
                        'type' => 'css',
55
                        'active' => true,
56
                        'location' => 'adminetic/assets/css/vendors/datatables.css',
57
                    ],
58
                    [
59
                        'type' => 'js',
60
                        'active' => true,
61
                        'location' => 'adminetic/assets/js/datatable/datatables/jquery.dataTables.min.js',
62
                    ],
63
                    [
64
                        'type' => 'js',
65
                        'active' => true,
66
                        'location' => 'adminetic/assets/js/datatable/datatable-extension/dataTables.buttons.min.js',
67
                    ],
68
                    [
69
                        'type' => 'js',
70
                        'active' => true,
71
                        'location' => 'adminetic/assets/js/datatable/datatable-extension/buttons.flash.min.js',
72
                    ],
73
                    [
74
                        'type' => 'js',
75
                        'active' => true,
76
                        'location' => 'adminetic/assets/js/datatable/datatable-extension/jszip.min.js',
77
                    ],
78
                    [
79
                        'type' => 'js',
80
                        'active' => true,
81
                        'location' => 'adminetic/assets/js/datatable/datatable-extension/pdfmake.min.js',
82
                    ],
83
                    [
84
                        'type' => 'js',
85
                        'active' => true,
86
                        'location' => 'adminetic/assets/js/datatable/datatable-extension/vfs_fonts.js',
87
                    ],
88
                    [
89
                        'type' => 'js',
90
                        'active' => true,
91
                        'location' => 'adminetic/assets/js/datatable/datatable-extension/buttons.html5.min.js',
92
                    ],
93
                    [
94
                        'type' => 'js',
95
                        'active' => true,
96
                        'location' => 'adminetic/assets/js/datatable/datatable-extension/buttons.print.min.js',
97
                    ],
98
                ],
99
            ],
100
            [
101
                'name' => 'Icons',
102
                'active' => true,
103
                'files' => [
104
                    [
105
                        'type' => 'css',
106
                        'active' => true,
107
                        'location' => 'adminetic/assets/css/font-awesome.css',
108
                    ],
109
                    [
110
                        'type' => 'css',
111
                        'active' => true,
112
                        'location' => 'adminetic/assets/css/vendors/icofont.css',
113
                    ],
114
                    [
115
                        'type' => 'css',
116
                        'active' => true,
117
                        'location' => 'adminetic/assets/css/vendors/themify.css',
118
                    ],
119
                    [
120
                        'type' => 'css',
121
                        'active' => true,
122
                        'location' => 'adminetic/assets/css/vendors/flag-icon.css',
123
                    ],
124
                    [
125
                        'type' => 'css',
126
                        'active' => true,
127
                        'location' => 'adminetic/assets/css/vendors/feather-icon.css',
128
                    ],
129
                ],
130
            ],
131
            [
132
                'name' => 'Scrollbar',
133
                'active' => true,
134
                'files' => [
135
                    [
136
                        'type' => 'css',
137
                        'active' => true,
138
                        'location' => 'adminetic/assets/css/vendors/scrollbar.css',
139
                    ],
140
                    [
141
                        'type' => 'js',
142
                        'active' => true,
143
                        'location' => 'adminetic/assets/js/scrollbar/simplebar.js',
144
                    ],
145
                    [
146
                        'type' => 'js',
147
                        'active' => true,
148
                        'location' => 'adminetic/assets/js/scrollbar/custom.js',
149
                    ],
150
                ],
151
            ],
152
            [
153
                'name' => 'Touchspin',
154
                'active' => true,
155
                'files' => [
156
                    [
157
                        'type' => 'js',
158
                        'active' => true,
159
                        'location' => 'adminetic/assets/js/touchspin/touchspin.js',
160
                    ],
161
                    [
162
                        'type' => 'js',
163
                        'active' => true,
164
                        'location' => 'adminetic/assets/js/touchspin/input-groups.min.js',
165
                    ],
166
                ],
167
            ],
168
            [
169
                'name' => 'Datepicker',
170
                'active' => true,
171
                'files' => [
172
                    [
173
                        'type' => 'css',
174
                        'active' => true,
175
                        'location' => 'adminetic/assets/css/vendors/date-picker.css',
176
                    ],
177
                    [
178
                        'type' => 'js',
179
                        'active' => true,
180
                        'location' => 'adminetic/assets/js/datepicker/date-picker/datepicker.js',
181
                    ],
182
                    [
183
                        'type' => 'js',
184
                        'active' => true,
185
                        'location' => 'adminetic/assets/js/datepicker/date-picker/datepicker.en.js',
186
                    ],
187
                ],
188
            ],
189
            [
190
                'name' => 'CKEditor',
191
                'active' => true,
192
                'files' => [
193
                    [
194
                        'type' => 'js',
195
                        'active' => true,
196
                        'location' => 'adminetic/assets/js/editor/ckeditor/ckeditor.js',
197
                    ],
198
                    [
199
                        'type' => 'js',
200
                        'active' => true,
201
                        'location' => 'adminetic/assets/js/editor/ckeditor/styles.js',
202
                    ],
203
                ],
204
            ],
205
            [
206
                'name' => 'Summernote',
207
                'active' => true,
208
                'files' => [
209
                    [
210
                        'type' => 'css',
211
                        'active' => true,
212
                        'location' => 'adminetic/assets/css/vendors/summernote.css',
213
                    ],
214
                    [
215
                        'type' => 'js',
216
                        'active' => true,
217
                        'location' => 'adminetic/assets/js/editor/summernote/summernote.js',
218
                    ],
219
                ],
220
            ],
221
            [
222
                'name' => 'Select2',
223
                'active' => true,
224
                'files' => [
225
                    [
226
                        'type' => 'css',
227
                        'active' => true,
228
                        'location' => 'adminetic/assets/css/vendors/select2.css',
229
                    ],
230
                    [
231
                        'type' => 'js',
232
                        'active' => true,
233
                        'location' => 'adminetic/assets/js/select2/select2.full.min.js',
234
                    ],
235
                ],
236
            ],
237
            [
238
                'name' => 'ACE Editor',
239
                'active' => false,
240
                'files' => [
241
                    [
242
                        'type' => 'js',
243
                        'active' => true,
244
                        'location' => 'adminetic/assets/js/editor/ace-editor/ace.js',
245
                    ],
246
                    [
247
                        'type' => 'js',
248
                        'active' => true,
249
                        'location' => 'adminetic/assets/js/editor/ace-editor/mode-html.js',
250
                    ],
251
                ],
252
            ],
253
            [
254
                'name' => 'Notify',
255
                'active' => true,
256
                'files' => [
257
                    [
258
                        'type' => 'js',
259
                        'active' => true,
260
                        'location' => 'adminetic/assets/js/notify/bootstrap-notify.min.js',
261
                    ],
262
                ],
263
            ],
264
            [
265
                'name' => 'Card',
266
                'active' => true,
267
                'files' => [
268
                    [
269
                        'type' => 'js',
270
                        'active' => true,
271
                        'location' => 'adminetic/assets/js/custom-card/custom-card.js',
272
                    ],
273
                ],
274
            ],
275
        ];
276
    }
277
278
    public function getPluginAssets(): array
279
    {
280
        $pluginAssets = [];
281
        if (count($this->getAdapters()) > 0) {
282
            foreach ($this->getAdapters() as $adapter) {
283
                $pluginAssets = array_merge($pluginAssets, $adapter->assets());
284
            }
285
        }
286
287
        return $pluginAssets;
288
    }
289
290
    public function admineticMenus(): array
291
    {
292
        return [
293
            [
294
                'type' => 'breaker',
295
                'name' => 'General',
296
                'description' => 'Administration Control',
297
            ],
298
            [
299
                'type' => 'link',
300
                'name' => 'Dashboard',
301
                'icon' => 'fa fa-home',
302
                'link' => route('home'),
303
                'is_active' => request()->routeIs('home') ? 'active' : '',
304
                'conditions' => [
305
                    [
306
                        'type' => 'and',
307
                        'condition' => auth()->user()->hasRole('admin'),
308
                    ],
309
                ],
310
            ],
311
            [
312
                'type' => 'menu',
313
                'name' => 'User Management',
314
                'icon' => 'fa fa-users',
315
                'is_active' => request()->routeIs('user*') ? 'active' : '',
316
                'pill' => [
317
                    'class' => 'badge badge-info badge-air-info',
318
                    'value' => \App\Models\User::count(),
0 ignored issues
show
Bug introduced by
The type App\Models\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
319
                ],
320
                'conditions' => [
321
                    [
322
                        'type' => 'or',
323
                        'condition' => auth()->user()->can('view-any', \App\Models\User::class),
324
                    ],
325
                    [
326
                        'type' => 'or',
327
                        'condition' => auth()->user()->can('create', \App\Models\User::class),
328
                    ],
329
                ],
330
                'children' => $this->indexCreateChildren('user', \App\Models\User::class),
331
            ],
332
            [
333
                'type' => 'menu',
334
                'name' => 'Role',
335
                'icon' => 'fa fa-black-tie',
336
                'is_active' => request()->routeIs('role*') ? 'active' : '',
337
                'conditions' => [
338
                    [
339
                        'type' => 'or',
340
                        'condition' => auth()->user()->can('view-any', \Pratiksh\Adminetic\Models\Admin\Role::class),
341
                    ],
342
                    [
343
                        'type' => 'or',
344
                        'condition' => auth()->user()->can('create', \Pratiksh\Adminetic\Models\Admin\Role::class),
345
                    ],
346
                ],
347
                'children' => $this->indexCreateChildren('role', \Pratiksh\Adminetic\Models\Admin\Role::class),
348
            ],
349
            [
350
                'type' => 'menu',
351
                'name' => 'Permission',
352
                'icon' => 'fa fa-check',
353
                'is_active' => request()->routeIs('permission*') ? 'active' : '',
354
                'conditions' => [
355
                    [
356
                        'type' => 'or',
357
                        'condition' => auth()->user()->can('view-any', \Pratiksh\Adminetic\Models\Admin\Permission::class),
358
                    ],
359
                    [
360
                        'type' => 'or',
361
                        'condition' => auth()->user()->can('create', \Pratiksh\Adminetic\Models\Admin\Permission::class),
362
                    ],
363
                ],
364
                'children' => $this->indexCreateChildren('permission', \Pratiksh\Adminetic\Models\Admin\Permission::class),
365
            ],
366
            [
367
                'type' => 'menu',
368
                'name' => 'Setting',
369
                'icon' => 'fa fa-cog',
370
                'is_active' => request()->routeIs('setting*') ? 'active' : '',
371
                'conditions' => [
372
                    [
373
                        'type' => 'or',
374
                        'condition' => auth()->user()->can('view-any', \Pratiksh\Adminetic\Models\Admin\Setting::class),
375
                    ],
376
                    [
377
                        'type' => 'or',
378
                        'condition' => auth()->user()->can('create', \Pratiksh\Adminetic\Models\Admin\Setting::class),
379
                    ],
380
                ],
381
                'children' => $this->indexCreateChildren('setting', \Pratiksh\Adminetic\Models\Admin\Setting::class),
382
            ],
383
            [
384
                'type' => 'menu',
385
                'name' => 'Preference',
386
                'icon' => 'fa fa-wrench',
387
                'is_active' => request()->routeIs('preference*') ? 'active' : '',
388
                'conditions' => [
389
                    [
390
                        'type' => 'or',
391
                        'condition' => auth()->user()->can('view-any', \Pratiksh\Adminetic\Models\Admin\Preference::class),
392
                    ],
393
                    [
394
                        'type' => 'or',
395
                        'condition' => auth()->user()->can('create', \Pratiksh\Adminetic\Models\Admin\Preference::class),
396
                    ],
397
                ],
398
                'children' => $this->indexCreateChildren('preference', \Pratiksh\Adminetic\Models\Admin\Preference::class),
399
            ],
400
            [
401
                'type' => 'link',
402
                'name' => 'Activities',
403
                'icon' => 'fa fa-book',
404
                'is_active' => request()->routeIs('activity*') ? 'active' : '',
405
                'link' => adminRedirectRoute('activity'),
406
                'conditions' => [
407
                    [
408
                        'type' => 'and',
409
                        'condition' => auth()->user()->hasRole('admin'),
410
                    ],
411
                ],
412
            ],
413
        ];
414
    }
415
416
    public function clientMenus(): array
417
    {
418
        $myMenu = config('adminetic.myMenu', \App\Services\MyMenu::class);
0 ignored issues
show
Bug introduced by
The type App\Services\MyMenu was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
419
        if (class_exists($myMenu)) {
420
            if (method_exists($myMenu, 'myMenu')) {
421
                $menu = new $myMenu;
422
                if (is_array($menu->myMenu())) {
423
                    return $menu->myMenu();
424
                } else {
425
                    throw new Exception('myMenu method return type must be an array.');
426
                }
427
            } else {
428
                throw new Exception('myMenu method is not found', 1);
429
            }
430
        } else {
431
            throw new Exception('Given class namespace is not found');
432
        }
433
    }
434
435
    public function pluginMenus(): array
436
    {
437
        $pluginMenu = [];
438
        if (count($this->getAdapters()) > 0) {
439
            foreach ($this->getAdapters() as $adapter) {
440
                $pluginMenu = array_merge($pluginMenu, $adapter->myMenu());
441
            }
442
        }
443
        return $pluginMenu;
444
    }
445
446
    public function getHeaderData(): array
447
    {
448
        $headerData = [];
449
        if (count($this->getAdapters()) > 0) {
450
            foreach ($this->getAdapters() as $adapter) {
451
                if (method_exists($adapter, 'headerData')) {
452
                    $headerData = array_merge($headerData, $adapter->headerData());
453
                }
454
            }
455
        }
456
        return $headerData;
457
    }
458
459
    public function getFooterData(): array
460
    {
461
        $footerData = [];
462
        if (count($this->getAdapters()) > 0) {
463
            foreach ($this->getAdapters() as $adapter) {
464
                if (method_exists($adapter, 'footerData')) {
465
                    $footerData = array_merge($footerData, $adapter->footerData());
466
                }
467
            }
468
        }
469
        return $footerData;
470
    }
471
472
    public function getAdapters(): array
473
    {
474
        $adapters = [];
475
        foreach (config('adminetic.adapters', []) as $adapter) {
476
            if (class_exists($adapter)) {
477
                $init_adapter = new $adapter;
478
                $adapters[] = $init_adapter;
479
            }
480
        }
481
482
        return $adapters;
483
    }
484
485
    private function indexCreateChildren($route, $class)
486
    {
487
        $name = Str::ucfirst($route);
488
        $plural = Str::plural($name);
489
490
        $children = [
491
            [
492
                'type' => 'submenu',
493
                'name' => 'All ' . $plural,
494
                'is_active' => request()->routeIs($route . '.index') ? 'active' : '',
495
                'link' => adminRedirectRoute($route),
496
                'conditions' => [
497
                    [
498
                        'type' => 'or',
499
                        'condition' => auth()->user()->can('view-any', $class),
500
                    ],
501
                ],
502
            ],
503
            [
504
                'type' => 'submenu',
505
                'name' => 'Create ' . $route,
506
                'is_active' => request()->routeIs($route . '.create') ? 'active' : '',
507
                'link' => adminCreateRoute($route),
508
                'conditions' => [
509
                    [
510
                        'type' => 'or',
511
                        'condition' => auth()->user()->can('create', $class),
512
                    ],
513
                ],
514
            ],
515
        ];
516
517
        return $children;
518
    }
519
}
520