Completed
Push — master ( 22a42f...f630f8 )
by Artem
03:26
created

GenericUserProvider::refreshUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
//----------------------------------------------------------------------
4
//
5
//  Copyright (C) 2017 Artem Rodygin
6
//
7
//  You should have received a copy of the MIT License along with
8
//  this file. If not, see <http://opensource.org/licenses/MIT>.
9
//
10
//----------------------------------------------------------------------
11
12
namespace LazySec\Provider;
13
14
use LazySec\Repository\UserRepositoryInterface;
15
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
16
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
17
use Symfony\Component\Security\Core\User\UserInterface;
18
use Symfony\Component\Security\Core\User\UserProviderInterface;
19
20
/**
21
 * Generic provider to load users from specified repository.
22
 */
23
class GenericUserProvider implements UserProviderInterface
24
{
25
    protected $repository;
26
27
    /**
28
     * Dependency Injection constructor.
29
     *
30
     * @param UserRepositoryInterface $repository
31
     */
32 5
    public function __construct(UserRepositoryInterface $repository)
33
    {
34 5
        $this->repository = $repository;
35 5
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 4
    public function loadUserByUsername($username)
41
    {
42 4
        $user = $this->repository->findOneByUsername($username);
43
44 4
        if ($user === null) {
45 2
            throw new UsernameNotFoundException();
46
        }
47
48 2
        return $user;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 3
    public function refreshUser(UserInterface $user)
55
    {
56 3
        if (!$this->supportsClass(get_class($user))) {
57 1
            throw new UnsupportedUserException();
58
        }
59
60 2
        return $this->loadUserByUsername($user->getUsername());
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 3
    public function supportsClass($class)
67
    {
68 3
        return $class === $this->repository->getClassName();
69
    }
70
}
71