Completed
Push — develop ( 697ba9...9ec113 )
by Raphael De
02:34
created

AccessToken::getAccount()   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 0
Metric Value
c 1
b 0
f 0
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\RefreshTokenInterface;
9
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
10
11
/**
12
 * Access token class.
13
 */
14
class AccessToken implements AccessTokenInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $hash;
20
21
    /**
22
     * @var int
23
     */
24
    protected $expireIn;
25
26
    /**
27
     * @var AccountInterface
28
     */
29
    protected $account;
30
31
    /**
32
     * @var ApplicationInterface
33
     */
34
    protected $application;
35
36
    /**
37
     * @var RefreshTokenInterface
38
     */
39
    protected $refreshToken;
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
        RefreshTokenInterface $refreshToken = null
50
    ) {
51
        $this->application = $application;
52
        $this->account = $account;
53
        $this->expireIn = $expireIn;
54
        $this->refreshToken = $refreshToken;
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
    /**
63
     * @see AccessTokenInterface::getHash()
64
     */
65
    public function getHash()
66
    {
67
        return $this->hash;
68
    }
69
70
    /**
71
     * @see AccessTokenInterface::getExpireIn()
72
     */
73
    public function getExpireIn()
74
    {
75
        return $this->expireIn;
76
    }
77
78
    /**
79
     * @see AccessTokenInterface::getAccount()
80
     */
81
    public function getAccount()
82
    {
83
        return $this->account;
84
    }
85
86
    /**
87
     * @see AccessTokenInterface::getApplication()
88
     */
89
    public function getApplication()
90
    {
91
        return $this->application;
92
    }
93
94
    /**
95
     * @see AccessTokenInterface::getRoles()
96
     */
97
    public function getRoles()
98
    {
99
        return array_intersect($this->account->getRoles(), $this->application->getRoles());
100
    }
101
102
    /**
103
     * @see AccessTokenInterface::getRefreshToken()
104
     */
105
    public function getRefreshToken()
106
    {
107
        return $this->refreshToken;
108
    }
109
110
111
}
112