Completed
Push — orm ( df18d5...6592b1 )
by
unknown
02:37
created

Token::getRoles()   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
use Majora\Framework\Model\CollectionableInterface;
11
use Majora\Framework\Model\CollectionableTrait;
12
use Majora\Framework\Serializer\Model\SerializableTrait;
13
14
/**
15
 * Abstract token class.
16
 *
17
 * @author Raphael De Freitas <[email protected]>
18
 */
19
abstract class Token implements TokenInterface, CollectionableInterface
20
{
21
    use CollectionableTrait, SerializableTrait;
22
23
    /**
24
     * @var int
25
     */
26
    protected $id;
27
28
    /**
29
     * @var string
30
     */
31
    protected $hash;
32
33
    /**
34
     * @var int
35
     */
36
    protected $expireIn;
37
38
    /**
39
     * @var AccountInterface
40
     */
41
    protected $account;
42
43
    /**
44
     * @var ApplicationInterface
45
     */
46
    protected $application;
47
48
    /**
49
     * @see AccessTokenInterface::__construct()
50
     */
51
    public function __construct(
52
        ApplicationInterface $application,
53
        AccountInterface $account = null,
54
        $expireIn = AccessTokenInterface::DEFAULT_TTL,
55
        $hash = null
56
    ) {
57
        $this->application = $application;
58
        $this->account = $account;
59
        $this->expireIn = $expireIn;
60
61
        $this->hash = $hash ?: (new MessageDigestPasswordEncoder())->encodePassword(
62
            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...
63
            uniqid(mt_rand(), true)
64
        );
65
    }
66
67
    public function getId()
68
    {
69
        return $this->id;
70
    }
71
72
    /**
73
     * @see AccessTokenInterface::getHash()
74
     */
75
    public function getHash()
76
    {
77
        return $this->hash;
78
    }
79
80
    /**
81
     * @see AccessTokenInterface::getExpireIn()
82
     */
83
    public function getExpireIn()
84
    {
85
        return $this->expireIn;
86
    }
87
88
    /**
89
     * @see AccessTokenInterface::getAccount()
90
     */
91
    public function getAccount()
92
    {
93
        return $this->account;
94
    }
95
96
    /**
97
     * @see AccessTokenInterface::getApplication()
98
     */
99
    public function getApplication()
100
    {
101
        return $this->application;
102
    }
103
104
    /**
105
     * @see AccessTokenInterface::getRoles()
106
     */
107
    public function getRoles()
108
    {
109
        return array_intersect($this->account->getRoles(), $this->application->getRoles());
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115
    public static function getScopes()
116
    {
117
        return array(
118
            'identifier' => array('id', 'hash'),
119
        );
120
    }
121
}
122