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

RefreshToken::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
ccs 11
cts 11
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 10
nc 1
nop 9
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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