RolesService::__construct()   A
last analyzed

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 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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