AccessToken::getToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BPCI\SumUp\OAuth;
4
5
class AccessToken
6
{
7
    private $token;
8
    private $type;
9
    private $expiration;
10
    private $scope = [
11
        'payments',
12
        'user.app-settings',
13
        'transactions.history',
14
        'user.profile_readonly'
15
    ];
16
17
    /**
18
     * @param string $token
19
     * @param string $type
20
     * @param integer $expiresIn
21
     * @param string|null $scope
22
     */
23 5
    public function __construct(string $token, string $type, int $expiresIn, string $scope = null)
24
    {
25 5
        $this->token = $token;
26 5
        $this->type = $type;
27 5
        $this->expiration = time() + $expiresIn;
28
29 5
        if($scope !== null) {
30
            $this->scope = $scope;
31
        }
32 5
    }
33
34
    /**
35
     * Check if token is valid
36
     *
37
     * @return boolean
38
     */
39 5
    public function isValid(): bool
40
    {
41 5
        return $this->getToken() !== '' && time() < $this->expiration;
42
    }
43
44
    /**
45
     * getToken
46
     *
47
     * @return string
48
     */
49 5
    public function getToken(): string
50
    {
51 5
        return $this->token;
52
    }
53
54
    /**
55
     * getType
56
     *
57
     * @return string
58
     */
59 5
    public function getType(): string
60
    {
61 5
        return $this->type;
62
    }
63
64
    /**
65
     * Get Expiration
66
     *
67
     * @return integer
68
     */
69
    public function getExpiration(): int
70
    {
71
        return $this->expiration;
72
    }
73
74
    /**
75
     * Get scope
76
     *
77
     * @return array
78
     */
79
    public function getScope(): array
80
    {
81
        return $this->scope;
82
    }
83
84
    /**
85
     * Check if token can access one or more scope
86
     *
87
     * @param string $scope[,$scope[,$scope[,...]]]
88
     * @return boolean
89
     */
90
    public function canAccess(string $scope)
91
    {
92
        $scopes = func_get_args();
93
        $diff = array_diff($scopes, $this->scope);
94
        return count($diff)>0;
95
    }
96
}
97