UserProvider::loadUserByToken()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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