Completed
Pull Request — master (#17)
by Valentyn
13:24
created

UserProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 81.48%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 81
ccs 22
cts 27
cp 0.8148
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A loadUserByUsername() 0 12 2
A refreshUser() 0 20 3
A supportsClass() 0 5 2
A findUser() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Security;
5
6
use App\Repository\UserRepository;
7
use App\Entity\User;
8
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
9
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
10
use Symfony\Component\Security\Core\User\UserInterface;
11
use Symfony\Component\Security\Core\User\UserProviderInterface;
12
use Symfony\Component\Translation\TranslatorInterface;
13
14
class UserProvider implements UserProviderInterface
15
{
16
    /**
17
     * @var UserRepository
18
     */
19
    private $userRepository;
20
21
    /**
22
     * @var TranslatorInterface
23
     */
24
    private $translator;
25
26 6
    public function __construct(UserRepository $userRepository, TranslatorInterface $translator)
27
    {
28 6
        $this->userRepository = $userRepository;
29 6
        $this->translator = $translator;
30 6
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 2
    public function loadUserByUsername($username)
36
    {
37 2
        $user = $this->findUser($username);
38
39 2
        if (!$user) {
40 1
            throw new UsernameNotFoundException($this->translator->trans('not_found_by_username', [
41 1
                'username' => $username,
42 1
            ], 'users'));
43
        }
44
45 1
        return $user;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 1
    public function refreshUser(UserInterface $user): ?User
52
    {
53 1
        if (!$this->supportsClass(get_class($user))) {
54 1
            throw new UnsupportedUserException($this->translator->trans('unexpected_class', [
55 1
                'expected_class' => User::class,
56 1
                'actual_class' => get_class($user),
57 1
            ], 'exceptions'));
58
        }
59
60
        /**
61
         * @var $user User
62
         */
63
        if (null === $reloadedUser = $this->userRepository->find($user->getId())) {
64
            throw new UsernameNotFoundException($this->translator->trans('user_provider.reload.error', [
65
                'user_id' => $user->getId(),
66
            ], 'exceptions'));
67
        }
68
69
        return $reloadedUser;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 1
    public function supportsClass($class)
76
    {
77 1
        $userClass = User::class;
78 1
        return $userClass === $class || is_subclass_of($class, $userClass);
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $userClass can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
79
    }
80
81
    /**
82
     * Finds a user by username.
83
     *
84
     * This method is meant to be an extension point for child classes.
85
     *
86
     * @param string $username
87
     *
88
     * @return User|null
89
     */
90 2
    protected function findUser($username)
91
    {
92 2
        return $this->userRepository->loadUserByUsername($username);
93
    }
94
}