Completed
Push — master ( 0fb5d3...44fe8c )
by Artem
02:34
created

GenericUserProvider::loadUserByUsername()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 5
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 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
 * Generic provider to load users from the database.
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 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
    /**
70
     * {@inheritdoc}
71
     */
72 3
    public function supportsClass($class)
73
    {
74 3
        return $class === $this->repository->getClassName();
75
    }
76
}
77