Completed
Push — master ( 5b6d3c...e19d53 )
by Marcel
03:17
created

Builder::servicesMenu()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3.072

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 19
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 29
ccs 16
cts 20
cp 0.8
crap 3.072
rs 9.6333
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 SchoolIT\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
23 2
    public function __construct(FactoryInterface $factory, AuthorizationCheckerInterface $authorizationChecker,
24
                                TranslatorInterface $translator, TokenStorageInterface $tokenStorage,
25
                                UserServiceProviderResolver $userServiceProviderResolver, DarkModeManagerInterface $darkModeManager) {
26 2
        $this->factory = $factory;
27 2
        $this->authorizationChecker = $authorizationChecker;
28 2
        $this->translator = $translator;
29 2
        $this->tokenStorage = $tokenStorage;
30 2
        $this->userServiceProviderResolver = $userServiceProviderResolver;
31 2
        $this->darkModeManager = $darkModeManager;
32 2
    }
33
34 2
    public function mainMenu(): ItemInterface {
35 2
        $menu = $this->factory->createItem('root')
36 2
            ->setChildrenAttribute('class', 'navbar-nav mr-auto');
37
38 2
        $menu->addChild('dashboard.label', [
39 2
            'route' => 'dashboard'
40
        ])
41 2
            ->setAttribute('icon', 'fa fa-home');
42
43 2
        $menu->addChild('profile.label', [
44 2
            'route' => 'profile'
45
        ])
46 2
            ->setAttribute('icon', 'fas fa-user');
47
48 2
        $menu->addChild('two_factor.label', [
49 2
            'route' => 'two_factor'
50
        ])
51 2
            ->setAttribute('icon', 'fas fa-shield-alt');
52
53 2
        if($this->authorizationChecker->isGranted('ROLE_ADMIN')) {
54
            $menu->addChild('users.label', [
55
                'route' => 'users'
56
            ])
57
                ->setAttribute('icon', 'fas fa-users');
58
59
            $menu->addChild('codes.label', [
60
                'route' => 'registration_codes'
61
            ])
62
                ->setAttribute('icon', 'fas fa-qrcode');
63
64
            $menu->addChild('privacy_policy.label', [
65
                'route' => 'edit_privacy_policy'
66
            ])
67
                ->setAttribute('icon', 'fas fa-user-shield');
68
        } else {
69 2
            $menu->addChild('privacy_policy.label', [
70 2
                'route' => 'show_privacy_policy'
71
            ])
72 2
                ->setAttribute('icon', 'fas fa-user-shield');
73
        }
74
75 2
        return $menu;
76
    }
77
78 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

78
    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...
79 2
        $root = $this->factory->createItem('root')
80 2
            ->setChildrenAttributes([
81 2
                'class' => 'navbar-nav float-lg-right'
82
            ]);
83
84 2
        $menu = $root->addChild('admin', [
85 2
            'label' => ''
86
        ])
87 2
            ->setAttribute('icon', 'fa fa-cogs')
88 2
            ->setAttribute('title', $this->translator->trans('management.label'))
89 2
            ->setExtra('menu', 'admin')
90 2
            ->setExtra('menu-container', '#submenu')
91 2
            ->setExtra('pull-right', true);
92
93 2
        if($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
94
            $menu->addChild('user_types.label', [
95
                'route' => 'user_types'
96
            ]);
97
98
            $menu->addChild('user_roles.label', [
99
                'route' => 'user_roles'
100
            ]);
101
102
            $menu->addChild('ad_sync_options.label', [
103
                'route' => 'ad_sync_options'
104
            ]);
105
106
            $menu->addChild('service_providers.label', [
107
                'route' => 'service_providers'
108
            ]);
109
110
            $menu->addChild('service_attributes.label', [
111
                'route' => 'attributes'
112
            ]);
113
114
            $menu->addChild('idp.details', [
115
                'route' => 'idp_details'
116
            ]);
117
118
            $menu->addChild('applications.label', [
119
                'route' => 'applications'
120
            ]);
121
122
            $menu->addChild('logs.label', [
123
                'route' => 'admin_logs'
124
            ]);
125
126
            $menu->addChild('mails.label', [
127
                'route' => 'admin_mails'
128
            ]);
129
130
            $menu->addChild('api.doc', [
131
                'uri' => '/docs/api'
132
            ]);
133
        }
134
135 2
        return $root;
136
    }
137
138 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

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