Completed
Pull Request — master (#26)
by Jérémy
06:33
created

RefreshToken   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 96.55%

Importance

Changes 5
Bugs 3 Features 2
Metric Value
wmc 6
c 5
b 3
f 2
lcom 1
cbo 7
dl 0
loc 61
ccs 28
cts 29
cp 0.9655
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B refresh() 0 29 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 Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenManagerInterface;
19
use Gesdinet\JWTRefreshTokenBundle\Security\Authenticator\RefreshTokenAuthenticator;
20
use Gesdinet\JWTRefreshTokenBundle\Security\Provider\RefreshTokenProvider;
21
22
/**
23
 * Class RefreshToken.
24
 */
25
class RefreshToken
26
{
27
    private $authenticator;
28
    private $provider;
29
    private $successHandler;
30
    private $failureHandler;
31
    private $refreshTokenManager;
32
    private $ttl;
33
    private $ttlUpdate;
34
35 4
    public function __construct(RefreshTokenAuthenticator $authenticator, RefreshTokenProvider $provider, AuthenticationSuccessHandler $successHandler, AuthenticationFailureHandler $failureHandler, RefreshTokenManagerInterface $refreshTokenManager, $ttl, $providerKey, $ttlUpdate)
36
    {
37 4
        $this->authenticator = $authenticator;
38 4
        $this->provider = $provider;
39 4
        $this->successHandler = $successHandler;
40 4
        $this->failureHandler = $failureHandler;
41 4
        $this->refreshTokenManager = $refreshTokenManager;
42 4
        $this->ttl = $ttl;
43 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...
44 4
        $this->ttlUpdate = $ttlUpdate;
45 4
    }
46
47
    /**
48
     * Refresh token.
49
     *
50
     * @param Request $request
51
     *
52
     * @return mixed
53
     *
54
     * @throws AuthenticationException
55
     */
56 3
    public function refresh(Request $request)
57
    {
58
        try {
59 3
            $preAuthenticatedToken = $this->authenticator->authenticateToken(
60 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...
61 3
            );
62 3
        } catch (AuthenticationException $e) {
63
            return $this->failureHandler->onAuthenticationFailure($request, $e);
64
        }
65
66 3
        $refreshToken = $this->refreshTokenManager->get($preAuthenticatedToken->getCredentials());
67
68 3
        if (null === $refreshToken || !$refreshToken->isValid()) {
69 1
            return $this->failureHandler->onAuthenticationFailure($request, new AuthenticationException(
70 1
                            sprintf('Refresh token "%s" is invalid.', $refreshToken)
71 1
                            )
72 1
            );
73
        }
74
75 2
        if ($this->ttlUpdate) {
76 1
            $expirationDate = new \DateTime();
77 1
            $expirationDate->modify(sprintf('+%d seconds', $this->ttl));
78 1
            $refreshToken->setValid($expirationDate);
79
80 1
            $this->refreshTokenManager->save($refreshToken);
81 1
        }
82
83 2
        return $this->successHandler->onAuthenticationSuccess($request, $preAuthenticatedToken);
84
    }
85
}
86