Completed
Push — develop ( 5a11da...78fd78 )
by Raphael De
02:33
created

AbstractToken   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 81
ccs 0
cts 34
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A getHash() 0 4 1
A getExpireIn() 0 4 1
A getAccount() 0 4 1
A getApplication() 0 4 1
A getRoles() 0 4 1
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 AbstractToken implements TokenInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $hash;
22
23
    /**
24
     * @var int
25
     */
26
    protected $expireIn;
27
28
    /**
29
     * @var AccountInterface
30
     */
31
    protected $account;
32
33
    /**
34
     * @var ApplicationInterface
35
     */
36
    protected $application;
37
38
    /**
39
     * @see AccessTokenInterface::__construct()
40
     */
41
    public function __construct(
42
        ApplicationInterface $application,
43
        AccountInterface $account = null,
44
        $expireIn = AccessTokenInterface::DEFAULT_TTL,
45
        $hash = null
46
    ) {
47
        $this->application = $application;
48
        $this->account = $account;
49
        $this->expireIn = $expireIn;
50
51
        $this->hash = $hash ?: (new MessageDigestPasswordEncoder())->encodePassword(
52
            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...
53
            uniqid(mt_rand(), true)
54
        );
55
    }
56
57
    /**
58
     * @see AccessTokenInterface::getHash()
59
     */
60
    public function getHash()
61
    {
62
        return $this->hash;
63
    }
64
65
    /**
66
     * @see AccessTokenInterface::getExpireIn()
67
     */
68
    public function getExpireIn()
69
    {
70
        return $this->expireIn;
71
    }
72
73
    /**
74
     * @see AccessTokenInterface::getAccount()
75
     */
76
    public function getAccount()
77
    {
78
        return $this->account;
79
    }
80
81
    /**
82
     * @see AccessTokenInterface::getApplication()
83
     */
84
    public function getApplication()
85
    {
86
        return $this->application;
87
    }
88
89
    /**
90
     * @see AccessTokenInterface::getRoles()
91
     */
92
    public function getRoles()
93
    {
94
        return array_intersect($this->account->getRoles(), $this->application->getRoles());
95
    }
96
}
97