Completed
Pull Request — master (#21)
by
unknown
11:48
created

AttachRefreshTokenOnSuccessListener   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 10.94 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 87.1%

Importance

Changes 9
Bugs 2 Features 1
Metric Value
wmc 6
c 9
b 2
f 1
lcom 1
cbo 4
dl 7
loc 64
ccs 27
cts 31
cp 0.871
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B attachRefreshToken() 7 50 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\EventListener;
13
14
use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenManagerInterface;
15
use Gesdinet\JWTRefreshTokenBundle\Entity\RefreshToken;
16
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
17
use Symfony\Component\Security\Core\User\UserInterface;
18
use Symfony\Component\Validator\Validator\ValidatorInterface;
19
20
class AttachRefreshTokenOnSuccessListener
21
{
22
    protected $userRefreshTokenManager;
23
    protected $ttl;
24
    protected $validator;
25
26 4
    public function __construct(RefreshTokenManagerInterface $refreshTokenManager, $ttl, ValidatorInterface $validator)
27
    {
28 4
        $this->refreshTokenManager = $refreshTokenManager;
0 ignored issues
show
Bug introduced by
The property refreshTokenManager does not seem to exist. Did you mean userRefreshTokenManager?

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...
29 4
        $this->ttl = $ttl;
30 4
        $this->validator = $validator;
31 4
    }
32
33 3
    public function attachRefreshToken(AuthenticationSuccessEvent $event)
34
    {
35 3
        $data = $event->getData();
36 3
        $user = $event->getUser();
37 3
        $request = $event->getRequest();
38
39 3
        if (!$user instanceof UserInterface) {
40 1
            return;
41
        }
42
43 2
        $refreshTokenString = null;
0 ignored issues
show
Unused Code introduced by
$refreshTokenString is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
44 2 View Code Duplication
        if ($request->headers->get('content_type') == 'application/json') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
            $content = $request->getContent();
46
            $params = !empty($content) ? json_decode($content, true) : array();
47
            $refreshTokenString = trim($params['refresh_token']);
48
        } else {
49 2
            $refreshTokenString = $request->request->get('refresh_token');
50
        }
51
52 2
        if ($refreshTokenString) {
53 1
            $data['refresh_token'] = $refreshTokenString;
54 1
        } else {
55 1
            $datetime = new \DateTime();
56 1
            $datetime->modify('+'.$this->ttl.' seconds');
57
58 1
            $refreshToken = $this->refreshTokenManager->create();
0 ignored issues
show
Bug introduced by
The property refreshTokenManager does not seem to exist. Did you mean userRefreshTokenManager?

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...
59 1
            $refreshToken->setUsername($user->getUsername());
60 1
            $refreshToken->setRefreshToken();
61 1
            $refreshToken->setValid($datetime);
62
63
//            $valid = false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
64
//            while (false === $valid) {
65
//                $valid = true;
66
//                $errors = $this->validator->validate($refreshToken);
67
//                if ($errors->count() > 0) {
68
//                    foreach ($errors as $error) {
69
//                        if ('refreshToken' === $error->getPropertyPath()) {
70
//                            $valid = false;
71
//                            $refreshToken->setRefreshToken();
72
//                        }
73
//                    }
74
//                }
75
//            }
76
77 1
            $this->refreshTokenManager->save($refreshToken);
0 ignored issues
show
Bug introduced by
The property refreshTokenManager does not seem to exist. Did you mean userRefreshTokenManager?

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...
78 1
            $data['refresh_token'] = $refreshToken->getRefreshToken();
79
        }
80
81 2
        $event->setData($data);
82 2
    }
83
}
84