UserProvider   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 87
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A findUserByUsernameOrEmail() 0 10 3
A findOneUserBy() 0 5 1
A __construct() 0 3 1
A loadUserByIdentifier() 0 14 2
A supportsClass() 0 3 1
A refreshUser() 0 12 2
1
<?php
2
3
namespace ProjetNormandie\UserBundle\Security;
4
5
use ProjetNormandie\UserBundle\Entity\User;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Symfony\Component\Security\Core\User\UserInterface;
8
use Symfony\Component\Security\Core\User\UserProviderInterface;
9
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
10
11
final class UserProvider implements UserProviderInterface
12
{
13
    /**
14
     * @var EntityManagerInterface
15
     */
16
    private EntityManagerInterface $entityManager;
17
18
    /**
19
     * @param EntityManagerInterface $entityManager
20
     */
21
    public function __construct(EntityManagerInterface $entityManager)
22
    {
23
        $this->entityManager = $entityManager;
24
    }
25
26
    /**
27
     * @param string $identifier
28
     * @return User
29
     */
30
    public function loadUserByIdentifier(string $identifier): User
31
    {
32
        $user = $this->findUserByUsernameOrEmail($identifier);
33
34
        if (!$user) {
35
            throw new UserNotFoundException(
36
                sprintf(
37
                    'User with "%s" email does not exist.',
38
                    $identifier
39
                )
40
            );
41
        }
42
43
        return $user;
44
    }
45
46
    /**
47
     * @param $usernameOrEmail
48
     * @return User|null
49
     */
50
    public function findUserByUsernameOrEmail($usernameOrEmail): ?User
51
    {
52
        if (preg_match('/^.+\@\S+\.\S+$/', $usernameOrEmail)) {
53
            $user = $this->findOneUserBy(['email' => $usernameOrEmail]);
54
            if (null !== $user) {
55
                return $user;
56
            }
57
        }
58
59
        return $this->findOneUserBy(['username' => $usernameOrEmail]);
60
    }
61
62
    /**
63
     * @param array $options
64
     * @return User|null
65
     */
66
    private function findOneUserBy(array $options): ?User
67
    {
68
        return $this->entityManager
69
            ->getRepository(User::class)
70
            ->findOneBy($options);
71
    }
72
73
    /**
74
     * @param UserInterface $user
75
     * @return User
76
     */
77
    public function refreshUser(UserInterface $user): User
78
    {
79
        assert($user instanceof User);
80
81
        if (null === $reloadedUser = $this->findOneUserBy(['id' => $user->getId()])) {
82
            throw new UserNotFoundException(sprintf(
83
                'User with ID "%s" could not be reloaded.',
84
                $user->getId()
85
            ));
86
        }
87
88
        return $reloadedUser;
89
    }
90
91
    /**
92
     * @param string $class
93
     * @return bool
94
     */
95
    public function supportsClass(string $class): bool
96
    {
97
        return $class === User::class;
98
    }
99
}
100