Completed
Push — develop ( 78fd78...600bfc )
by Quentin
02:41
created

Token::getHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Majora\Component\OAuth\Entity;
4
5
use Majora\Component\OAuth\Model\AccessTokenInterface;
6
use Majora\Component\OAuth\Model\AccountInterface;
7
use Majora\Component\OAuth\Model\ApplicationInterface;
8
use Majora\Component\OAuth\Model\TokenInterface;
9
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
10
11
/**
12
 * Abstract token class.
13
 */
14
abstract class Token implements TokenInterface
15
{
16
    /**
17
     * @var int
18
     */
19
    protected $id;
20
21
    /**
22
     * @var string
23
     */
24
    protected $hash;
25
26
    /**
27
     * @var int
28
     */
29
    protected $expireIn;
30
31
    /**
32
     * @var AccountInterface
33
     */
34
    protected $account;
35
36
    /**
37
     * @var ApplicationInterface
38
     */
39
    protected $application;
40
41
    /**
42
     * @see AccessTokenInterface::__construct()
43
     */
44
    public function __construct(
45
        ApplicationInterface $application,
46
        AccountInterface $account = null,
47
        $expireIn = AccessTokenInterface::DEFAULT_TTL,
48
        $hash = null
49
    ) {
50
        $this->application = $application;
51
        $this->account = $account;
52
        $this->expireIn = $expireIn;
53
54
        $this->hash = $hash ?: (new MessageDigestPasswordEncoder())->encodePassword(
55
            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...
56
            uniqid(mt_rand(), true)
57
        );
58
    }
59
60
    public function getId()
61
    {
62
        return $this->id;
63
    }
64
65
    /**
66
     * @see AccessTokenInterface::getHash()
67
     */
68
    public function getHash()
69
    {
70
        return $this->hash;
71
    }
72
73
    /**
74
     * @see AccessTokenInterface::getExpireIn()
75
     */
76
    public function getExpireIn()
77
    {
78
        return $this->expireIn;
79
    }
80
81
    /**
82
     * @see AccessTokenInterface::getAccount()
83
     */
84
    public function getAccount()
85
    {
86
        return $this->account;
87
    }
88
89
    /**
90
     * @see AccessTokenInterface::getApplication()
91
     */
92
    public function getApplication()
93
    {
94
        return $this->application;
95
    }
96
97
    /**
98
     * @see AccessTokenInterface::getRoles()
99
     */
100
    public function getRoles()
101
    {
102
        return array_intersect($this->account->getRoles(), $this->application->getRoles());
103
    }
104
}
105