Completed
Pull Request — master (#25)
by Jérémy
05:47
created

RefreshToken::refresh()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5.0043

Importance

Changes 4
Bugs 2 Features 2
Metric Value
c 4
b 2
f 2
dl 0
loc 28
ccs 17
cts 18
cp 0.9444
rs 8.439
cc 5
eloc 15
nc 4
nop 1
crap 5.0043
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
            $modifier = sprintf('+%d seconds', $this->ttl);
77 1
            $refreshToken->getValid()->modify($modifier);
78
79 1
            $this->refreshTokenManager->save($refreshToken);
80 1
        }
81
82 2
        return $this->successHandler->onAuthenticationSuccess($request, $preAuthenticatedToken);
83
    }
84
}
85