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

AbstractUserProvider::refreshUser()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 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