Completed
Push — master ( 33fd23...1adc69 )
by Johnny
05:23
created

Client::setScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
namespace Redbox\Twitch;
3
use Redbox\Twitch\Auth\AuthModel;
4
use Redbox\Twitch\Transport\Http;
5
use Redbox\Twitch\Transport\HttpRequest;
6
use Redbox\Twitch\Transport\TransportInterface;
7
8
class Client
9
{
10
11
    /**
12
     * @var TransportInterface
13
     */
14
    protected $transport;
15
16
    /**
17
     * @var string
18
     */
19
    protected $client_id;
20
21
    /**
22
     * @var string
23
     */
24
    protected $redirect_uri;
25
26
    /**
27
     * @var string
28
     */
29
    protected $client_secret;
30
31
    /**
32
     * @var bool
33
     */
34
    protected $force_relogin;
35
36
    /**
37
     * @var string
38
     */
39
    protected $access_token;
40
41
    /**
42
     * @var array
43
     */
44
    protected $scope = [];
45
46
    /**
47
     * @var string
48
     */
49
    protected $api_url;
50
51
    // --- NEW --
52
53
    /**
54
     * @var array
55
     */
56
    protected $resources = [];
57
58
    /**
59
     * @var Resource\Auth
60
     */
61
    public $auth;
62
63
    /**
64
     * @var Resource\Games
65
     */
66
    public $games;
67
68
    /**
69
     * @var Resource\Root
70
     */
71
    public $root;
72
73
    /**
74
     * @var Resource\Teams
75
     */
76
    public $teams;
77
78
    /**
79
     * @var Resource\Videos
80
     */
81
    public $videos;
82
83
    /**
84
     * @var Resource\Ingests
85
     */
86
    public $ingests;
87
88
    /**
89
     * @var Resource\Users
90
     */
91
    public $users;
92
93
    /**
94
     * @var Resource\Chat
95
     */
96
    public $chat;
97
98
    /**
99
     * @var Resource\Streams
100
     */
101
    public $streams;
102
103
    /**
104
     * @var Resource\Channels
105
     */
106
    public $channels;
107
108
    /**
109
     * Client constructor.
110
     */
111
    public function __construct()
112
    {
113
        $this->setApiUrl('https://api.twitch.tv/kraken');
114
115
116
        $this->auth = new Resource\Auth(
117
            $this,
118
            "Auth",
119
            array(
120
                'methods' => array(
121
                    'requestAccessToken' => array(
122
                        'path'         => 'oauth2/token',
123
                        'httpMethod'   => HttpRequest::REQUEST_METHOD_POST,
124
                        'requiresAuth' => false,
125
                        'requireScope' => array(),
126
                    )
127
                )
128
            )
129
        );
130
        $this->users = new Resource\Users(
131
            $this,
132
            "Users",
133
            array(
134
                'methods' => array(
135
                    'getUserByUsername' => array(
136
                        'path'        => 'users/:user',
137
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
138
                        'requiresAuth'=> false,
139
                        'parameters'  => array (
140
                            'user'   => array (
141
                                'type'      => 'string',
142
                                'url_part'  => true,
143
                            )
144
                        )
145
                    ),
146
                    'getUser' => array(
147
                        'path'        => 'user',
148
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
149
                        'requiresAuth'=> true,
150
                        'parameters'  => array()
151
                    )
152
                )
153
            )
154
        );
155
        $this->games = new Resource\Games(
156
            $this,
157
            "Games",
158
            array(
159
                'methods' => array(
160
                    'listTopGames' => array(
161
                        'path'        => 'games/top',
162
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
163
                        'requiresAuth'=> false,
164
                        'parameters'  => array (
165
                            'limit'   => array (
166
                                'type'    => 'integer',
167
                                'min'     => 0,
168
                                'max'     => 100,
169
                            ),
170
                            'offset'  => array (
171
                                'type'    => 'integer',
172
                            )
173
                        )
174
                    )
175
                )
176
            )
177
        );
178
        $this->root = new Resource\Root(
179
            $this,
180
            "Root",
181
            array(
182
                'methods' => array(
183
                    'getRoot' => array(
184
                        'path'         => '/',
185
                        'httpMethod'   => HttpRequest::REQUEST_METHOD_GET,
186
                        'requiresAuth' => false,
187
                        'requireScope' => array(),
188
                        'parameters'   => array()
189
                    )
190
                )
191
            )
192
        );
193
        $this->ingests = new Resource\Ingests(
194
            $this,
195
            "Ingests",
196
            array(
197
                'methods' => array(
198
                    'get' => array(
199
                        'path'        => '/ingests',
200
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
201
                        'requiresAuth'=> false,
202
                        'parameters'  => array()
203
                    )
204
                )
205
            )
206
        );
207
        $this->teams = new Resource\Teams(
208
            $this,
209
            "Teams",
210
            array(
211
                'methods' => array(
212
                    'listTeams' => array(
213
                        'path'        => '/teams',
214
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
215
                        'requiresAuth'=> false,
216
                        'parameters'  => array (
217
                            'limit'   => array (
218
                                'type'    => 'integer',
219
                                'min'     => 0,
220
                                'max'     => 100,
221
                            ),
222
                            'offset'  => array (
223
                                'type'    => 'integer',
224
                            )
225
                        )
226
                    ),
227
                    'getTeamByName' => array(
228
                        'path'        => '/teams/:team/',
229
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
230
                        'requiresAuth'=> false,
231
                        'parameters'  => array (
232
                            'team'   => array (
233
                                'type'      => 'string',
234
                                'url_part'  => true,
235
                            )
236
                        )
237
                    )
238
                )
239
            )
240
        );
241
        $this->videos = new Resource\Videos(
242
            $this,
243
            "Videos",
244
            array(
245
                'methods' => array(
246
                    'listTopVideos' => array(
247
                        'path'        => 'videos/top',
248
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
249
                        'requiresAuth'=> false,
250
                        'parameters'  => array (
251
                            'limit'   => array (
252
                                'type'    => 'integer',
253
                                'min'     => 0,
254
                                'max'     => 100,
255
                            ),
256
                            'offset' => array (
257
                                'type' => 'integer',
258
                            ),
259
                            'game' => array (
260
                                'type' => 'string',
261
                            ),
262
                            'period' => array (
263
                                'type'             => 'string',
264
                                'restricted_value' => array(
265
                                    'week', 'month', 'all'
266
                                )
267
                            )
268
                        )
269
                    ),
270
                    'getVideo' => array(
271
                        'path'        => 'videos/:id/',
272
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
273
                        'requiresAuth'=> false,
274
                        'parameters'  => array (
275
                            'id'   => array (
276
                                'type'      => 'string',
277
                                'url_part'  => true,
278
                            )
279
                        )
280
                    ),
281
                    /* @ depricated */
282
                    'listChannelVideos' => array(
283
                        'path'        => 'channels/:channel/videos',
284
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
285
                        'requiresAuth'=> false,
286
                        'parameters'  => array (
287
                            'limit'   => array (
288
                                'type'    => 'integer',
289
                                'min'     => 0,
290
                                'max'     => 100,
291
                            ),
292
                            'offset' => array (
293
                                'type' => 'integer',
294
                            ),
295
                            'broadcasts' => array (
296
                                'type' => 'bool',
297
                            ),
298
                            'hls' => array(
299
                                'type' => 'bool'
300
                            ),
301
                            'channel'   => array (
302
                                'type'      => 'string',
303
                                'url_part'  => true,
304
                            )
305
                        )
306
                    ),
307
                    'listVideosFollowed' => array(
308
                        'path'        => 'videos/followed',
309
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
310
                        'requiresAuth'=> true,
311
                        'parameters'  => array (
312
                            'limit'   => array (
313
                                'type'    => 'integer',
314
                                'min'     => 0,
315
                                'max'     => 100,
316
                            ),
317
                            'offset' => array (
318
                                'type' => 'integer',
319
                            )
320
                        )
321
                    )
322
                )
323
            )
324
        );
325
        $this->chat = new Resource\Chat(
326
            $this,
327
            "Chat",
328
            array(
329
                'methods' => array(
330
                    'getChannelLinks' => array(
331
                        'path'        => '/chat/:channel',
332
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
333
                        'requiresAuth'=> false,
334
                        'parameters'  => array (
335
                            'channel'   => array (
336
                                'type'      => 'string',
337
                                'url_part'  => true,
338
                            )
339
                        )
340
                    ),
341
                    'getEmoticons' => array(
342
                        'path'        => 'chat/emoticons',
343
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
344
                        'requiresAuth'=> false,
345
                        'parameters'  => array()
346
                    ),
347
                    'getChannelEmoticons' => array(
348
                        'path'        => 'chat/:channel/emoticons',
349
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
350
                        'requiresAuth'=> false,
351
                        'parameters'  => array(
352
                            'channel'   => array (
353
                                'type'      => 'string',
354
                                'url_part'  => true,
355
                            )
356
                        )
357
                    ),
358
                    'getChannelBadges' => array(
359
                        'path'        => 'chat/:channel/badges',
360
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
361
                        'requiresAuth'=> false,
362
                        'parameters'  => array(
363
                            'channel'   => array (
364
                                'type'      => 'string',
365
                                'url_part'  => true,
366
                            )
367
                        )
368
                    ),
369
                    'getEmoticonImages' => array(
370
                        'path'        => 'chat/emoticon_images',
371
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
372
                        'requiresAuth'=> false,
373
                        'parameters'  => array()
374
                    ),
375
                    'getEmoticonSets' => array(
376
                        'path'        => 'chat/emoticon_images/',
377
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
378
                        'requiresAuth'=> false,
379
                        'parameters'  => array(
380
                            'emotesets'   => array (
381
                                'type'     => 'integer',
382
                                'required' => true,
383
                            )
384
                        )
385
                    )
386
                )
387
            )
388
        );
389
        $this->streams = new Resource\Streams(
390
            $this,
391
            "Streams",
392
            array(
393
                'methods' => array(
394
                    'getStreamByChannel' => array(
395
                        'path'        => '/streams/:channel',
396
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
397
                        'requiresAuth'=> false,
398
                        'parameters'  => array (
399
                            'channel'   => array (
400
                                'type'      => 'string',
401
                                'url_part'  => true,
402
                            )
403
                        )
404
                    ),
405
                    'getStreams' => array(
406
                        'path'        => '/streams/',
407
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
408
                        'requiresAuth'=> false,
409
                        'parameters'  => array (
410
                            'game'   => array (
411
                                'type'      => 'string',
412
                            ),
413
                            'channel'   => array (
414
                                'type'      => 'string',
415
                            ),
416
                            'limit'   => array (
417
                                'type'      => 'integer',
418
                                'min'       => 1,
419
                                'max'       => 100,
420
                                'default'   => 25,
421
                            ),
422
                            'offset'   => array (
423
                                'type'      => 'integer',
424
                                'default'   => 0,
425
                            ),
426
                            'client_id' => array(
427
                                'type' => 'string',
428
                            ),
429
                            'stream_type' => array(
430
                                'type' => 'string',
431
                                'restricted_value' => array(
432
                                    'all', 'playlist', 'live'
433
                                )
434
                            )
435
                        )
436
                    ),
437
                    'getFeaturedStreams' => array(
438
                        'path'        => '/streams/featured',
439
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
440
                        'requiresAuth'=> false,
441
                        'parameters'  => array (
442
                            'limit'   => array (
443
                                'type'      => 'integer',
444
                                'min'       => 1,
445
                                'max'       => 100,
446
                                'default'   => 25,
447
                            ),
448
                            'offset'   => array (
449
                                'type'      => 'integer',
450
                                'default'   => 0,
451
                            ),
452
                        )
453
                    ),
454
                    'getStreamsSummary' => array(
455
                        'path'        => '/streams/summary',
456
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
457
                        'requiresAuth'=> false,
458
                        'parameters'  => array (
459
                            'game'   => array (
460
                                'type'      => 'string'
461
                            )
462
                        )
463
                    ),
464
                    'getStreamsFollowed' => array(
465
                        'path'        => '/streams/followed',
466
                        'httpMethod'  => HttpRequest::REQUEST_METHOD_GET,
467
                        'requiresAuth'=> true,
468
                        'parameters'  => array (
469
                            'limit'   => array (
470
                                'type'      => 'integer',
471
                                'min'       => 1,
472
                                'max'       => 100,
473
                                'default'   => 25,
474
                            ),
475
                            'offset'   => array (
476
                                'type'      => 'integer',
477
                                'default'   => 0,
478
                            ),
479
                            'stream_type' => array(
480
                                'type' => 'string',
481
                                'restricted_value' => array(
482
                                    'all', 'playlist', 'live'
483
                                )
484
                            )
485
                        )
486
                    )
487
                )
488
            )
489
        );
490
        $this->channels = new Resource\Channels(
491
            $this,
492
            "Channels",
493
            array(
494
                'methods' => array(
495
                    'getChannel' => array(
496
                        'path'         => 'channel',
497
                        'httpMethod'   => HttpRequest::REQUEST_METHOD_GET,
498
                        'requiresAuth' => true,
499
                        'requireScope' => array(Scope::CHANNEL_READ),
500
                        'parameters'   => array (),
501
                    ),
502
                    'getChannelByName' => array(
503
                        'path'         => 'channels/:channel',
504
                        'httpMethod'   => HttpRequest::REQUEST_METHOD_GET,
505
                        'requiresAuth' => false,
506
                        'requireScope' => array(Scope::CHANNEL_READ),
507
                        'parameters'   => array(
508
                            'channel'   => array (
509
                                'type'      => 'string',
510
                                'url_part'  => true,
511
                            )
512
                        ),
513
                    ),
514
                    'getChannelVideos' => array(
515
                        'path'         => 'channels/:channel/videos',
516
                        'httpMethod'   => HttpRequest::REQUEST_METHOD_GET,
517
                        'requireScope' => array(),
518
                        'requiresAuth' => false,
519
                        'requireScope' => array(),
520
                        'parameters'   => array (
521
                            'limit'    => array (
522
                                'type'    => 'integer',
523
                                'min'     => 0,
524
                                'max'     => 100,
525
                            ),
526
                            'offset' => array (
527
                                'type' => 'integer',
528
                            ),
529
                            'broadcasts' => array (
530
                                'type' => 'bool',
531
                            ),
532
                            'hls' => array(
533
                                'type' => 'bool'
534
                            ),
535
                            'channel'   => array (
536
                                'type'      => 'string',
537
                                'url_part'  => true,
538
                            )
539
                        )
540
                    ),
541
                    'getChannelEditors' => array(
542
                        'path'         => 'channels/:channel/editors',
543
                        'httpMethod'   => HttpRequest::REQUEST_METHOD_GET,
544
                        'requiresAuth' => true,
545
                        'requireScope' => array(Scope::CHANNEL_READ),
546
                        'parameters'   => array(
547
                            'channel'   => array (
548
                                'type'      => 'string',
549
                                'url_part'  => true,
550
                            )
551
                        ),
552
                    ),
553
                    'updateChannelByName' => array(
554
                        'path'         => 'channels/:channel',
555
                        'httpMethod'   => HttpRequest::REQUEST_METHOD_GET,
556
                        'requiresAuth' => false,
557
                        'requireScope' => array(Scope::CHANNEL_EDITOR),
558
                        'parameters'   => array(
559
                            'channel'   => array (
560
                                'type'      => 'string',
561
                                'url_part'  => true,
562
                            ),
563
                            'status'    => array (
564
                                'type'    => 'string',
565
                            ),
566
                            'game'    => array (
567
                                'type'    => 'string',
568
                            ),
569
                            'delay'    => array (
570
                                'type'    => 'string',
571
                            )
572
                        )
573
                    ),
574
                )
575
            )
576
        );
577
    }
578
579
    /**
580
     * @param string $name
581
     * @param Resource\ResourceAbstract $instance
582
     */
583
    public function registerResource($name="", Resource\ResourceAbstract $instance) {
584
        $this->resources[$name] = $instance;
585
    }
586
587
    /**
588
     * @return array
589
     */
590
    public function getSupportedFunctions() {
591
        return $this->resources;
592
    }
593
594
    /* -- Setters */
595
596
597
    /**
598
     * @param $client_id
599
     * @return $this
600
     */
601
    public function setClientId($client_id)
602
    {
603
        $this->client_id = $client_id;
604
        return $this;
605
    }
606
607
608
    /**
609
     * @param $redirect_uri
610
     * @return $this
611
     */
612
    public function setRedirectUri($redirect_uri)
613
    {
614
        $this->redirect_uri = $redirect_uri;
615
        return $this;
616
    }
617
618
619
    /**
620
     * @param $client_secret
621
     * @return $this
622
     */
623
    public function setClientSecret($client_secret)
624
    {
625
        $this->client_secret = $client_secret;
626
        return $this;
627
    }
628
629
630
    /**
631
     * @param string $api_url
632
     * @return $this
633
     */
634
    public function setApiUrl($api_url)
635
    {
636
        $this->api_url = $api_url;
637
        return $this;
638
    }
639
640
641
    /**
642
     * @param $force_relogin
643
     * @return $this
644
     */
645
    public function setForceRelogin($force_relogin)
646
    {
647
        $this->force_relogin = $force_relogin;
648
        return $this;
649
    }
650
651
652
    /**
653
     * @param $access_token
654
     */
655
    public function setAccessToken($access_token)
656
    {
657
        $this->access_token = $access_token;
658
    }
659
660
    /**
661
     * @param array $scope
662
     */
663
    public function setScope($scope)
664
    {
665
        $this->scope = $scope;
666
    }
667
668
669
    /* -- Getters  */
670
671
    /**
672
     * @return string
673
     */
674
    public function getClientId()
675
    {
676
        return $this->client_id;
677
    }
678
679
    /**
680
     * @return string
681
     */
682
    public function getRedirectUri()
683
    {
684
        return $this->redirect_uri;
685
    }
686
687
    /**
688
     * @return string
689
     */
690
    public function getClientSecret()
691
    {
692
        return $this->client_secret;
693
    }
694
695
    /**
696
     * @return string
697
     */
698
    public function getApiUrl()
699
    {
700
        return $this->api_url;
701
    }
702
703
    /**
704
     * @return boolean
705
     */
706
    public function isForceRelogin()
707
    {
708
        return $this->force_relogin;
709
    }
710
711
    /**
712
     * @return string
713
     */
714
    public function getAccessToken()
715
    {
716
        return $this->access_token;
717
    }
718
719
    /**
720
     * @return array
721
     */
722
    public function getScope()
723
    {
724
        return $this->scope;
725
    }
726
727
728
729
    /**
730
     * @return AuthModel
731
     */
732
    public function getAuthModel() {
733
        return new AuthModel($this);
734
    }
735
736
    /**
737
     * Get transport
738
     *
739
     * @return TransportInterface Transport
740
     */
741
    public function getTransport()
742
    {
743
        // Set transport if haven't
744
        if ($this->transport === null) {
745
            $this->setTransport(new Http($this));
746
        }
747
        return $this->transport;
748
    }
749
750
    /**
751
     * Set transport
752
     *
753
     * @param TransportInterface $transport Transport
754
     *
755
     * @return self This object
756
     */
757
    public function setTransport(TransportInterface $transport)
758
    {
759
        $this->transport = $transport;
760
        return $this;
761
    }
762
763
}