Token::getType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace XoopsModules\Wggithub\Github\OAuth;
4
5
use XoopsModules\Wggithub\Github;
6
7
8
/**
9
 * OAuth token envelope.
10
 *
11
 * @author  Miloslav Hůla (https://github.com/milo)
12
 */
13
class Token extends Github\Sanity
14
{
15
    /** @var string */
16
    private $value;
17
18
    /** @var string */
19
    private $type;
20
21
    /** @var string[] */
22
    private $scopes;
23
24
25
    /**
26
     * @param  string
27
     * @param  string
28
     * @param  string[]
29
     */
30
    public function __construct($value, $type = '', array $scopes = [])
31
    {
32
        $this->value = $value;
33
        $this->type = $type;
34
        $this->scopes = $scopes;
35
    }
36
37
38
    /**
39
     * @return string
40
     */
41
    public function getValue()
42
    {
43
        return $this->value;
44
    }
45
46
47
    /**
48
     * @return string
49
     */
50
    public function getType()
51
    {
52
        return $this->type;
53
    }
54
55
56
    /**
57
     * @return string[]
58
     */
59
    public function getScopes()
60
    {
61
        return $this->scopes;
62
    }
63
64
65
    /**
66
     * @see https://developer.github.com/v3/oauth/#scopes
67
     *
68
     * @param  string
69
     * @return bool
70
     */
71
    public function hasScope($scope)
72
    {
73
        if (\in_array($scope, $this->scopes, TRUE)) {
74
            return TRUE;
75
        }
76
77
        static $superiors = [
78
            'user:email' => 'user',
79
            'user:follow' => 'user',
80
            'notifications' => 'repo',
81
        ];
82
83
        if (\array_key_exists($scope, $superiors) && \in_array($superiors[$scope], $this->scopes, TRUE)) {
84
            return TRUE;
85
        }
86
87
        return FALSE;
88
    }
89
90
91
    /** @internal */
92
    public function toArray()
93
    {
94
        return [
95
            'value' => $this->value,
96
            'type' => $this->type,
97
            'scopes' => $this->scopes,
98
        ];
99
    }
100
101
102
    /** @internal
103
     * @param array $data
104
     * @return Token
105
     */
106
    public static function createFromArray(array $data)
107
    {
108
        return new static($data['value'], $data['type'], $data['scopes']);
109
    }
110
111
}
112