Completed
Push — master ( 8f2b16...c80f65 )
by Marcel
03:24
created

Builder::userMenu()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 3.0155

Importance

Changes 0
Metric Value
cc 3
eloc 26
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 42
ccs 22
cts 25
cp 0.88
crap 3.0155
rs 9.504
1
<?php
2
3
namespace App\Menu;
4
5
use App\Entity\ServiceProvider;
6
use App\Entity\User;
7
use App\Service\UserServiceProviderResolver;
8
use Knp\Menu\FactoryInterface;
9
use Knp\Menu\ItemInterface;
10
use SchulIT\CommonBundle\DarkMode\DarkModeManagerInterface;
11
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
12
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
13
use Symfony\Contracts\Translation\TranslatorInterface;
14
15
class Builder {
16
    private $factory;
17
    private $authorizationChecker;
18
    private $translator;
19
    private $tokenStorage;
20
    private $userServiceProviderResolver;
21
    private $darkModeManager;
22
    private $adAuthEnabled;
23
24 2
    public function __construct(FactoryInterface $factory, AuthorizationCheckerInterface $authorizationChecker,
25
                                TranslatorInterface $translator, TokenStorageInterface $tokenStorage,
26
                                UserServiceProviderResolver $userServiceProviderResolver, DarkModeManagerInterface $darkModeManager,
27
                                bool $adAuthEnabled) {
28 2
        $this->factory = $factory;
29 2
        $this->authorizationChecker = $authorizationChecker;
30 2
        $this->translator = $translator;
31 2
        $this->tokenStorage = $tokenStorage;
32 2
        $this->userServiceProviderResolver = $userServiceProviderResolver;
33 2
        $this->darkModeManager = $darkModeManager;
34 2
        $this->adAuthEnabled = $adAuthEnabled;
35 2
    }
36
37 2
    public function mainMenu(): ItemInterface {
38 2
        $menu = $this->factory->createItem('root')
39 2
            ->setChildrenAttribute('class', 'navbar-nav mr-auto');
40
41 2
        $menu->addChild('dashboard.label', [
42 2
            'route' => 'dashboard'
43
        ])
44 2
            ->setAttribute('icon', 'fa fa-home');
45
46 2
        $menu->addChild('profile.label', [
47 2
            'route' => 'profile'
48
        ])
49 2
            ->setAttribute('icon', 'fas fa-user');
50
51 2
        $menu->addChild('two_factor.label', [
52 2
            'route' => 'two_factor'
53
        ])
54 2
            ->setAttribute('icon', 'fas fa-shield-alt');
55
56 2
        if($this->authorizationChecker->isGranted('ROLE_ADMIN')) {
57
            $menu->addChild('users.label', [
58
                'route' => 'users'
59
            ])
60
                ->setAttribute('icon', 'fas fa-users');
61
62
            $menu->addChild('codes.label', [
63
                'route' => 'registration_codes'
64
            ])
65
                ->setAttribute('icon', 'fas fa-qrcode');
66
67
            $menu->addChild('privacy_policy.label', [
68
                'route' => 'edit_privacy_policy'
69
            ])
70
                ->setAttribute('icon', 'fas fa-user-shield');
71
        } else {
72 2
            $menu->addChild('privacy_policy.label', [
73 2
                'route' => 'show_privacy_policy'
74
            ])
75 2
                ->setAttribute('icon', 'fas fa-user-shield');
76
        }
77
78 2
        return $menu;
79
    }
80
81 2
    public function adminMenu(array $options) {
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

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

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...
82 2
        $root = $this->factory->createItem('root')
83 2
            ->setChildrenAttributes([
84 2
                'class' => 'navbar-nav float-lg-right'
85
            ]);
86
87 2
        $menu = $root->addChild('admin', [
88 2
            'label' => ''
89
        ])
90 2
            ->setAttribute('icon', 'fa fa-cogs')
91 2
            ->setAttribute('title', $this->translator->trans('management.label'))
92 2
            ->setExtra('menu', 'admin')
93 2
            ->setExtra('menu-container', '#submenu')
94 2
            ->setExtra('pull-right', true);
95
96 2
        if($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
97
            $menu->addChild('user_types.label', [
98
                'route' => 'user_types'
99
            ])
100
                ->setAttribute('icon', 'fas fa-user-cog');
101
102
            $menu->addChild('user_roles.label', [
103
                'route' => 'user_roles'
104
            ])
105
                ->setAttribute('icon', 'fas fa-user-tag');
106
107
            if($this->adAuthEnabled === true) {
108
                $menu->addChild('ad_sync_options.label', [
109
                    'route' => 'ad_sync_options'
110
                ])
111
                    ->setAttribute('icon', 'fas fa-sync');
112
            }
113
114
            $menu->addChild('service_providers.label', [
115
                'route' => 'service_providers'
116
            ])
117
                ->setAttribute('icon', 'fa fa-th');
118
119
            $menu->addChild('service_attributes.label', [
120
                'route' => 'attributes'
121
            ])
122
                ->setAttribute('icon', 'far fa-list-alt');
123
124
            $menu->addChild('idp.details', [
125
                'route' => 'idp_details'
126
            ])
127
                ->setAttribute('icon', 'fas fa-info-circle');
128
129
            $menu->addChild('applications.label', [
130
                'route' => 'applications'
131
            ])
132
                ->setAttribute('icon', 'fas fa-key');
133
134
            $menu->addChild('cron.label', [
135
                'route' => 'admin_cronjobs'
136
            ])
137
                ->setAttribute('icon', 'fas fa-history');
138
139
            $menu->addChild('logs.label', [
140
                'route' => 'admin_logs'
141
            ])
142
                ->setAttribute('icon', 'fas fa-clipboard-list');
143
144
            $menu->addChild('mails.label', [
145
                'route' => 'admin_mails'
146
            ])
147
                ->setAttribute('icon', 'far fa-envelope');
148
149
            $menu->addChild('api.doc', [
150
                'uri' => '/docs/api'
151
            ]);
152
        }
153
154 2
        return $root;
155
    }
156
157 2
    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

157
    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...
158 2
        $menu = $this->factory->createItem('root')
159 2
            ->setChildrenAttributes([
160 2
                'class' => 'navbar-nav float-lg-right'
161
            ]);
162
163 2
        $user = $this->tokenStorage->getToken()->getUser();
164
165 2
        if(!$user instanceof User) {
166
            return $menu;
167
        }
168
169 2
        $label = 'dark_mode.enable';
170 2
        $icon = 'far fa-moon';
171
172 2
        if($this->darkModeManager->isDarkModeEnabled()) {
173
            $label = 'dark_mode.disable';
174
            $icon = 'far fa-sun';
175
        }
176
177 2
        $menu->addChild($label, [
178 2
            'route' => 'toggle_darkmode',
179
            'label' => ''
180
        ])
181 2
            ->setAttribute('icon', $icon)
182 2
            ->setAttribute('title', $this->translator->trans($label));
183
184 2
        $displayName = $user->getUsername();
185
186 2
        $menu->addChild('user', [
187 2
            'label' => $displayName
188
        ])
189 2
            ->setAttribute('icon', 'fa fa-user');
190
191 2
        $menu->addChild('label.logout', [
192 2
            'route' => 'logout',
193
            'label' => ''
194
        ])
195 2
            ->setAttribute('icon', 'fas fa-sign-out-alt')
196 2
            ->setAttribute('title', $this->translator->trans('auth.logout'));
197
198 2
        return $menu;
199
    }
200
201 2
    public function servicesMenu(): ItemInterface {
202 2
        $root = $this->factory->createItem('root')
203 2
            ->setChildrenAttributes([
204 2
                'class' => 'navbar-nav float-lg-right'
205
            ]);
206
207 2
        $token = $this->tokenStorage->getToken();
208 2
        $user = $token->getUser();
209
210 2
        if($user instanceof User) {
211 2
            $menu = $root->addChild('services', [
212 2
                'label' => ''
213
            ])
214 2
                ->setAttribute('icon', 'fa fa-th')
215 2
                ->setExtra('menu', 'services')
216 2
                ->setExtra('pull-right', true)
217 2
                ->setAttribute('title', $this->translator->trans('services.label'));
218
219 2
            $services = $this->userServiceProviderResolver->getServices($user);
220 2
            foreach ($services as $service) {
221
                $menu->addChild($service->getName(), [
222
                    'uri' => $service->getUrl()
223
                ])
224
                    ->setAttribute('title', $service->getDescription())
225
                    ->setAttribute('target', '_blank');
226
            }
227
        }
228
229 2
        return $root;
230
    }
231
}