Completed
Push — master ( 7e589b...500fd9 )
by Tarmo
16s queued 11s
created

SecurityUserFactory::refreshUser()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Security/Provider/SecurityUserFactory.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Security\Provider;
10
11
use App\Entity\User;
12
use App\Repository\UserRepository;
13
use App\Security\RolesService;
14
use App\Security\SecurityUser;
15
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
16
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
17
use Symfony\Component\Security\Core\User\UserInterface;
18
use Symfony\Component\Security\Core\User\UserProviderInterface;
19
use Throwable;
20
use function get_class;
21
22
/**
23
 * Class SecurityUserFactory
24
 *
25
 * @package App\Security\Provider
26
 * @author TLe, Tarmo Leppänen <[email protected]>
27
 */
28
class SecurityUserFactory implements UserProviderInterface
29
{
30 345
    public function __construct(
31
        private UserRepository $userRepository,
32
        private RolesService $rolesService,
33
        private string $uuidV1Regex,
34
    ) {
35 345
    }
36
37 8
    public function supportsClass(string $class): bool
38
    {
39 8
        return $class === SecurityUser::class;
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     *
45
     * @throws Throwable
46
     */
47 277
    public function loadUserByIdentifier(string $identifier): SecurityUser
48
    {
49 277
        $user = $this->userRepository->loadUserByIdentifier(
50 277
            $identifier,
51 277
            (bool)preg_match('#' . $this->uuidV1Regex . '#', $identifier)
52
        );
53
54 277
        if (!($user instanceof User)) {
55 23
            throw new UserNotFoundException(sprintf('User not found for UUID: "%s".', $identifier));
56
        }
57
58 254
        return new SecurityUser($user, $this->rolesService->getInheritedRoles($user->getRoles()));
59
    }
60
61
    /**
62
     * @throws Throwable
63
     */
64 7
    public function refreshUser(UserInterface $user): SecurityUser
65
    {
66 7
        if (!($user instanceof SecurityUser)) {
67 2
            throw new UnsupportedUserException(sprintf('Invalid user class "%s".', get_class($user)));
68
        }
69
70 5
        $userEntity = $this->userRepository->find($user->getUserIdentifier());
71
72 5
        if (!($userEntity instanceof User)) {
73 2
            throw new UserNotFoundException(sprintf('User not found for UUID: "%s".', $user->getUserIdentifier()));
74
        }
75
76 3
        return new SecurityUser($userEntity, $this->rolesService->getInheritedRoles($userEntity->getRoles()));
77
    }
78
79
    /**
80
     * @todo Remove this method when Symfony 6.0.0 is released
81
     *
82
     * {@inheritDoc}
83
     *
84
     * @throws Throwable
85
     *
86
     * @codeCoverageIgnore
87
     */
88
    public function loadUserByUsername(string $username): SecurityUser
89
    {
90
        return $this->loadUserByIdentifier($username);
91
    }
92
}
93