|
1
|
|
|
<?php |
|
2
|
|
|
namespace Redbox\Twitch; |
|
3
|
|
|
use Redbox\Twitch\Transport\TransportInterface; |
|
4
|
|
|
use Redbox\Twitch\Transport\HttpRequest; |
|
5
|
|
|
use Redbox\Twitch\Transport\Http; |
|
6
|
|
|
use Redbox\Twitch\Commands; |
|
7
|
|
|
use Redbox\Twitch\Auth\AuthModel; |
|
8
|
|
|
|
|
9
|
|
|
class Client |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var TransportInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $transport; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $client_id; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $redirect_uri; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $client_secret; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var bool |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $force_relogin; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $access_token; |
|
41
|
|
|
|
|
42
|
|
|
/* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $api_url; |
|
46
|
|
|
|
|
47
|
|
|
// --- NEW -- |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $resources = []; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var Resource\Auth |
|
56
|
|
|
*/ |
|
57
|
|
|
public $auth; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var Resource\Game |
|
61
|
|
|
*/ |
|
62
|
|
|
public $games; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var Resource\Root |
|
66
|
|
|
*/ |
|
67
|
|
|
public $root; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var Resource\Teams |
|
71
|
|
|
*/ |
|
72
|
|
|
public $teams; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var Resource\Videos |
|
76
|
|
|
*/ |
|
77
|
|
|
public $videos; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var Resource\Ingests |
|
81
|
|
|
*/ |
|
82
|
|
|
public $ingests; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Client constructor. |
|
86
|
|
|
*/ |
|
87
|
|
|
public function __construct() |
|
88
|
|
|
{ |
|
89
|
|
|
$this->setApiUrl('https://api.twitch.tv/kraken'); |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
$this->auth = new Resource\Auth( |
|
93
|
|
|
$this, |
|
94
|
|
|
"Auth", |
|
95
|
|
|
array( |
|
96
|
|
|
'methods' => array( |
|
97
|
|
|
'requestAccessToken' => array( |
|
98
|
|
|
'path' => 'oauth2/token', |
|
99
|
|
|
'httpMethod' => HttpRequest::REQUEST_METHOD_POST, |
|
100
|
|
|
'requiresAuth'=> false, |
|
101
|
|
|
) |
|
102
|
|
|
) |
|
103
|
|
|
) |
|
104
|
|
|
); |
|
105
|
|
|
$this->games = new Resource\Games( |
|
|
|
|
|
|
106
|
|
|
$this, |
|
107
|
|
|
"Games", |
|
108
|
|
|
array( |
|
109
|
|
|
'methods' => array( |
|
110
|
|
|
'listTopGames' => array( |
|
111
|
|
|
'path' => 'games/top', |
|
112
|
|
|
'httpMethod' => HttpRequest::REQUEST_METHOD_GET, |
|
113
|
|
|
'requiresAuth'=> false, |
|
114
|
|
|
'parameters' => array ( |
|
115
|
|
|
'limit' => array ( |
|
116
|
|
|
'type' => 'integer', |
|
117
|
|
|
'min' => 0, |
|
118
|
|
|
'max' => 100, |
|
119
|
|
|
), |
|
120
|
|
|
'offset' => array ( |
|
121
|
|
|
'type' => 'integer', |
|
122
|
|
|
) |
|
123
|
|
|
) |
|
124
|
|
|
) |
|
125
|
|
|
) |
|
126
|
|
|
) |
|
127
|
|
|
); |
|
128
|
|
|
$this->root = new Resource\Root( |
|
129
|
|
|
$this, |
|
130
|
|
|
"Root", |
|
131
|
|
|
array( |
|
132
|
|
|
'methods' => array( |
|
133
|
|
|
'getRoot' => array( |
|
134
|
|
|
'path' => '/', |
|
135
|
|
|
'httpMethod' => HttpRequest::REQUEST_METHOD_GET, |
|
136
|
|
|
'requiresAuth'=> false, |
|
137
|
|
|
'parameters' => array() |
|
138
|
|
|
) |
|
139
|
|
|
) |
|
140
|
|
|
) |
|
141
|
|
|
); |
|
142
|
|
|
$this->ingests = new Resource\Ingests( |
|
143
|
|
|
$this, |
|
144
|
|
|
"Ingests", |
|
145
|
|
|
array( |
|
146
|
|
|
'methods' => array( |
|
147
|
|
|
'get' => array( |
|
148
|
|
|
'path' => '/ingests', |
|
149
|
|
|
'httpMethod' => HttpRequest::REQUEST_METHOD_GET, |
|
150
|
|
|
'requiresAuth'=> false, |
|
151
|
|
|
'parameters' => array() |
|
152
|
|
|
) |
|
153
|
|
|
) |
|
154
|
|
|
) |
|
155
|
|
|
); |
|
156
|
|
|
|
|
157
|
|
|
$this->teams = new Resource\Teams( |
|
158
|
|
|
$this, |
|
159
|
|
|
"Teams", |
|
160
|
|
|
array( |
|
161
|
|
|
'methods' => array( |
|
162
|
|
|
'listTeams' => array( |
|
163
|
|
|
'path' => '/teams', |
|
164
|
|
|
'httpMethod' => HttpRequest::REQUEST_METHOD_GET, |
|
165
|
|
|
'requiresAuth'=> false, |
|
166
|
|
|
'parameters' => array ( |
|
167
|
|
|
'limit' => array ( |
|
168
|
|
|
'type' => 'integer', |
|
169
|
|
|
'min' => 0, |
|
170
|
|
|
'max' => 100, |
|
171
|
|
|
), |
|
172
|
|
|
'offset' => array ( |
|
173
|
|
|
'type' => 'integer', |
|
174
|
|
|
) |
|
175
|
|
|
) |
|
176
|
|
|
), |
|
177
|
|
|
'getTeamByName' => array( |
|
178
|
|
|
'path' => '/teams/:team/', |
|
179
|
|
|
'httpMethod' => HttpRequest::REQUEST_METHOD_GET, |
|
180
|
|
|
'requiresAuth'=> false, |
|
181
|
|
|
'parameters' => array ( |
|
182
|
|
|
'team' => array ( |
|
183
|
|
|
'type' => 'string', |
|
184
|
|
|
'url_part' => true, |
|
185
|
|
|
) |
|
186
|
|
|
) |
|
187
|
|
|
) |
|
188
|
|
|
) |
|
189
|
|
|
) |
|
190
|
|
|
); |
|
191
|
|
|
$this->videos = new Resource\Videos( |
|
192
|
|
|
$this, |
|
193
|
|
|
"Videos", |
|
194
|
|
|
array( |
|
195
|
|
|
'methods' => array( |
|
196
|
|
|
'listTopVideos' => array( |
|
197
|
|
|
'path' => 'videos/top', |
|
198
|
|
|
'httpMethod' => HttpRequest::REQUEST_METHOD_GET, |
|
199
|
|
|
'requiresAuth'=> false, |
|
200
|
|
|
'parameters' => array ( |
|
201
|
|
|
'limit' => array ( |
|
202
|
|
|
'type' => 'integer', |
|
203
|
|
|
'min' => 0, |
|
204
|
|
|
'max' => 100, |
|
205
|
|
|
), |
|
206
|
|
|
'offset' => array ( |
|
207
|
|
|
'type' => 'integer', |
|
208
|
|
|
), |
|
209
|
|
|
'game' => array ( |
|
210
|
|
|
'type' => 'string', |
|
211
|
|
|
), |
|
212
|
|
|
'period' => array ( |
|
213
|
|
|
'type' => 'string', |
|
214
|
|
|
'restricted_value' => array( |
|
215
|
|
|
'week', 'month', 'all' |
|
216
|
|
|
) |
|
217
|
|
|
) |
|
218
|
|
|
) |
|
219
|
|
|
), |
|
220
|
|
|
'getVideo' => array( |
|
221
|
|
|
'path' => 'videos/:id/', |
|
222
|
|
|
'httpMethod' => HttpRequest::REQUEST_METHOD_GET, |
|
223
|
|
|
'requiresAuth'=> false, |
|
224
|
|
|
'parameters' => array ( |
|
225
|
|
|
'id' => array ( |
|
226
|
|
|
'type' => 'string', |
|
227
|
|
|
'url_part' => true, |
|
228
|
|
|
) |
|
229
|
|
|
) |
|
230
|
|
|
), |
|
231
|
|
|
'getChannelVideos' => array( |
|
232
|
|
|
'path' => 'channels/:channel/videos', |
|
233
|
|
|
'httpMethod' => HttpRequest::REQUEST_METHOD_GET, |
|
234
|
|
|
'requiresAuth'=> false, |
|
235
|
|
|
'parameters' => array ( |
|
236
|
|
|
'limit' => array ( |
|
237
|
|
|
'type' => 'integer', |
|
238
|
|
|
'min' => 0, |
|
239
|
|
|
'max' => 100, |
|
240
|
|
|
), |
|
241
|
|
|
'offset' => array ( |
|
242
|
|
|
'type' => 'integer', |
|
243
|
|
|
), |
|
244
|
|
|
'broadcasts' => array ( |
|
245
|
|
|
'type' => 'bool', |
|
246
|
|
|
), |
|
247
|
|
|
'hls' => array( |
|
248
|
|
|
'type' => 'bool' |
|
249
|
|
|
), |
|
250
|
|
|
'channel' => array ( |
|
251
|
|
|
'type' => 'string', |
|
252
|
|
|
'url_part' => true, |
|
253
|
|
|
) |
|
254
|
|
|
) |
|
255
|
|
|
) |
|
256
|
|
|
) |
|
257
|
|
|
) |
|
258
|
|
|
); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* @param string $name |
|
263
|
|
|
* @param Resource\ResourceAbstract $instance |
|
264
|
|
|
*/ |
|
265
|
|
|
public function registerResource($name="", Resource\ResourceAbstract $instance) { |
|
266
|
|
|
$this->resources[$name] = $instance; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* @return array |
|
271
|
|
|
*/ |
|
272
|
|
|
public function getSupportedFunctions() { |
|
273
|
|
|
return $this->resources; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/* -- Setters */ |
|
277
|
|
|
|
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* @param $client_id |
|
281
|
|
|
* @return $this |
|
282
|
|
|
*/ |
|
283
|
|
|
public function setClientId($client_id) |
|
284
|
|
|
{ |
|
285
|
|
|
$this->client_id = $client_id; |
|
286
|
|
|
return $this; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* @param $redirect_uri |
|
292
|
|
|
* @return $this |
|
293
|
|
|
*/ |
|
294
|
|
|
public function setRedirectUri($redirect_uri) |
|
295
|
|
|
{ |
|
296
|
|
|
$this->redirect_uri = $redirect_uri; |
|
297
|
|
|
return $this; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* @param $client_secret |
|
303
|
|
|
* @return $this |
|
304
|
|
|
*/ |
|
305
|
|
|
public function setClientSecret($client_secret) |
|
306
|
|
|
{ |
|
307
|
|
|
$this->client_secret = $client_secret; |
|
308
|
|
|
return $this; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* @param $api_url |
|
314
|
|
|
* @return $this |
|
315
|
|
|
*/ |
|
316
|
|
|
public function setApiUrl($api_url) |
|
317
|
|
|
{ |
|
318
|
|
|
$this->api_url = $api_url; |
|
319
|
|
|
return $this; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* @param $force_relogin |
|
325
|
|
|
* @return $this |
|
326
|
|
|
*/ |
|
327
|
|
|
public function setForceRelogin($force_relogin) |
|
328
|
|
|
{ |
|
329
|
|
|
$this->force_relogin = $force_relogin; |
|
330
|
|
|
return $this; |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* @param $access_token |
|
336
|
|
|
*/ |
|
337
|
|
|
public function setAccessToken($access_token) |
|
338
|
|
|
{ |
|
339
|
|
|
$this->access_token = $access_token; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
|
|
343
|
|
|
/* -- Getters */ |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* @return string |
|
347
|
|
|
*/ |
|
348
|
|
|
public function getClientId() |
|
349
|
|
|
{ |
|
350
|
|
|
return $this->client_id; |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* @return string |
|
355
|
|
|
*/ |
|
356
|
|
|
public function getRedirectUri() |
|
357
|
|
|
{ |
|
358
|
|
|
return $this->redirect_uri; |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
/** |
|
362
|
|
|
* @return string |
|
363
|
|
|
*/ |
|
364
|
|
|
public function getClientSecret() |
|
365
|
|
|
{ |
|
366
|
|
|
return $this->client_secret; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
/** |
|
370
|
|
|
* @return string |
|
371
|
|
|
*/ |
|
372
|
|
|
public function getApiUrl() |
|
373
|
|
|
{ |
|
374
|
|
|
return $this->api_url; |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
/** |
|
378
|
|
|
* @return boolean |
|
379
|
|
|
*/ |
|
380
|
|
|
public function isForceRelogin() |
|
381
|
|
|
{ |
|
382
|
|
|
return $this->force_relogin; |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
/** |
|
386
|
|
|
* @return string |
|
387
|
|
|
*/ |
|
388
|
|
|
public function getAccessToken() |
|
389
|
|
|
{ |
|
390
|
|
|
return $this->access_token; |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
/** |
|
394
|
|
|
* @return AuthModel |
|
395
|
|
|
*/ |
|
396
|
|
|
public function getAuthModel() { |
|
397
|
|
|
return new AuthModel($this); |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
/** |
|
401
|
|
|
* Get transport |
|
402
|
|
|
* |
|
403
|
|
|
* @return TransportInterface Transport |
|
404
|
|
|
*/ |
|
405
|
|
|
public function getTransport() |
|
406
|
|
|
{ |
|
407
|
|
|
// Set transport if haven't |
|
408
|
|
|
if ($this->transport === null) { |
|
409
|
|
|
$this->setTransport(new Http($this)); |
|
410
|
|
|
} |
|
411
|
|
|
return $this->transport; |
|
412
|
|
|
} |
|
413
|
|
|
|
|
414
|
|
|
/** |
|
415
|
|
|
* Set transport |
|
416
|
|
|
* |
|
417
|
|
|
* @param TransportInterface $transport Transport |
|
418
|
|
|
* |
|
419
|
|
|
* @return self This object |
|
420
|
|
|
*/ |
|
421
|
|
|
public function setTransport(TransportInterface $transport) |
|
422
|
|
|
{ |
|
423
|
|
|
$this->transport = $transport; |
|
424
|
|
|
return $this; |
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..