RolesService   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 33
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getInheritedRoles() 0 3 1
A getRoles() 0 3 1
A getShort() 0 7 2
A __construct() 0 3 1
A getRoleLabel() 0 7 2
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\Enum\Role;
12
use App\Security\Interfaces\RolesServiceInterface;
13
use BackedEnum;
14
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
15
use function array_map;
16
use function array_unique;
17
use function array_values;
18
use function mb_strtolower;
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 718
    public function __construct(
29
        private readonly RoleHierarchyInterface $roleHierarchy,
30
    ) {
31 718
    }
32
33 13
    public function getRoles(): array
34
    {
35 13
        return array_map(static fn (BackedEnum $enum): string => $enum->value, Role::cases());
0 ignored issues
show
Bug introduced by
Accessing value on the interface BackedEnum suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
36
    }
37
38 18
    public function getRoleLabel(string $role): string
39
    {
40 18
        $enum = Role::tryFrom($role);
41
42 18
        return $enum instanceof Role
43 17
            ? $enum->label()
44 18
            : 'Unknown - ' . $role;
45
    }
46
47 18
    public function getShort(string $role): string
48
    {
49 18
        $enum = Role::tryFrom($role);
50
51 18
        return $enum instanceof Role
52 17
            ? mb_strtolower($enum->name)
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on App\Enum\Role.
Loading history...
53 18
            : 'Unknown - ' . $role;
54
    }
55
56 286
    public function getInheritedRoles(array $roles): array
57
    {
58 286
        return array_values(array_unique($this->roleHierarchy->getReachableRoleNames($roles)));
59
    }
60
}
61