SecurityClaims::resolve()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\Bundle\ApiAuthBundle\Jwt\Claims;
6
7
use Damax\Bundle\ApiAuthBundle\Jwt\Claims;
8
use Symfony\Component\Security\Core\Role\Role;
9
use Symfony\Component\Security\Core\User\UserInterface;
10
11
final class SecurityClaims implements Claims
12
{
13
    public function resolve(UserInterface $user): array
14
    {
15
        return [
16
            self::SUBJECT => $user->getUsername(),
17
            self::ROLES => array_map([$this, 'roleToString'], $user->getRoles()),
18
        ];
19
    }
20
21
    private function roleToString($role): string
22
    {
23
        $str = strtolower(strval($role instanceof Role ? $role->getRole() : $role));
24
25
        return 0 === strpos($str, 'role_') ? substr($str, 5) : $str;
26
    }
27
}
28