Completed
Push — master ( 96ea8b...1276b9 )
by Alexis
08:08
created

UserProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 71
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A loadUserByUsername() 0 10 2
A refreshUser() 0 14 3
A supportsClass() 0 6 2
A findUser() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the awurth/silex-user package.
5
 *
6
 * (c) Alexis Wurth <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace AWurth\Silex\User\Provider;
13
14
use AWurth\Silex\User\Model\UserInterface;
15
use AWurth\Silex\User\Model\UserManagerInterface;
16
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
17
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
18
use Symfony\Component\Security\Core\User\UserInterface as SecurityUserInterface;
19
use Symfony\Component\Security\Core\User\UserProviderInterface;
20
21
class UserProvider implements UserProviderInterface
22
{
23
    /**
24
     * @var UserManagerInterface
25
     */
26
    protected $userManager;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param UserManagerInterface $userManager
32
     */
33
    public function __construct(UserManagerInterface $userManager)
34
    {
35
        $this->userManager = $userManager;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function loadUserByUsername($username)
42
    {
43
        $user = $this->findUser($username);
44
45
        if (null === $user) {
46
            throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
47
        }
48
49
        return $user;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function refreshUser(SecurityUserInterface $user)
56
    {
57
        $class = get_class($user);
58
59
        if (!$user instanceof UserInterface) {
60
            throw new UnsupportedUserException(sprintf('Expected an instance of AWurth\Silex\User\Entity\UserInterface, but got "%s".', $class));
61
        }
62
63
        if (!$this->supportsClass($class)) {
64
            throw new UnsupportedUserException(sprintf('Expected an instance of %s, but got "%s".', $this->userManager->getClass(), $class));
65
        }
66
67
        return $this->loadUserByUsername($user->getUsername());
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function supportsClass($class)
74
    {
75
        $userClass = $this->userManager->getClass();
76
77
        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...
78
    }
79
80
    /**
81
     * Finds a user by username.
82
     *
83
     * @param string $username
84
     *
85
     * @return UserInterface|null
86
     */
87
    protected function findUser($username)
88
    {
89
        return $this->userManager->findUserByUsername($username);
90
    }
91
}
92