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

UserProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A loadUserByUsername() 0 8 2
A refreshUser() 0 8 2
A supportsClass() 0 4 2
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