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