Passed
Pull Request — master (#81)
by
unknown
06:53
created

RefreshTokenProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 76.92%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 2
cbo 5
dl 0
loc 58
ccs 20
cts 26
cp 0.7692
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A loadUserByUsername() 0 12 2
A __construct() 0 4 1
A setCustomUserProvider() 0 4 1
A getUsernameForRefreshToken() 0 10 2
A refreshUser() 0 8 2
A supportsClass() 0 8 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
28
    protected $customUserProvider;
29
30 6
    public function __construct(RefreshTokenManagerInterface $refreshTokenManager)
31
    {
32 6
        $this->refreshTokenManager = $refreshTokenManager;
33 6
    }
34
35
    public function setCustomUserProvider(UserProviderInterface $customUserProvider)
36
    {
37
        $this->customUserProvider = $customUserProvider;
38
    }
39
40 2
    public function getUsernameForRefreshToken($token)
41
    {
42 2
        $refreshToken = $this->refreshTokenManager->get($token);
43
44 2
        if ($refreshToken instanceof RefreshTokenInterface) {
45 1
            return $refreshToken->getUsername();
46
        }
47
48 1
        return;
49
    }
50
51 1
    public function loadUserByUsername($username)
52
    {
53 1
        if (null != $this->customUserProvider) {
54
            return $this->customUserProvider->loadUserByUsername($username);
55
        } else {
56 1
            return new User(
57 1
                $username,
58 1
                null,
59 1
                array('ROLE_USER')
60
            );
61
        }
62
    }
63
64 1
    public function refreshUser(UserInterface $user)
65
    {
66 1
        if (null != $this->customUserProvider) {
67
            return $this->customUserProvider->refreshUser($user);
68
        } else {
69 1
            throw new UnsupportedUserException();
70
        }
71
    }
72
73 1
    public function supportsClass($class)
74
    {
75 1
        if (null != $this->customUserProvider) {
76
            return $this->customUserProvider->supportsClass($class);
77
        } else {
78 1
            return 'Symfony\Component\Security\Core\User\User' === $class;
79
        }
80
    }
81
}
82