Completed
Push — master ( 247c01...b42d97 )
by Artem
03:04
created

AbstractUserProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A loadUserByUsername() 0 10 2
A refreshUser() 0 14 3
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 Pignus\Provider;
13
14
use Pignus\Model\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
 * Abstract provider to load users from the database.
22
 */
23
abstract class AbstractUserProvider 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 2
    public function loadUserByUsername($username)
41
    {
42 2
        $user = $this->repository->findOneByUsername($username);
43
44 2
        if ($user === null) {
45 1
            throw new UsernameNotFoundException();
46
        }
47
48 1
        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
        $user = $this->repository->findOneByUsername($user->getUsername());
61
62 2
        if ($user === null) {
63 1
            throw new UsernameNotFoundException();
64
        }
65
66 1
        return $user;
67
    }
68
}
69