Passed
Pull Request — master (#1439)
by Tarmo
07:54
created

RolesService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 0
c 2
b 1
f 0
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Security/RolesService.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Security;
10
11
use App\Security\Interfaces\RolesServiceInterface;
12
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
13
use function array_key_exists;
14
use function array_unique;
15
use function array_values;
16
use function mb_strpos;
17
use function mb_strtolower;
18
use function mb_substr;
19
20
/**
21
 * Class RolesService
22
 *
23
 * @package App\Security
24
 * @author TLe, Tarmo Leppänen <[email protected]>
25
 */
26
class RolesService implements RolesServiceInterface
27
{
28
    /**
29
     * @var array<string, string>
30
     */
31
    private static array $roleNames = [
32
        self::ROLE_LOGGED => 'Logged in users',
33
        self::ROLE_USER => 'Normal users',
34
        self::ROLE_ADMIN => 'Admin users',
35
        self::ROLE_ROOT => 'Root users',
36
        self::ROLE_API => 'API users',
37
    ];
38
39 451
    public function __construct(
40
        private RoleHierarchyInterface $roleHierarchy,
41
    ) {
42 451
    }
43
44 2
    public function getRoles(): array
45
    {
46
        return [
47 2
            self::ROLE_LOGGED,
48 2
            self::ROLE_USER,
49 2
            self::ROLE_ADMIN,
50 2
            self::ROLE_ROOT,
51 2
            self::ROLE_API,
52
        ];
53
    }
54
55 7
    public function getRoleLabel(string $role): string
56
    {
57 7
        $output = 'Unknown - ' . $role;
58
59 7
        if (array_key_exists($role, self::$roleNames)) {
60 6
            $output = self::$roleNames[$role];
61
        }
62
63 7
        return $output;
64
    }
65
66 7
    public function getShort(string $role): string
67
    {
68 7
        $offset = mb_strpos($role, '_');
69 7
        $offset = $offset !== false ? $offset + 1 : 0;
70
71 7
        return mb_strtolower(mb_substr($role, $offset));
72
    }
73
74 298
    public function getInheritedRoles(array $roles): array
75
    {
76 298
        return array_values(array_unique($this->roleHierarchy->getReachableRoleNames($roles)));
77
    }
78
}
79