Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

UserManagementMenuAdaptor::adaptChildren()   F

Complexity

Conditions 19
Paths 25

Size

Total Lines 123

Duplication

Lines 38
Ratio 30.89 %

Code Coverage

Tests 0
CRAP Score 380

Importance

Changes 0
Metric Value
dl 38
loc 123
ccs 0
cts 116
cp 0
rs 3.6133
c 0
b 0
f 0
cc 19
nc 25
nop 4
crap 380

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kunstmaan\UserManagementBundle\Helper\Menu;
4
5
use Kunstmaan\AdminBundle\Helper\Menu\MenuAdaptorInterface;
6
use Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder;
7
use Kunstmaan\AdminBundle\Helper\Menu\MenuItem;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
10
11
class UserManagementMenuAdaptor implements MenuAdaptorInterface
12
{
13
    /**
14
     * @var AuthorizationCheckerInterface
15
     */
16
    protected $authorizationChecker;
17
18
    /**
19
     * @param AuthorizationCheckerInterface $authorizationChecker
20
     */
21
    public function __construct(AuthorizationCheckerInterface $authorizationChecker)
22
    {
23
        $this->authorizationChecker = $authorizationChecker;
24
    }
25
26
    /**
27
     * In this method you can add children for a specific parent, but also remove and change the already created children
28
     *
29
     * @param MenuBuilder $menu      The MenuBuilder
30
     * @param MenuItem[]  &$children The current children
31
     * @param MenuItem    $parent    The parent Menu item
0 ignored issues
show
Documentation introduced by
Should the type for parameter $parent not be null|MenuItem?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
32
     * @param Request     $request   The Request
0 ignored issues
show
Documentation introduced by
Should the type for parameter $request not be null|Request?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
33
     */
34
    public function adaptChildren(MenuBuilder $menu, array &$children, MenuItem $parent = null, Request $request = null)
35
    {
36
        if (\is_null($parent)) {
37
            return;
38
        }
39
40
        if ('KunstmaanAdminBundle_settings' == $parent->getRoute()) {
41 View Code Duplication
            if ($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
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...
42
                $menuItem = new MenuItem($menu);
43
                $menuItem
44
                    ->setRoute('KunstmaanUserManagementBundle_settings_users')
45
                    ->setUniqueId('Users')
46
                    ->setLabel('settings.users')
47
                    ->setParent($parent);
48
                if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
49
                    $menuItem->setActive(true);
50
                    $parent->setActive(true);
51
                }
52
                $children[] = $menuItem;
53
54
                $menuItem = new MenuItem($menu);
55
                $menuItem
56
                    ->setRoute('KunstmaanUserManagementBundle_settings_groups')
57
                    ->setUniqueId('Groups')
58
                    ->setLabel('settings.groups')
59
                    ->setParent($parent);
60
61
                if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
62
                    $menuItem->setActive(true);
63
                    $parent->setActive(true);
64
                }
65
                $children[] = $menuItem;
66
67
                $menuItem = new MenuItem($menu);
68
                $menuItem
69
                    ->setRoute('KunstmaanUserManagementBundle_settings_roles')
70
                    ->setUniqueId('Roles')
71
                    ->setLabel('settings.roles')
72
                    ->setParent($parent);
73
                if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
74
                    $menuItem->setActive(true);
75
                    $parent->setActive(true);
76
                }
77
                $children[] = $menuItem;
78
            }
79
        } elseif ('KunstmaanUserManagementBundle_settings_users' == $parent->getRoute()) {
80
            if ($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
81
                $menuItem = new MenuItem($menu);
82
                $menuItem
83
                    ->setRoute('KunstmaanUserManagementBundle_settings_users_add')
84
                    ->setUniqueId('Add user')
85
                    ->setLabel('Add user')
86
                    ->setParent($parent)
87
                    ->setAppearInNavigation(false);
88
                if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
89
                    $menuItem->setActive(true);
90
                }
91
                $children[] = $menuItem;
92
93
                $menuItem = new MenuItem($menu);
94
                $menuItem
95
                    ->setRoute('KunstmaanUserManagementBundle_settings_users_edit')
96
                    ->setUniqueId('Edit user')
97
                    ->setLabel('Edit user')
98
                    ->setParent($parent)
99
                    ->setAppearInNavigation(false);
100
                if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
101
                    $menuItem->setActive(true);
102
                }
103
                $children[] = $menuItem;
104
            }
105
        } elseif ('KunstmaanUserManagementBundle_settings_groups' == $parent->getRoute()) {
106
            if ($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
107
                $menuItem = new MenuItem($menu);
108
                $menuItem
109
                    ->setRoute('KunstmaanUserManagementBundle_settings_groups_add')
110
                    ->setUniqueId('Add group')
111
                    ->setLabel('Add group')
112
                    ->setParent($parent)
113
                    ->setAppearInNavigation(false);
114
                if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
115
                    $menuItem->setActive(true);
116
                }
117
                $children[] = $menuItem;
118
119
                $menuItem = new MenuItem($menu);
120
                $menuItem
121
                    ->setRoute('KunstmaanUserManagementBundle_settings_groups_edit')
122
                    ->setUniqueId('Edit group')
123
                    ->setLabel('Edit group')
124
                    ->setParent($parent)
125
                    ->setAppearInNavigation(false);
126
                if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
127
                    $menuItem->setActive(true);
128
                }
129
                $children[] = $menuItem;
130
            }
131
        } elseif (('KunstmaanUserManagementBundle_settings_roles' == $parent->getRoute()) && $this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
132
            $menuItem = new MenuItem($menu);
133
            $menuItem
134
                ->setRoute('KunstmaanUserManagementBundle_settings_roles_add')
135
                ->setUniqueId('Add role')
136
                ->setLabel('Add role')
137
                ->setParent($parent)
138
                ->setAppearInNavigation(false);
139
            if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
140
                $menuItem->setActive(true);
141
            }
142
            $children[] = $menuItem;
143
144
            $menuItem = new MenuItem($menu);
145
            $menuItem
146
                ->setRoute('KunstmaanUserManagementBundle_settings_roles_edit')
147
                ->setUniqueId('Edit role')
148
                ->setLabel('Edit role')
149
                ->setParent($parent)
150
                ->setAppearInNavigation(false);
151
            if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
152
                $menuItem->setActive(true);
153
            }
154
            $children[] = $menuItem;
155
        }
156
    }
157
}
158