|
1
|
|
|
<?php |
|
2
|
|
|
namespace Redbox\Twitch\Resource; |
|
3
|
|
|
use Redbox\Twitch\Exception; |
|
4
|
|
|
use Redbox\Twitch\Client; |
|
5
|
|
|
use Redbox\Twitch\Transport\HttpRequest; |
|
6
|
|
|
|
|
7
|
|
|
class ResourceAbstract |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var array |
|
11
|
|
|
*/ |
|
12
|
|
|
private $methods; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var Client |
|
16
|
|
|
*/ |
|
17
|
|
|
private $client; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $resource_name; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
private $url_parts = array(); |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
public function __construct(Client $client = null, $resource_name = "", $declaration = []) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->client = $client; |
|
34
|
|
|
$this->resource_name = $resource_name; |
|
35
|
|
|
$this->methods = isset($declaration['methods']) ? $declaration['methods'] : []; |
|
36
|
|
|
|
|
37
|
|
|
// If this line gives errros .. Comment it out because somewhere in the resource it self is an error. |
|
38
|
|
|
// $client->registerResource($this->resource_name, $this); |
|
39
|
|
|
// Todo validate resources |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
private function validate_arguments($method_name = '', $args = array()) |
|
43
|
|
|
{ |
|
44
|
|
|
if (isset($this->methods[$method_name]) == true) { |
|
|
|
|
|
|
45
|
|
|
$method = $this->methods[$method_name]; |
|
46
|
|
|
if (isset($method['parameters']) == true and is_array($method['parameters']) == true) { |
|
|
|
|
|
|
47
|
|
|
$parameters = $method['parameters']; |
|
48
|
|
|
foreach ($parameters as $name => $options) { |
|
49
|
|
|
switch ($options['type']) { |
|
50
|
|
|
case 'integer': |
|
51
|
|
|
// todo implment default value as seen in team |
|
52
|
|
|
|
|
53
|
|
|
// Required |
|
54
|
|
|
if (isset($options['required']) == true and $options['required'] == true and isset($args[$name]) == false) |
|
|
|
|
|
|
55
|
|
|
throw new Exception\RuntimeException($this->resource_name . ' requires parameter ' . $name . ' to be given for method ' . $method); |
|
56
|
|
|
|
|
57
|
|
|
// Min |
|
58
|
|
|
if (isset($options['min']) == true and (isset($args[$name]) == true and $args[$name] < $options['min'])) |
|
|
|
|
|
|
59
|
|
|
throw new Exception\RuntimeException($this->resource_name . ' requires parameter ' . $name . ' to have a minimum value of ' . $options['min'] . ' for method ' . $method); |
|
60
|
|
|
|
|
61
|
|
|
// Min |
|
62
|
|
|
if (isset($options['max']) == true and (isset($args[$name]) == true and $args[$name] > $options['max'])) |
|
|
|
|
|
|
63
|
|
|
throw new Exception\RuntimeException($this->resource_name . ' requires parameter ' . $name . ' to have a maximum value of ' . $options['min'] . ' for method ' . $method); |
|
64
|
|
|
|
|
65
|
|
|
break; |
|
66
|
|
|
case 'string': |
|
67
|
|
|
|
|
68
|
|
|
// Required |
|
69
|
|
|
if (isset($options['required']) == true and $options['required'] == true and isset($args[$name]) == false) |
|
|
|
|
|
|
70
|
|
|
throw new Exception\RuntimeException($this->resource_name . ' requires parameter ' . $name . ' to be given for method ' . $method); |
|
71
|
|
|
|
|
72
|
|
|
// Url Part |
|
73
|
|
|
if (isset($options['url_part']) == true and $options['url_part'] == true and isset($args[$name]) == false) { |
|
|
|
|
|
|
74
|
|
|
throw new Exception\RuntimeException($this->resource_name . ' requires parameter ' . $name . ' to be given for method ' . $method); |
|
75
|
|
|
} else if (isset($options['url_part']) == true and $options['url_part'] == true and isset($args[$name]) == true) { |
|
|
|
|
|
|
76
|
|
|
$this->url_parts[$method_name][':' . $name] = array('name' => $name, 'value' => $args[$name]); |
|
77
|
|
|
} |
|
78
|
|
|
break; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function call($method, $arguments = array(), $body = array()) |
|
87
|
|
|
{ |
|
88
|
|
|
if ($this->validate_arguments($method, $arguments) == true) |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
|
|
91
|
|
|
$headers = array(); |
|
92
|
|
|
$headers['Accept'] = 'application/vnd.twitchtv.v3+json'; |
|
93
|
|
|
$headers['Client-ID'] = $this->client->getClientId(); |
|
94
|
|
|
|
|
95
|
|
|
if (isset($this->methods[$method]['requiresAuth']) == true) { |
|
|
|
|
|
|
96
|
|
|
if ($this->methods[$method]['requiresAuth'] == true && !$this->client->getAccessToken()) { |
|
97
|
|
|
throw Exception\AuthorizationRequiredException('Method: '.$method.' requires authorization. Did you forget to use setAccessToken() ?'); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if ($this->client->getAccessToken()) |
|
102
|
|
|
$headers['Authorization'] = 'OAuth ' . $this->client->getAccessToken(); |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
if (isset($this->url_parts[$method]) == true and is_array($this->url_parts[$method]) == true) { |
|
|
|
|
|
|
106
|
|
|
if (count($this->url_parts[$method]) > 0) { |
|
107
|
|
|
foreach ($this->url_parts[$method] as $key => $url_part) { |
|
108
|
|
|
$this->methods[$method]['path'] = str_replace($key, $url_part['value'], $this->methods[$method]['path']); |
|
109
|
|
|
unset($arguments[$url_part['name']]); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$request = new HttpRequest( |
|
115
|
|
|
'/'.$this->methods[$method]['path'], |
|
116
|
|
|
$this->methods[$method]['httpMethod'], |
|
117
|
|
|
$headers, |
|
118
|
|
|
$body |
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
|
|
$response = $this->client->getTransport()->sendRequest( |
|
122
|
|
|
$request |
|
123
|
|
|
); |
|
124
|
|
|
|
|
125
|
|
|
return $response; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function getClient() { |
|
130
|
|
|
return $this->client; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function getResourceName() { |
|
134
|
|
|
return $this->resource_name; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function getMethods() { |
|
138
|
|
|
return $this->methods; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
} |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.