Completed
Pull Request — development (#798)
by Nick
04:43
created

UserProvider::supportsClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Oc\Security;
6
7
use Oc\Entity\UserEntity;
8
use Oc\Repository\Exception\RecordNotFoundException;
9
use Oc\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
15
class UserProvider implements UserProviderInterface
16
{
17
    /**
18
     * @var UserRepository
19
     */
20
    private $userRepository;
21
22
    public function __construct(UserRepository $userRepository)
23
    {
24
        $this->userRepository = $userRepository;
25
    }
26
27
    public function loadUserByUsername($username): UserInterface
28
    {
29
        try {
30
            return $this->userRepository->fetchOneByUsername($username);
31
        } catch (RecordNotFoundException $e) {
32
            throw new UsernameNotFoundException('User by username "' . $username . '" not found!', 0, $e);
33
        }
34
    }
35
36
    public function refreshUser(UserInterface $user): UserInterface
37
    {
38
        if (!$user instanceof UserEntity) {
39
            throw new UnsupportedUserException(sprintf('Invalid user class "%s".', get_class($user)));
40
        }
41
42
        return $this->userRepository->fetchOneByUsername($user->getUsername());
43
    }
44
45
    public function supportsClass($class): bool
46
    {
47
        return UserEntity::class === $class || is_subclass_of($class, UserEntity::class);
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Oc\Entity\UserEntity::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
48
    }
49
}
50