AdmingeneratorMenuBuilder   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 117
Duplicated Lines 18.8 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 22
loc 117
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A addLinkURI() 11 11 2
A addLinkRoute() 11 11 2
A setActive() 0 7 2
A addDropdown() 0 9 1
A isCurrentUri() 0 6 1

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 Admingenerator\GeneratorBundle\Menu;
4
5
use Knp\Menu\FactoryInterface;
6
use Knp\Menu\ItemInterface;
7
use Symfony\Component\HttpFoundation\RequestStack;
8
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
9
10
class AdmingeneratorMenuBuilder
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $dividers = array();
16
17
    /**
18
     * @var string
19
     */
20
    protected $translation_domain = 'Admingenerator';
21
22
    /**
23
     * @var FactoryInterface
24
     */
25
    protected $factory;
26
27
    /**
28
     * @var RequestStack
29
     */
30
    protected $requestStack;
31
32
    /**
33
     * @var string
34
     */
35
    protected $dashboardRoute;
36
37
    public function __construct(FactoryInterface $factory, RequestStack $requestStack, $dashboardRoute)
38
    {
39
        $this->factory = $factory;
40
        $this->requestStack = $requestStack;
41
        $this->dashboardRoute = $dashboardRoute;
42
    }
43
44
    /**
45
     * Creates link to uri element and adds it to menu
46
     * 
47
     * @param \Knp\Menu\ItemInterface $menu
48
     * @param string $label Link label
49
     * @param string $uri
50
     * @return ItemInterface Link element
51
     */
52 View Code Duplication
    protected function addLinkURI(ItemInterface $menu, $label, $uri)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
53
    {
54
        $item = $menu->addChild($label, array('uri' => $uri));
55
        $item->setExtra('translation_domain', $this->translation_domain);
56
57
        if ($this->isCurrentUri($item->getUri())) {
58
            $item->setAttribute('class', 'active');
59
        }
60
61
        return $item;
62
    }
63
64
    /**
65
     * Creates link to route element and adds it to menu
66
     * 
67
     * @param \Knp\Menu\ItemInterface $menu
68
     * @param string $label Link label
69
     * @param string $route Link route
70
     * @param array $routeParameters Route parameters
71
     * @return ItemInterface Link element
72
     */
73 View Code Duplication
    protected function addLinkRoute(ItemInterface $menu, $label, $route, $routeParameters = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
74
    {
75
        $item = $menu->addChild($label, array('route' => $route, 'routeParameters' => $routeParameters, 'routeAbsolute' => UrlGeneratorInterface::ABSOLUTE_PATH));
76
        $item->setExtra('translation_domain', $this->translation_domain);
77
78
        if ($this->isCurrentUri($item->getUri())) {
79
            $this->setActive($item);
80
        }
81
82
        return $item;
83
    }
84
85
    /**
86
     * Set active class to current item and all its parents (so it is automatically opened)
87
     * 
88
     * @param ItemInterface $item
89
     */
90
    protected function setActive(ItemInterface $item = null)
91
    {
92
        if ($item) {
93
            $this->setActive($item->getParent());
94
            $item->setAttribute('class', $item->getAttribute('class', '') . ' active');
95
        }
96
    }
97
98
    /**
99
     * Creates dropdown menu element and adds it to menu
100
     * 
101
     * @param \Knp\Menu\ItemInterface $menu
102
     * @param string $label Dropdown label
103
     * @param bool $caret Wheather or not append caret
104
     * @return ItemInterface Dropdown element
105
     */
106
    protected function addDropdown(ItemInterface $menu, $label, $caret = true)
107
    {
108
        $item = $this->addLinkURI($menu, $label, '#');
109
        $item->setChildrenAttributes(array('class' => 'treeview-menu'));
110
        $item->setAttributes(array('class' => 'treeview'));
111
        $item->setExtra('caret', $caret);
112
113
        return $item;
114
    }
115
116
    /**
117
     * @param string $uri
118
     * @return bool
119
     */
120
    protected function isCurrentUri($uri)
121
    {
122
        $request = $this->requestStack->getCurrentRequest();
123
124
        return $request->getBaseUrl().$request->getPathInfo() === $uri;
125
    }
126
}
127