Passed
Push — main ( 674e66...68245a )
by Axel
04:12
created

RoleHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 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\UsersBundle\Helper;
15
16
use Symfony\Component\DependencyInjection\Attribute\Autowire;
17
18
class RoleHelper
19
{
20
    private array $roleHierarchy;
21
22
    public function __construct(
23
        #[Autowire('%security.role_hierarchy.roles%')]
24
        array $roleHierarchy
25
    ) {
26
        $this->roleHierarchy = $roleHierarchy;
27
    }
28
29
    public function getRoleOptions(): array
30
    {
31
        $systemRoles = [
32
            'User' => 'ROLE_USER',
33
            'Editor' => 'ROLE_EDITOR',
34
            'Administrator' => 'ROLE_ADMIN',
35
            'Super administrator' => 'ROLE_SUPER_ADMIN',
36
        ];
37
38
        $definedRoles = [];
39
        array_walk_recursive($this->roleHierarchy, function($role) use (&$roles) {
40
            $definedRoles[$role] = $role;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$definedRoles was never initialized. Although not strictly required by PHP, it is generally a good practice to add $definedRoles = array(); before regardless.
Loading history...
41
        });
42
43
        $roles = $systemRoles;
44
        foreach ($definedRoles as $role) {
45
            if (!in_array($role, $roles, true)) {
46
                $roles[$role] = $role;
47
            }
48
        }
49
50
        return $roles;
51
    }
52
}
53