Completed
Pull Request — master (#26)
by Valentyn
02:41
created

UserProvider::refreshUser()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
ccs 11
cts 11
cp 1
rs 9.4285
cc 3
eloc 11
nc 3
nop 1
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Users\Security;
5
6
use App\Users\Repository\ApiTokenRepository;
7
use App\Users\Repository\UserRepository;
8
use App\Users\Entity\User;
9
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
10
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
11
use Symfony\Component\Security\Core\User\UserInterface;
12
use Symfony\Component\Security\Core\User\UserProviderInterface;
13
use Symfony\Component\Translation\TranslatorInterface;
14
15
class UserProvider implements UserProviderInterface
16
{
17
    /**
18
     * @var UserRepository
19
     */
20
    private $userRepository;
21
22
    /**
23
     * @var ApiTokenRepository
24
     */
25
    private $apiTokenRepository;
26
27
    /**
28
     * @var TranslatorInterface
29
     */
30
    private $translator;
31
32 26
    public function __construct(
33
        UserRepository $userRepository,
34
        ApiTokenRepository $apiTokenRepository,
35
        TranslatorInterface $translator
36
    )
37
    {
38 26
        $this->userRepository = $userRepository;
39 26
        $this->apiTokenRepository = $apiTokenRepository;
40 26
        $this->translator = $translator;
41 26
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 2
    public function loadUserByUsername($username): User
47
    {
48 2
        $user = $this->userRepository->loadUserByUsername($username);
49
50 2
        if (!$user) {
51 1
            throw new UsernameNotFoundException($this->translator->trans('not_found_by_username', [
52 1
                'username' => $username,
53 1
            ], 'users'));
54
        }
55
56 1
        return $user;
57
    }
58
59
    /**
60
     * @param string $token
61
     * @return User
62
     */
63 7
    public function loadUserByToken(string $token): User
64
    {
65 7
        $apiToken = $this->apiTokenRepository->findByToken($token);
66
67 7
        if (!$apiToken) {
68 2
            throw new UsernameNotFoundException($this->translator->trans('not_found_by_api_token', [
69 2
                'token' => $token,
70 2
            ], 'users'));
71
        }
72
73 5
        return $apiToken->getUser();
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 3
    public function refreshUser(UserInterface $user): ?User
80
    {
81 3
        if (!$this->supportsClass(get_class($user))) {
82 1
            throw new UnsupportedUserException($this->translator->trans('unexpected_class', [
83 1
                'expected_class' => User::class,
84 1
                'actual_class' => get_class($user),
85 1
            ], 'exceptions'));
86
        }
87
88
        /**
89
         * @var $user User
90
         */
91 2
        if (null === $reloadedUser = $this->userRepository->find($user->getId())) {
92 1
            throw new UsernameNotFoundException($this->translator->trans('user_provider.reload.error', [
93 1
                'user_id' => $user->getId(),
94 1
            ], 'exceptions'));
95
        }
96
97 1
        return $reloadedUser;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 3
    public function supportsClass($class)
104
    {
105 3
        $userClass = User::class;
106 3
        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...
107
    }
108
}