Token   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 34.62%

Importance

Changes 3
Bugs 1 Features 3
Metric Value
wmc 12
c 3
b 1
f 3
lcom 1
cbo 3
dl 0
loc 114
rs 10
ccs 9
cts 26
cp 0.3462

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 4 1
A getId() 0 4 1
A getHash() 0 4 1
A getExpireIn() 0 4 1
A getAccount() 0 4 1
A getApplication() 0 4 1
A getRoles() 0 4 1
A __construct() 0 17 4
A getExpireAt() 0 4 1
1
<?php
2
3
namespace Majora\Component\OAuth\Entity;
4
5
use Majora\Component\OAuth\Model\TokenInterface;
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
 * Abstract token class.
12
 */
13
abstract class Token implements TokenInterface
14
{
15
    /**
16
     * @var int
17
     */
18
    protected $id;
19
20
    /**
21
     * @var string
22
     */
23
    protected $hash;
24
25
    /**
26
     * @var int
27
     */
28
    protected $expireIn;
29
30
    /**
31
     * @var \DateTime
32
     */
33
    protected $expireAt;
34
35
    /**
36
     * @var AccountInterface
37
     */
38
    protected $account;
39
40
    /**
41
     * @var ApplicationInterface
42
     */
43
    protected $application;
44
45
    /**
46
     * @see TokenInterface::__construct()
47
     */
48 12
    public function __construct(
49
        ApplicationInterface $application,
50
        AccountInterface $account = null,
51
        $expireIn = TokenInterface::DEFAULT_TTL,
52
        \DateTime $expireAt = null,
53
        $hash = null
54
    ) {
55 12
        $this->application = $application;
56 12
        $this->account = $account;
57 12
        $this->expireIn = $expireIn;
58 12
        $this->expireAt = $expireAt ?: \DateTime::createFromFormat('U', time() + intval($expireIn));
0 ignored issues
show
Documentation Bug introduced by
It seems like $expireAt ?: \DateTime::...() + intval($expireIn)) can also be of type false. However, the property $expireAt is declared as type object<DateTime>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
59
60 12
        $this->hash = $hash ?: (new MessageDigestPasswordEncoder())->encodePassword(
61
            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...
62
            uniqid(mt_rand(), true)
63
        );
64 12
    }
65
66
    /**
67
     * @see TokenInterface::__toString()
68
     */
69
    public function __toString()
70
    {
71
        return $this->hash;
72
    }
73
74
    public function getId()
75
    {
76
        return $this->id;
77
    }
78
79
    /**
80
     * @see TokenInterface::getHash()
81
     */
82
    public function getHash()
83
    {
84
        return $this->hash;
85
    }
86
87
    /**
88
     * @see TokenInterface::getExpireIn()
89
     */
90 12
    public function getExpireIn()
91
    {
92 12
        return $this->expireIn;
93
    }
94
95
    /**
96
     * @see TokenInterface::getExpireAt()
97
     */
98
    public function getExpireAt()
99
    {
100
        return $this->expireAt;
101
    }
102
103
    /**
104
     * @see TokenInterface::getAccount()
105
     */
106
    public function getAccount()
107
    {
108
        return $this->account;
109
    }
110
111
    /**
112
     * @see TokenInterface::getApplication()
113
     */
114
    public function getApplication()
115
    {
116
        return $this->application;
117
    }
118
119
    /**
120
     * @see TokenInterface::getRoles()
121
     */
122
    public function getRoles()
123
    {
124
        return array_intersect($this->account->getRoles(), $this->application->getRoles());
125
    }
126
}
127