Completed
Push — master ( 6c1fd7...c06421 )
by Craig
06:11 queued 49s
created

ExtensionMenu::getUser()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 4
nop 0
dl 0
loc 23
rs 9.7666
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A ExtensionMenu::getBundleName() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - 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
75
        return null;
76
    }
77
78
    private function getAdmin(): ?ItemInterface
79
    {
80
        $menu = $this->factory->createItem('zauthAdminMenu');
81
        if ($this->permissionApi->hasPermission($this->getBundleName() . '::', '::', ACCESS_ADMIN)) {
82
            $menu->addChild('Users list', [
83
                'route' => 'zikulazauthmodule_useradministration_list',
84
            ])->setAttribute('icon', 'fas fa-list');
85
        }
86
        if ($this->variableApi->get('ZikulaUsersModule', UsersConstant::MODVAR_REGISTRATION_ENABLED)) {
87
            $createUserAccessLevel = ACCESS_ADD;
88
        } else {
89
            $createUserAccessLevel = ACCESS_ADMIN;
90
        }
91
        if ($this->permissionApi->hasPermission($this->getBundleName() . '::', '::', $createUserAccessLevel)) {
92
            $menu->addChild('New users', [
93
                'route' => 'zikulazauthmodule_useradministration_create',
94
            ])->setAttribute('icon', 'fas fa-plus')
95
                ->setAttribute('dropdown', true);
96
            $menu['New users']->addChild('Create new user', [
97
                'route' => 'zikulazauthmodule_useradministration_create',
98
            ]);
99
            $menu['New users']->addChild('Import users', [
100
                'route' => 'zikulazauthmodule_fileio_import',
101
            ]);
102
        }
103
        if ($this->permissionApi->hasPermission($this->getBundleName() . '::', '::', ACCESS_ADMIN)) {
104
            $menu->addChild('Settings', [
105
                'route' => 'zikulazauthmodule_config_config',
106
            ])->setAttribute('icon', 'fas fa-wrench');
107
            $menu->addChild('Batch password change', [
108
                'route' => 'zikulazauthmodule_useradministration_batchforcepasswordchange',
109
            ])->setAttribute('icon', 'fas fa-lock');
110
        }
111
112
        return 0 === $menu->count() ? null : $menu;
113
    }
114
115
    private function getAccount(): ?ItemInterface
116
    {
117
        $menu = $this->factory->createItem('zauthAccountMenu');
118
        if (!$this->currentUser->isLoggedIn()) {
119
            $menu->addChild('UserName', [
120
                'label' => 'I have forgotten my account information (for example, my user name)',
121
                'route' => 'zikulazauthmodule_account_lostusername',
122
            ])->setAttribute('icon', 'fas fa-user');
123
            $menu->addChild('Password', [
124
                'label' => 'I have forgotten my password',
125
                'route' => 'zikulazauthmodule_account_lostpassword',
126
            ])->setAttribute('icon', 'fas fa-key');
127
        } else {
128
            $userMapping = $this->mappingRepository->findOneBy(['uid' => $this->currentUser->get('uid')]);
129
            if (isset($userMapping)) {
130
                $menu->addChild('Change password', [
131
                    'route' => 'zikulazauthmodule_account_changepassword',
132
                ])->setAttribute('icon', 'fas fa-key')
133
                    ->setLinkAttribute('class', 'text-success');
134
                $menu->addChild('Change e-mail address', [
135
                    'route' => 'zikulazauthmodule_account_changeemail',
136
                ])->setAttribute('icon', 'fas fa-at');
137
            }
138
        }
139
140
        return 0 === $menu->count() ? null : $menu;
141
    }
142
143
    public function getBundleName(): string
144
    {
145
        return 'ZikulaZAuthModule';
146
    }
147
}
148