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

RefreshToken::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 11
ccs 10
cts 10
cp 1
rs 9.4286
cc 1
eloc 9
nc 1
nop 8
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 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