SecurityUserFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 48
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A refreshUser() 0 13 3
A loadUserByIdentifier() 0 12 2
A supportsClass() 0 3 1
A __construct() 0 4 1
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\Routing\Requirement\Requirement;
16
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
17
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
18
use Symfony\Component\Security\Core\User\UserInterface;
19
use Symfony\Component\Security\Core\User\UserProviderInterface;
20
use Throwable;
21
22
/**
23
 * Class SecurityUserFactory
24
 *
25
 * @package App\Security\Provider
26
 * @author TLe, Tarmo Leppänen <[email protected]>
27
 *
28
 * @template-implements UserProviderInterface<SecurityUser>
29
 */
30
class SecurityUserFactory implements UserProviderInterface
31
{
32 287
    public function __construct(
33
        private readonly UserRepository $userRepository,
34
        private readonly RolesService $rolesService,
35
    ) {
36 287
    }
37
38 8
    public function supportsClass(string $class): bool
39
    {
40 8
        return $class === SecurityUser::class;
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     *
46
     * @throws Throwable
47
     */
48 272
    public function loadUserByIdentifier(string $identifier): SecurityUser
49
    {
50 272
        $user = $this->userRepository->loadUserByIdentifier(
51 272
            $identifier,
52 272
            (bool)preg_match('#' . Requirement::UUID_V1 . '#', $identifier)
0 ignored issues
show
Bug introduced by
The constant Symfony\Component\Routin...nt\Requirement::UUID_V1 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
53 272
        );
54
55 272
        if (!($user instanceof User)) {
56 8
            throw new UserNotFoundException(sprintf('User not found for UUID: "%s".', $identifier));
57
        }
58
59 264
        return new SecurityUser($user, $this->rolesService->getInheritedRoles($user->getRoles()));
60
    }
61
62
    /**
63
     * @throws Throwable
64
     */
65 7
    public function refreshUser(UserInterface $user): SecurityUser
66
    {
67 7
        if (!($user instanceof SecurityUser)) {
68 2
            throw new UnsupportedUserException(sprintf('Invalid user class "%s".', $user::class));
69
        }
70
71 5
        $userEntity = $this->userRepository->find($user->getUserIdentifier());
72
73 5
        if (!($userEntity instanceof User)) {
74 2
            throw new UserNotFoundException(sprintf('User not found for UUID: "%s".', $user->getUserIdentifier()));
75
        }
76
77 3
        return new SecurityUser($userEntity, $this->rolesService->getInheritedRoles($userEntity->getRoles()));
78
    }
79
}
80