Test Setup Failed
Push — master ( 195648...185a1e )
by Craig
58s queued 15s
created

ExtensionMenu   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
eloc 70
dl 0
loc 138
rs 10
c 5
b 1
f 1
wmc 19

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getUser() 0 23 3
A getBundleName() 0 3 1
A getAccount() 0 18 4
B getAdmin() 0 32 6
A __construct() 0 12 1
A get() 0 13 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\ZAuthModule\Menu;
15
16
use Knp\Menu\FactoryInterface;
17
use Knp\Menu\ItemInterface;
18
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
19
use Zikula\MenuModule\ExtensionMenu\ExtensionMenuInterface;
20
use Zikula\PermissionsModule\Api\ApiInterface\PermissionApiInterface;
21
use Zikula\UsersModule\Api\ApiInterface\CurrentUserApiInterface;
22
use Zikula\UsersModule\Constant as UsersConstant;
23
use Zikula\ZAuthModule\Entity\RepositoryInterface\AuthenticationMappingRepositoryInterface;
24
25
class ExtensionMenu implements ExtensionMenuInterface
26
{
27
    /**
28
     * @var FactoryInterface
29
     */
30
    private $factory;
31
32
    /**
33
     * @var PermissionApiInterface
34
     */
35
    private $permissionApi;
36
37
    /**
38
     * @var VariableApiInterface
39
     */
40
    private $variableApi;
41
42
    /**
43
     * @var CurrentUserApiInterface
44
     */
45
    private $currentUser;
46
47
    /**
48
     * @var AuthenticationMappingRepositoryInterface
49
     */
50
    private $mappingRepository;
51
52
    public function __construct(
53
        FactoryInterface $factory,
54
        PermissionApiInterface $permissionApi,
55
        VariableApiInterface $variableApi,
56
        CurrentUserApiInterface $currentUserApi,
57
        AuthenticationMappingRepositoryInterface $mappingRepository
58
    ) {
59
        $this->factory = $factory;
60
        $this->permissionApi = $permissionApi;
61
        $this->variableApi = $variableApi;
62
        $this->currentUser = $currentUserApi;
63
        $this->mappingRepository = $mappingRepository;
64
    }
65
66
    public function get(string $type = self::TYPE_ADMIN): ?ItemInterface
67
    {
68
        if (self::TYPE_ADMIN === $type) {
69
            return $this->getAdmin();
70
        }
71
        if (self::TYPE_ACCOUNT === $type) {
72
            return $this->getAccount();
73
        }
74
        if (self::TYPE_USER === $type) {
75
            return $this->getUser();
76
        }
77
78
        return null;
79
    }
80
81
    private function getAdmin(): ?ItemInterface
82
    {
83
        $menu = $this->factory->createItem('zauthAdminMenu');
84
        if ($this->permissionApi->hasPermission($this->getBundleName() . '::', '::', ACCESS_ADMIN)) {
85
            $menu->addChild('Users list', [
86
                'route' => 'zikulazauthmodule_useradministration_list',
87
            ])->setAttribute('icon', 'fas fa-list');
88
        }
89
        if ($this->variableApi->get('ZikulaUsersModule', UsersConstant::MODVAR_REGISTRATION_ENABLED)) {
90
            $createUserAccessLevel = ACCESS_ADD;
91
        } else {
92
            $createUserAccessLevel = ACCESS_ADMIN;
93
        }
94
        if ($this->permissionApi->hasPermission($this->getBundleName() . '::', '::', $createUserAccessLevel)) {
95
            $menu->addChild('New users', [
96
                'route' => 'zikulazauthmodule_useradministration_create',
97
            ])->setAttribute('icon', 'fas fa-plus')
98
                ->setAttribute('dropdown', true);
99
            $menu['New users']->addChild('Create new user', [
100
                'route' => 'zikulazauthmodule_useradministration_create',
101
            ]);
102
            $menu['New users']->addChild('Import users', [
103
                'route' => 'zikulazauthmodule_fileio_import',
104
            ]);
105
        }
106
        if ($this->permissionApi->hasPermission($this->getBundleName() . '::', '::', ACCESS_ADMIN)) {
107
            $menu->addChild('Settings', [
108
                'route' => 'zikulazauthmodule_config_config',
109
            ])->setAttribute('icon', 'fas fa-wrench');
110
        }
111
112
        return 0 === $menu->count() ? null : $menu;
113
    }
114
115
    private function getAccount(): ?ItemInterface
116
    {
117
        if (!$this->currentUser->isLoggedIn()) {
118
            return null;
119
        }
120
        $menu = $this->factory->createItem('zauthAccountMenu');
121
        $userMapping = $this->mappingRepository->findOneBy(['uid' => $this->currentUser->get('uid')]);
122
        if (isset($userMapping)) {
123
            $menu->addChild('Change password', [
124
                'route' => 'zikulazauthmodule_account_changepassword',
125
            ])->setAttribute('icon', 'fas fa-key')
126
            ->setLinkAttribute('class', 'text-success');
127
            $menu->addChild('Change e-mail address', [
128
                'route' => 'zikulazauthmodule_account_changeemail',
129
            ])->setAttribute('icon', 'fas fa-at');
130
        }
131
132
        return 0 === $menu->count() ? null : $menu;
133
    }
134
135
    private function getUser(): ?ItemInterface
136
    {
137
        $menu = $this->factory->createItem('zauthUserMenu');
138
        $menu->addChild('Account menu', [
139
            'route' => 'zikulausersmodule_account_menu',
140
        ])->setAttribute('icon', 'fas fa-user-circle');
141
142
        $acctMenu = $this->getAccount();
143
        foreach ($acctMenu as $item) {
144
            $menu->addChild($item);
145
        }
146
147
        $menu->addChild('Recover account information or password', [
148
            'route' => 'zikulausersmodule_account_menu',
149
        ])->setAttribute('icon', 'fas fa-key');
150
        $menu['Recover account information or password']->addChild('Recover Lost User Name', [
151
            'route' => 'zikulazauthmodule_account_lostusername',
152
        ]);
153
        $menu['Recover account information or password']->addChild('Recover Lost Password', [
154
            'route' => 'zikulazauthmodule_account_lostpassword',
155
        ]);
156
157
        return 0 === $menu->count() ? null : $menu;
158
    }
159
160
    public function getBundleName(): string
161
    {
162
        return 'ZikulaZAuthModule';
163
    }
164
}
165