UserProvider::loadUserByUsername()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.032
1
<?php
2
3
/*
4
 * (c) Lukasz D. Tulikowski <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace App\Security;
13
14
use App\Entity\User;
15
use App\Service\Manager\UserManager;
16
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
17
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
18
use Symfony\Component\Security\Core\User\UserInterface;
19
use Symfony\Component\Security\Core\User\UserProviderInterface;
20
21
class UserProvider implements UserProviderInterface
22
{
23
    /**
24
     * @var UserManager
25
     */
26
    protected $userManager;
27
28
    /**
29
     * UserProvider constructor.
30
     *
31
     * @param UserManager $userManager
32
     */
33 51
    public function __construct(UserManager $userManager)
34
    {
35 51
        $this->userManager = $userManager;
36 51
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 22
    public function loadUserByUsername($username)
42
    {
43 22
        $user = $this->findUser($username);
44
45 22
        if (false === !!$user) {
46
            throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
47
        }
48
49 22
        return $user;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function refreshUser(UserInterface $user): ?UserInterface
56
    {
57
        /** @var User $user */
58
        if (!$this->supportsClass(\get_class($user))) {
59
            throw new UnsupportedUserException(sprintf('Expected an instance of %s, but got "%s".', $this->userManager->getClass(), \get_class($user)));
60
        }
61
62
        if (null === $reloadedUser = $this->userManager->findUserBy(['id' => $user->getId()])) {
63
            throw new UsernameNotFoundException(sprintf('User with ID "%s" could not be reloaded.', $user->getId()));
64
        }
65
66
        if ($reloadedUser instanceof UserInterface) {
67
            return $reloadedUser;
68
        }
69
70
        return null;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function supportsClass($class)
77
    {
78
        $userClass = $this->userManager->getClass();
79
80
        return $userClass === $class || is_subclass_of($class, $userClass);
81
    }
82
83
    /**
84
     * Finds a user by username.
85
     *
86
     * This method is meant to be an extension point for child classes.
87
     *
88
     * @param string $email
89
     *
90
     * @return UserInterface|null
91
     */
92 22
    protected function findUser($email): ?UserInterface
93
    {
94 22
        return $this->userManager->findUserByEmail($email);
95
    }
96
}
97