UserManagementMenuAdaptor::adaptChildren()   F
last analyzed

Complexity

Conditions 19
Paths 26

Size

Total Lines 129

Duplication

Lines 119
Ratio 92.25 %

Importance

Changes 0
Metric Value
dl 119
loc 129
rs 3.6133
c 0
b 0
f 0
cc 19
nc 26
nop 4

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 Wim Vandersmissen
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 Wim Vandersmissen
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 View Code Duplication
        } elseif ('KunstmaanAdminBundle_settings' == $parent->getRoute()) {
0 ignored issues
show
Duplication introduced by Jeroen Thora
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...
39
            if ($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
40
                $menuItem = new MenuItem($menu);
41
                $menuItem
42
                    ->setRoute('KunstmaanUserManagementBundle_settings_users')
43
                    ->setUniqueId('Users')
44
                    ->setLabel('settings.users')
45
                    ->setParent($parent);
46
                if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
47
                    $menuItem->setActive(true);
48
                    $parent->setActive(true);
49
                }
50
                $children[] = $menuItem;
51
52
                $menuItem = new MenuItem($menu);
53
                $menuItem
54
                    ->setRoute('KunstmaanUserManagementBundle_settings_groups')
55
                    ->setUniqueId('Groups')
56
                    ->setLabel('settings.groups')
57
                    ->setParent($parent);
58
59
                if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
60
                    $menuItem->setActive(true);
61
                    $parent->setActive(true);
62
                }
63
                $children[] = $menuItem;
64
65
                $menuItem = new MenuItem($menu);
66
                $menuItem
67
                    ->setRoute('KunstmaanUserManagementBundle_settings_roles')
68
                    ->setUniqueId('Roles')
69
                    ->setLabel('settings.roles')
70
                    ->setParent($parent);
71
                if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
72
                    $menuItem->setActive(true);
73
                    $parent->setActive(true);
74
                }
75
                $children[] = $menuItem;
76
            }
77
        } else {
78
            if ('KunstmaanUserManagementBundle_settings_users' == $parent->getRoute()) {
79 View Code Duplication
                if ($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
0 ignored issues
show
Duplication introduced by Kristof Jochmans
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...
80
                    $menuItem = new MenuItem($menu);
81
                    $menuItem
82
                        ->setRoute('KunstmaanUserManagementBundle_settings_users_add')
83
                        ->setUniqueId('Add user')
84
                        ->setLabel('Add user')
85
                        ->setParent($parent)
86
                        ->setAppearInNavigation(false);
87
                    if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
88
                        $menuItem->setActive(true);
89
                    }
90
                    $children[] = $menuItem;
91
92
                    $menuItem = new MenuItem($menu);
93
                    $menuItem
94
                        ->setRoute('KunstmaanUserManagementBundle_settings_users_edit')
95
                        ->setUniqueId('Edit user')
96
                        ->setLabel('Edit user')
97
                        ->setParent($parent)
98
                        ->setAppearInNavigation(false);
99
                    if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
100
                        $menuItem->setActive(true);
101
                    }
102
                    $children[] = $menuItem;
103
                }
104
            } else {
105
                if ('KunstmaanUserManagementBundle_settings_groups' == $parent->getRoute()) {
106 View Code Duplication
                    if ($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
0 ignored issues
show
Duplication introduced by Kristof Jochmans
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...
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 View Code Duplication
                } else {
0 ignored issues
show
Duplication introduced by Wim Vandersmissen
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...
132
                    if ('KunstmaanUserManagementBundle_settings_roles' == $parent->getRoute()) {
133
                        if ($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
134
                            $menuItem = new MenuItem($menu);
135
                            $menuItem
136
                                ->setRoute('KunstmaanUserManagementBundle_settings_roles_add')
137
                                ->setUniqueId('Add role')
138
                                ->setLabel('Add role')
139
                                ->setParent($parent)
140
                                ->setAppearInNavigation(false);
141
                            if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
142
                                $menuItem->setActive(true);
143
                            }
144
                            $children[] = $menuItem;
145
146
                            $menuItem = new MenuItem($menu);
147
                            $menuItem
148
                                ->setRoute('KunstmaanUserManagementBundle_settings_roles_edit')
149
                                ->setUniqueId('Edit role')
150
                                ->setLabel('Edit role')
151
                                ->setParent($parent)
152
                                ->setAppearInNavigation(false);
153
                            if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
154
                                $menuItem->setActive(true);
155
                            }
156
                            $children[] = $menuItem;
157
                        }
158
                    }
159
                }
160
            }
161
        }
162
    }
163
}
164