Completed
Push — develop ( 098743...5f7eab )
by Raphael De
02:19
created

RefreshToken   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 3
dl 81
loc 81
ccs 0
cts 34
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 15 15 3
A getHash() 4 4 1
A getExpireIn() 4 4 1
A getAccount() 4 4 1
A getApplication() 4 4 1
A getRoles() 4 4 1

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
namespace Majora\Component\OAuth\Entity;
4
5
use Majora\Component\OAuth\Model\RefreshTokenInterface;
6
use Majora\Component\OAuth\Model\AccountInterface;
7
use Majora\Component\OAuth\Model\ApplicationInterface;
8
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
9
10
/**
11
 * Class RefreshToken is the default implementation of RefreshTokenInterface
12
 *
13
 * @author Raphael De Freitas <[email protected]>
14
 */
15 View Code Duplication
class RefreshToken implements RefreshTokenInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $hash;
21
22
    /**
23
     * @var int
24
     */
25
    protected $expireIn;
26
27
    /**
28
     * @var AccountInterface
29
     */
30
    protected $account;
31
32
    /**
33
     * @var ApplicationInterface
34
     */
35
    protected $application;
36
37
    /**
38
     * @see RefreshTokenInterface::__construct()
39
     */
40
    public function __construct(
41
        ApplicationInterface $application,
42
        AccountInterface $account = null,
43
        $expireIn = RefreshTokenInterface::DEFAULT_TTL,
44
        $hash = null
45
    ) {
46
        $this->application = $application;
47
        $this->account = $account;
48
        $this->expireIn = $expireIn;
49
50
        $this->hash = $hash ?: (new MessageDigestPasswordEncoder())->encodePassword(
51
            sprintf('[%s\o/%s]', $application->getSecret(), $account->getPassword() ?: time()),
0 ignored issues
show
Bug introduced by
It seems like $account is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
52
            uniqid(mt_rand(), true)
53
        );
54
    }
55
56
    /**
57
     * @see RefreshTokenInterface::getHash()
58
     */
59
    public function getHash()
60
    {
61
        return $this->hash;
62
    }
63
64
    /**
65
     * @see RefreshTokenInterface::getExpireIn()
66
     */
67
    public function getExpireIn()
68
    {
69
        return $this->expireIn;
70
    }
71
72
    /**
73
     * @see RefreshTokenInterface::getAccount()
74
     */
75
    public function getAccount()
76
    {
77
        return $this->account;
78
    }
79
80
    /**
81
     * @see RefreshTokenInterface::getApplication()
82
     */
83
    public function getApplication()
84
    {
85
        return $this->application;
86
    }
87
88
    /**
89
     * @see RefreshTokenInterface::getRoles()
90
     */
91
    public function getRoles()
92
    {
93
        return array_intersect($this->account->getRoles(), $this->application->getRoles());
94
    }
95
}
96