Completed
Push — orm ( 49864f...4bdd44 )
by
unknown
02:26
created

Token::getScopes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

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 6
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 3
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
 * @author Raphael De Freitas <[email protected]>
15
 */
16
abstract class Token implements TokenInterface
17
{
18
    /**
19
     * @var int
20
     */
21
    protected $id;
22
23
    /**
24
     * @var string
25
     */
26
    protected $hash;
27
28
    /**
29
     * @var int
30
     */
31
    protected $expireIn;
32
33
    /**
34
     * @var AccountInterface
35
     */
36
    protected $account;
37
38
    /**
39
     * @var ApplicationInterface
40
     */
41
    protected $application;
42
43
    /**
44
     * @see AccessTokenInterface::__construct()
45
     */
46
    public function __construct(
47
        ApplicationInterface $application,
48
        AccountInterface $account = null,
49
        $expireIn = AccessTokenInterface::DEFAULT_TTL,
50
        $hash = null
51
    ) {
52
        $this->application = $application;
53
        $this->account = $account;
54
        $this->expireIn = $expireIn;
55
56
        $this->hash = $hash ?: (new MessageDigestPasswordEncoder())->encodePassword(
57
            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...
58
            uniqid(mt_rand(), true)
59
        );
60
    }
61
62
    public function getId()
63
    {
64
        return $this->id;
65
    }
66
67
    /**
68
     * @see AccessTokenInterface::getHash()
69
     */
70
    public function getHash()
71
    {
72
        return $this->hash;
73
    }
74
75
    /**
76
     * @see AccessTokenInterface::getExpireIn()
77
     */
78
    public function getExpireIn()
79
    {
80
        return $this->expireIn;
81
    }
82
83
    /**
84
     * @see AccessTokenInterface::getAccount()
85
     */
86
    public function getAccount()
87
    {
88
        return $this->account;
89
    }
90
91
    /**
92
     * @see AccessTokenInterface::getApplication()
93
     */
94
    public function getApplication()
95
    {
96
        return $this->application;
97
    }
98
99
    /**
100
     * @see AccessTokenInterface::getRoles()
101
     */
102
    public function getRoles()
103
    {
104
        return array_intersect($this->account->getRoles(), $this->application->getRoles());
105
    }
106
}
107