Completed
Pull Request — master (#63)
by
unknown
13:08
created

RefreshToken   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 92.59%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 8
dl 0
loc 65
ccs 25
cts 27
cp 0.9259
rs 10
c 2
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
B refresh() 0 31 5
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\Service;
13
14
use Symfony\Component\HttpFoundation\Request;
15
use Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Authentication\AuthenticationSuccessHandler;
16
use Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Authentication\AuthenticationFailureHandler;
17
use Symfony\Component\Security\Core\Exception\AuthenticationException;
18
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
19
use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenManagerInterface;
20
use Gesdinet\JWTRefreshTokenBundle\Security\Authenticator\RefreshTokenAuthenticator;
21
use Gesdinet\JWTRefreshTokenBundle\Security\Provider\RefreshTokenProvider;
22
23
/**
24
 * Class RefreshToken.
25
 */
26
class RefreshToken
27
{
28
    private $authenticator;
29
    private $provider;
30
    private $tokenStorage;
31
    private $successHandler;
32
    private $failureHandler;
33
    private $refreshTokenManager;
34
    private $ttl;
35
    private $ttlUpdate;
36
37 4
    public function __construct(RefreshTokenAuthenticator $authenticator, RefreshTokenProvider $provider, TokenStorageInterface $tokenStorage, AuthenticationSuccessHandler $successHandler, AuthenticationFailureHandler $failureHandler, RefreshTokenManagerInterface $refreshTokenManager, $ttl, $providerKey, $ttlUpdate)
38
    {
39 4
        $this->authenticator = $authenticator;
40 4
        $this->provider = $provider;
41 4
        $this->tokenStorage = $tokenStorage;
42 4
        $this->successHandler = $successHandler;
43 4
        $this->failureHandler = $failureHandler;
44 4
        $this->refreshTokenManager = $refreshTokenManager;
45 4
        $this->ttl = $ttl;
46 4
        $this->providerKey = $providerKey;
0 ignored issues
show
Bug introduced by
The property providerKey does not seem to exist. Did you mean provider?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
47 4
        $this->ttlUpdate = $ttlUpdate;
48 4
    }
49
50
    /**
51
     * Refresh token.
52
     *
53
     * @param Request $request
54
     *
55
     * @return mixed
56
     *
57
     * @throws AuthenticationException
58
     */
59 3
    public function refresh(Request $request)
60
    {
61
        try {
62 3
            $preAuthenticatedToken = $this->authenticator->authenticateToken(
63 3
                    $this->authenticator->createToken($request, $this->providerKey), $this->provider, $this->providerKey
0 ignored issues
show
Bug introduced by
The property providerKey does not seem to exist. Did you mean provider?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
64
            );
65
        } catch (AuthenticationException $e) {
66
            return $this->failureHandler->onAuthenticationFailure($request, $e);
67
        }
68
69 3
        $refreshToken = $this->refreshTokenManager->get($preAuthenticatedToken->getCredentials());
70
71 3
        if (null === $refreshToken || !$refreshToken->isValid()) {
72 1
            return $this->failureHandler->onAuthenticationFailure($request, new AuthenticationException(
73 1
                            sprintf('Refresh token "%s" is invalid.', $refreshToken)
74
                            )
75
            );
76
        }
77
78 2
        if ($this->ttlUpdate) {
79 1
            $expirationDate = new \DateTime();
80 1
            $expirationDate->modify(sprintf('+%d seconds', $this->ttl));
81 1
            $refreshToken->setValid($expirationDate);
82
83 1
            $this->refreshTokenManager->save($refreshToken);
84
        }
85
86 2
        $this->tokenStorage->setToken($preAuthenticatedToken);
87
88 2
        return $this->successHandler->onAuthenticationSuccess($request, $preAuthenticatedToken);
89
    }
90
}
91