Builder::adminMenu()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 79
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5.424

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 3
eloc 48
c 5
b 0
f 0
nc 3
nop 1
dl 0
loc 79
ccs 17
cts 48
cp 0.3542
crap 5.424
rs 9.1344

How to fix   Long Method   

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 App\Menu;
4
5
use App\Entity\User;
6
use App\Service\UserServiceProviderResolver;
7
use Knp\Menu\FactoryInterface;
8
use Knp\Menu\ItemInterface;
9
use SchulIT\CommonBundle\DarkMode\DarkModeManagerInterface;
10
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
11
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
12
use Symfony\Contracts\Translation\TranslatorInterface;
13
14
class Builder {
15
    public function __construct(private FactoryInterface $factory, private AuthorizationCheckerInterface $authorizationChecker, private TranslatorInterface $translator, private TokenStorageInterface $tokenStorage, private UserServiceProviderResolver $userServiceProviderResolver, private DarkModeManagerInterface $darkModeManager, private bool $adAuthEnabled)
16
    {
17
    }
18
19
    public function mainMenu(): ItemInterface {
20
        $menu = $this->factory->createItem('root')
21
            ->setChildrenAttribute('class', 'navbar-nav me-auto');
22
23
        $menu->addChild('dashboard.label', [
24
            'route' => 'dashboard'
25
        ])
26 6
            ->setExtra('icon', 'fa fa-home');
27
28
        $menu->addChild('profile.label', [
29
            'route' => 'profile'
30 6
        ])
31 6
            ->setExtra('icon', 'fas fa-user');
32 6
33 6
        if($this->authorizationChecker->isGranted('ROLE_ADMIN') || $this->authorizationChecker->isGranted('ROLE_PASSWORD_MANAGER')) {
34 6
            $menu->addChild('users.label', [
35 6
                'route' => 'users'
36 6
            ])
37 6
                ->setExtra('icon', 'fas fa-users');
38
        }
39 6
40 6
        if($this->authorizationChecker->isGranted('ROLE_ADMIN')) {
41 6
            $menu->addChild('codes.label', [
42
                'route' => 'registration_codes'
43 6
            ])
44 6
                ->setExtra('icon', 'fas fa-qrcode');
45
46 6
            $menu->addChild('privacy_policy.label', [
47
                'route' => 'edit_privacy_policy'
48 6
            ])
49 6
                ->setExtra('icon', 'fas fa-user-shield');
50
        } else {
51 6
            $menu->addChild('privacy_policy.label', [
52
                'route' => 'show_privacy_policy'
53
            ])
54 6
                ->setExtra('icon', 'fas fa-user-shield');
55 6
        }
56 6
57
        return $menu;
58 6
    }
59
60
    public function adminMenu(array $options): ItemInterface {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

60
    public function adminMenu(/** @scrutinizer ignore-unused */ array $options): ItemInterface {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61 6
        $root = $this->factory->createItem('root')
62
            ->setChildrenAttributes([
63
                'class' => 'navbar-nav float-lg-right'
64
            ]);
65
66
        $menu = $root->addChild('admin', [
67
            'label' => ''
68
        ])
69
            ->setExtra('icon', 'fa fa-cogs')
70
            ->setAttribute('title', $this->translator->trans('management.label'))
71
            ->setExtra('menu', 'admin')
72
            ->setExtra('menu-container', '#submenu')
73
            ->setExtra('pull-right', true);
74
75
        if($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
76
            $menu->addChild('settings.label', [
77 6
                'route' => 'settings'
78 6
            ])
79
                ->setExtra('icon', 'fas fa-wrench');
80 6
81
            $menu->addChild('user_types.label', [
82
                'route' => 'user_types'
83 6
            ])
84
                ->setExtra('icon', 'fas fa-user-cog');
85
86 6
            $menu->addChild('user_roles.label', [
87 6
                'route' => 'user_roles'
88 6
            ])
89 6
                ->setExtra('icon', 'fas fa-user-tag');
90
91
            if($this->adAuthEnabled === true) {
92 6
                $menu->addChild('ad_sync_options.label', [
93 6
                    'route' => 'ad_sync_options'
94
                ])
95 6
                    ->setExtra('icon', 'fas fa-sync');
96 6
            }
97 6
98 6
            $menu->addChild('service_providers.label', [
99 6
                'route' => 'service_providers'
100
            ])
101 6
                ->setExtra('icon', 'fa fa-th');
102
103
            $menu->addChild('service_attributes.label', [
104
                'route' => 'attributes'
105
            ])
106
                ->setExtra('icon', 'far fa-list-alt');
107
108
            $menu->addChild('idp.details', [
109
                'route' => 'idp_details'
110
            ])
111
                ->setExtra('icon', 'fas fa-info-circle');
112
113
            $menu->addChild('applications.label', [
114
                'route' => 'applications'
115
            ])
116
                ->setExtra('icon', 'fas fa-key');
117
118
            $menu->addChild('cron.label', [
119
                'route' => 'admin_cronjobs'
120
            ])
121
                ->setExtra('icon', 'fas fa-history');
122
123
            $menu->addChild('logs.label', [
124
                'route' => 'admin_logs'
125
            ])
126
                ->setExtra('icon', 'fas fa-clipboard-list');
127
128
            $menu->addChild('messenger.label', [
129
                'route' => 'admin_messenger'
130
            ])
131
                ->setExtra('icon', 'fas fa-envelope-open-text');
132
133
            $menu->addChild('api.doc', [
134
                'uri' => '/docs/api'
135
            ]);
136
        }
137
138
        return $root;
139
    }
140
141
    public function userMenu(array $options): ItemInterface {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

141
    public function userMenu(/** @scrutinizer ignore-unused */ array $options): ItemInterface {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
142
        $menu = $this->factory->createItem('root')
143
            ->setChildrenAttributes([
144
                'class' => 'navbar-nav float-lg-right'
145
            ]);
146
147
        $user = $this->tokenStorage->getToken()->getUser();
148
149
        if(!$user instanceof User) {
150
            return $menu;
151
        }
152
153
        $label = 'dark_mode.enable';
154
        $icon = 'far fa-moon';
155
156
        if($this->darkModeManager->isDarkModeEnabled()) {
157
            $label = 'dark_mode.disable';
158
            $icon = 'far fa-sun';
159 6
        }
160
161
        $menu->addChild($label, [
162 6
            'route' => 'toggle_darkmode',
163 6
            'label' => ''
164 6
        ])
165 6
            ->setExtra('icon', $icon)
166
            ->setAttribute('title', $this->translator->trans($label));
167
168 6
        $displayName = $user->getUsername();
169
170 6
        $menu->addChild('user', [
171
            'label' => $displayName
172
        ])
173
            ->setExtra('icon', 'fa fa-user');
174 6
175 6
        $menu->addChild('label.logout', [
176
            'route' => 'logout',
177 6
            'label' => ''
178
        ])
179
            ->setExtra('icon', 'fas fa-sign-out-alt')
180
            ->setAttribute('title', $this->translator->trans('auth.logout'));
181
182 6
        return $menu;
183 6
    }
184
185
    public function servicesMenu(): ItemInterface {
186 6
        $root = $this->factory->createItem('root')
187 6
            ->setChildrenAttributes([
188
                'class' => 'navbar-nav float-lg-right'
189 6
            ]);
190
191 6
        $token = $this->tokenStorage->getToken();
192 6
        $user = $token->getUser();
193
194 6
        if($user instanceof User) {
195
            $menu = $root->addChild('services', [
196 6
                'label' => ''
197 6
            ])
198
                ->setExtra('icon', 'fa fa-th')
199
                ->setExtra('menu', 'services')
200 6
                ->setExtra('pull-right', true)
201 6
                ->setAttribute('title', $this->translator->trans('services.label'));
202
203 6
            $services = $this->userServiceProviderResolver->getServices($user);
204
            foreach ($services as $service) {
205
                $item = $menu->addChild($service->getName(), [
206 6
                    'uri' => $service->getUrl()
207 6
                ])
208 6
                    ->setAttribute('title', $service->getDescription())
209 6
                    ->setLinkAttribute('target', '_blank');
210
211
                if(!empty($service->getIcon())) {
212 6
                    $item->setExtra('icon', $service->getIcon());
213 6
                }
214
            }
215 6
        }
216 6
217 6
        return $root;
218
    }
219
}