MenuService   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 71
Duplicated Lines 39.44 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 28
loc 71
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addMenuDefinition() 0 6 2
C render() 28 39 8

How to fix   Duplicated Code   

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:

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
}