MenuService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Hechoenlaravel\JarvisFoundation\Menu;
4
5
6
/**
7
 * Class MenuService
8
 * @package Hechoenlaravel\JarvisFoundation\Menu
9
 */
10
class MenuService
11
{
12
13
    /**
14
     * @var array|\Illuminate\Support\Collection
15
     */
16
    public $groups = [];
17
18
    /**
19
     * MenuService constructor.
20
     */
21
    public function __construct()
22
    {
23
        $this->groups = collect();
24
    }
25
26
    /**
27
     * @param $class
28
     */
29
    public function addMenuDefinition($class)
30
    {
31
        if(class_exists($class)) {
32
            $this->groups->push($class);
33
        }
34
    }
35
36
    /**
37
     * @param string $group
38
     * @return mixed
39
     */
40
    public function render($group = 'sidebar')
41
    {
42
        $this->groups->each(function($class){
43
            $definition = app($class);
44
            $menu = app('menu')->instance($definition->getInstance());
45
            if($definition->isDropdown()) {
46 View Code Duplication
                $menu->dropdown($definition->getName(), function($sub) use ($definition) {
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...
47
                    $definition->items->each(function($item) use ($sub) {
48
                        if($item['type'] == 'route') {
49
                            $sub->route($item['route'], $item['name'], [], 0, ['active' => $item['active-state']])
50
                                ->hideWhen($item['ability']);
51
                        }
52
                        if($item['type'] == 'header') {
53
                            $sub->addHeader($item['name']);
54
                        }
55
                        if($item['type'] == 'url') {
56
                            $sub->url($item['url'], $item['name'], 0, ['active' => $item['active-state']])
57
                                ->hideWhen($item['ability']);;
58
                        }
59
                    });
60
                });
61
            } else {
62 View Code Duplication
                $definition->items->each(function($item) use ($menu) {
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...
63
                    if($item['type'] == 'route') {
64
                        $menu->route($item['route'], $item['name'], [], 0, ['active' => $item['active-state']])
65
                            ->hideWhen($item['ability']);
66
                    }
67
                    if($item['type'] == 'header') {
68
                        $menu->addHeader($item['name']);
69
                    }
70
                    if($item['type'] == 'url') {
71
                        $menu->url($item['url'], $item['name'], 0, ['active' => $item['active-state']])
72
                            ->hideWhen($item['ability']);;
73
                    }
74
                });
75
            }
76
        });
77
        return app('menu')->render($group);
78
    }
79
80
}