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

GenericUserProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 48
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

4 Methods

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