Completed
Push — master ( 89f61c...cabb13 )
by Marcos
13:30 queued 09:28
created

RefreshToken::refresh()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4.002

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 30
ccs 19
cts 20
cp 0.95
rs 8.5806
cc 4
eloc 18
nc 3
nop 1
crap 4.002
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
33 3
    public function __construct(RefreshTokenAuthenticator $authenticator, RefreshTokenProvider $provider, AuthenticationSuccessHandler $successHandler, AuthenticationFailureHandler $failureHandler, RefreshTokenManagerInterface $refreshTokenManager, $ttl, $providerKey)
34
    {
35 3
        $this->authenticator = $authenticator;
36 3
        $this->provider = $provider;
37 3
        $this->successHandler = $successHandler;
38 3
        $this->failureHandler = $failureHandler;
39 3
        $this->refreshTokenManager = $refreshTokenManager;
40 3
        $this->ttl = $ttl;
0 ignored issues
show
Bug introduced by
The property ttl does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41 3
        $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...
42 3
    }
43
44
    /**
45
     * Refresh token.
46
     *
47
     * @param Request $request
48
     *
49
     * @return mixed
50
     *
51
     * @throws AuthenticationException
52
     */
53 2
    public function refresh(Request $request)
54
    {
55
        try {
56 2
            $preAuthenticatedToken = $this->authenticator->authenticateToken(
57 2
                $this->authenticator->createToken($request, $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...
58 2
                $this->provider,
59 2
                $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...
60 2
            );
61 2
        } catch (AuthenticationException $e) {
62
            return $this->failureHandler->onAuthenticationFailure($request, $e);
63
        }
64
65 2
        $refreshToken = $this->refreshTokenManager->get($preAuthenticatedToken->getCredentials());
66
67 2
        if (null === $refreshToken || !$refreshToken->isValid()) {
68 1
            return $this->failureHandler->onAuthenticationFailure($request,
69 1
                new AuthenticationException(
70 1
                    sprintf('Refresh token "%s" is invalid.', $refreshToken)
71 1
                )
72 1
            );
73
        }
74
75 1
        $datetime = new \DateTime();
76 1
        $datetime->modify('+'.$this->ttl.' seconds');
77 1
        $refreshToken->setValid($datetime);
78
79 1
        $this->refreshTokenManager->save($refreshToken);
80
81 1
        return $this->successHandler->onAuthenticationSuccess($request, $preAuthenticatedToken);
82
    }
83
}
84