Completed
Push — master ( 816ee7...b761d1 )
by Marcos
01:34
created

RefreshTokenProvider::setCustomUserProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the GesdinetJWTRefreshTokenBundle package.
5
 *
6
 * (c) Gesdinet <http://www.gesdinet.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gesdinet\JWTRefreshTokenBundle\Security\Provider;
13
14
use Symfony\Component\Security\Core\User\UserProviderInterface;
15
use Symfony\Component\Security\Core\User\User;
16
use Symfony\Component\Security\Core\User\UserInterface;
17
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
18
use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenManagerInterface;
19
use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenInterface;
20
21
/**
22
 * Class RefreshTokenProvider.
23
 */
24
class RefreshTokenProvider implements UserProviderInterface
25
{
26
    protected $refreshTokenManager;
27
    protected $customUserProvider;
28
29 6
    public function __construct(RefreshTokenManagerInterface $refreshTokenManager)
30
    {
31 6
        $this->refreshTokenManager = $refreshTokenManager;
32 6
    }
33
34
    public function setCustomUserProvider(UserProviderInterface $customUserProvider)
35
    {
36
        $this->customUserProvider = $customUserProvider;
37
    }
38
39 2
    public function getUsernameForRefreshToken($token)
40
    {
41 2
        $refreshToken = $this->refreshTokenManager->get($token);
42
43 2
        if ($refreshToken instanceof RefreshTokenInterface) {
44 1
            return $refreshToken->getUsername();
45
        }
46
47 1
        return;
48
    }
49
50 1
    public function loadUserByUsername($username)
51
    {
52 1
        if ($this->customUserProvider != null) {
53
            return $this->customUserProvider->loadUserByUsername($username);
54
        } else {
55 1
            return new User(
56 1
                $username,
57 1
                null,
58 1
                array('ROLE_USER')
59 1
            );
60
        }
61
    }
62
63 1
    public function refreshUser(UserInterface $user)
64
    {
65 1
        if ($this->customUserProvider != null) {
66
            return $this->customUserProvider->refreshUser($user);
67
        } else {
68 1
            throw new UnsupportedUserException();
69
        }
70
    }
71
72 1
    public function supportsClass($class)
73
    {
74 1
        if ($this->customUserProvider != null) {
75
            return $this->customUserProvider->supportsClass($class);
76
        } else {
77 1
            return 'Symfony\Component\Security\Core\User\User' === $class;
78
        }
79
    }
80
}
81