Completed
Push — release-v2.1 ( d04733...1a7b2b )
by Quentin
03:24
created

Token::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
ccs 0
cts 14
cp 0
rs 9.4285
cc 3
eloc 11
nc 2
nop 4
crap 12
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 AccountInterface
32
     */
33
    protected $account;
34
35
    /**
36
     * @var ApplicationInterface
37
     */
38
    protected $application;
39
40
    /**
41
     * @see TokenInterface::__construct()
42
     */
43
    public function __construct(
44
        ApplicationInterface $application,
45
        AccountInterface $account = null,
46
        $expireIn = TokenInterface::DEFAULT_TTL,
47
        $hash = null
48
    ) {
49
        $this->application = $application;
50
        $this->account = $account;
51
        $this->expireIn = $expireIn;
52
53
        $this->hash = $hash ?: (new MessageDigestPasswordEncoder())->encodePassword(
54
            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...
55
            uniqid(mt_rand(), true)
56
        );
57
    }
58
59
    /**
60
     * @see TokenInterface::__toString()
61
     */
62
    public function __toString()
63
    {
64
        return $this->hash;
65
    }
66
67
    public function getId()
68
    {
69
        return $this->id;
70
    }
71
72
    /**
73
     * @see TokenInterface::getHash()
74
     */
75
    public function getHash()
76
    {
77
        return $this->hash;
78
    }
79
80
    /**
81
     * @see TokenInterface::getExpireIn()
82
     */
83
    public function getExpireIn()
84
    {
85
        return $this->expireIn;
86
    }
87
88
    /**
89
     * @see TokenInterface::getAccount()
90
     */
91
    public function getAccount()
92
    {
93
        return $this->account;
94
    }
95
96
    /**
97
     * @see TokenInterface::getApplication()
98
     */
99
    public function getApplication()
100
    {
101
        return $this->application;
102
    }
103
104
    /**
105
     * @see TokenInterface::getRoles()
106
     */
107
    public function getRoles()
108
    {
109
        return array_intersect($this->account->getRoles(), $this->application->getRoles());
110
    }
111
}
112