Passed
Push — master ( 0deb8f...f54565 )
by Goffy
04:13
created

Token   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 93
rs 10
c 0
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 6 1
A __construct() 0 5 1
A getScopes() 0 3 1
A hasScope() 0 17 4
A getType() 0 3 1
A getValue() 0 3 1
A createFromArray() 0 3 1
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
	public static function createFromArray(array $data)
104
	{
105
		return new static($data['value'], $data['type'], $data['scopes']);
106
	}
107
108
}
109