Completed
Push — master ( c1abc1...1c17fb )
by Josef
03:52
created

TwitchSDK::teamGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace jofner\SDK\TwitchTV;
4
5
use jofner\SDK\TwitchTV\Methods;
6
7
/**
8
 * TwitchTV API SDK for PHP
9
 *
10
 * PHP SDK for interacting with the TwitchTV API
11
 *
12
 * @author Josef Ohnheiser <[email protected]>
13
 * @license https://github.com/jofner/Twitch-SDK/blob/master/LICENSE.md MIT
14
 * @homepage https://github.com/jofner/Twitch-SDK
15
 */
16
class TwitchSDK
17
{
18
    /** @var array */
19
    private $config = array();
20
21
    /** @var TwitchRequest */
22
    protected $request;
23
24
    /** @var Helper */
25
    protected $helper;
26
27
    /**
28
     * TwitchSDK constructor
29
     * @param array $config
30
     * @throws TwitchSDKException
31
     * @throws \InvalidArgumentException
32
     */
33
    public function __construct(array $config = array())
34
    {
35
        if (!in_array('curl', get_loaded_extensions(), true)) {
36
            throw new TwitchSDKException('cURL extension is not installed and is required');
37
        }
38
39
        if (!array_key_exists('client_id', $config)) {
40
            throw new \InvalidArgumentException('Missing required Client-ID parameter in config
41
                @see https://blog.twitch.tv/client-id-required-for-kraken-api-calls-afbb8e95f843');
42
        }
43
44
        if (count($config) > 0) {
45
            $this->setConfig($config);
46
        }
47
48
        $this->request = new TwitchRequest;
49
        $this->request->setClientId($config['client_id']);
50
        $this->helper = new Helper;
51
    }
52
53
    /**
54
     * config setter
55
     * @param array $config
56
     * @return TwitchSDK
57
     * @throws TwitchSDKException
58
     */
59
    public function setConfig(array $config)
60
    {
61
        if ($this->configValidate($config) === true) {
62
            $this->config = $config;
63
        } else {
64
            throw new TwitchSDKException('Wrong Twitch API config parameters');
65
        }
66
67
        return $this;
68
    }
69
70
    /**
71
     * Check if config is set
72
     * @throws TwitchSDKException
73
     */
74
    private function checkConfig()
75
    {
76
        if (count($this->config) === 0) {
77
            $this->configException();
78
        }
79
    }
80
81
    /**
82
     * Get value from config
83
     * @param string $key
84
     * @return mixed
85
     * @throws TwitchSDKException
86
     */
87
    private function getConfigParam($key)
88
    {
89
        if (!array_key_exists($key, $this->config)) {
90
            throw new TwitchSDKException('Missing configuration parameter');
91
        }
92
93
        return $this->config[$key];
94
    }
95
96
    /**
97
     * Basic information about the API and authentication status
98
     * @param null $token
99
     * @return \stdClass
100
     * @throws TwitchSDKException
101
     */
102
    public function status($token = null)
103
    {
104
        $auth = null;
105
106
        if ($token !== null) {
107
            if (count($this->config) === 0) {
108
                $this->configException();
109
            } else {
110
                $auth = $this->helper->buildQueryString(array('oauth_token' => $token));
111
            }
112
        }
113
114
        return $this->request->request($auth);
115
    }
116
117
    /**
118
     * Get the specified user
119
     * @param $username
120
     * @return \stdClass
121
     * @throws TwitchSDKException
122
     */
123
    public function userGet($username)
124
    {
125
        $user = new Methods\User($this->request);
126
127
        return $user->getUser($username);
128
    }
129
130
    /**
131
     * Get a user's list of followed channels
132
     * @param string $user
133
     * @param integer $limit
134
     * @param integer $offset
135
     * @param string $direction
136
     * @param string $sortby
137
     * @return \stdClass
138
     * @throws TwitchSDKException
139
     */
140 View Code Duplication
    public function userFollowChannels($user, $limit = null, $offset = null, $direction = null, $sortby = null)
141
    {
142
        $queryString = $this->helper->buildQueryString(array(
143
            'limit' => $limit,
144
            'offset' => $offset,
145
            'direction' => $direction,
146
            'sortby' => $sortby,
147
        ));
148
149
        $follow = new Methods\Follow($this->request);
150
151
        return $follow->userFollowChannels($user, $queryString);
152
    }
153
154
    /**
155
     * Get the status of a follow relationship
156
     * @param string $user
157
     * @param string $channel
158
     * @return \stdClass
159
     * @throws TwitchSDKException
160
     */
161
    public function userFollowRelationship($user, $channel)
162
    {
163
        $follow = new Methods\Follow($this->request);
164
165
        return $follow->userIsFollowingChannel($user, $channel);
166
    }
167
168
    /**
169
     * Set user to follow given channel
170
     *  - requires scope 'user_follows_edit'
171
     * @param string $user
172
     * @param string $channel
173
     * @param string $userToken
174
     * @param bool $notifications
175
     * @return \stdClass
176
     * @throws TwitchSDKException
177
     */
178 View Code Duplication
    public function authUserFollowChannel($user, $channel, $userToken, $notifications = false)
179
    {
180
        $this->checkConfig();
181
182
        $queryString = $this->helper->buildQueryString(array(
183
            'oauth_token' => $userToken,
184
            'notifications' => $notifications,
185
        ));
186
187
        $follow = new Methods\Follow($this->request);
188
189
        return $follow->followChannel($user, $channel, $queryString);
190
    }
191
192
    /**
193
     * Set user to unfollow given channel
194
     *  - requires scope 'user_follows_edit'
195
     * @param string $user
196
     * @param string $channel
197
     * @param string $userToken
198
     * @return \stdClass
199
     * @throws TwitchSDKException
200
     */
201 View Code Duplication
    public function authUserUnfollowChannel($user, $channel, $userToken)
202
    {
203
        $this->checkConfig();
204
205
        $queryString = $this->helper->buildQueryString(array(
206
            'oauth_token' => $userToken,
207
        ));
208
209
        $follow = new Methods\Follow($this->request);
210
211
        return $follow->unfollowChannel($user, $channel, $queryString);
212
    }
213
214
    /**
215
     * Get the specified channel
216
     * @param string $channelName
217
     * @return \stdClass
218
     * @throws TwitchSDKException
219
     */
220
    public function channelGet($channelName)
221
    {
222
        $channel = new Methods\Channel($this->request);
223
224
        return $channel->getChannels($channelName);
225
    }
226
227
    /**
228
     * Return team list for specified channel
229
     * @param string $channelName
230
     * @return \stdClass
231
     * @throws TwitchSDKException
232
     */
233
    public function channelTeamsGet($channelName)
234
    {
235
        $channel = new Methods\Channel($this->request);
236
237
        return $channel->getTeams($channelName);
238
    }
239
240
    /**
241
     * Search channels
242
     * @param string $query
243
     * @param integer $limit
244
     * @param integer $offset
245
     * @return \stdClass
246
     * @throws TwitchSDKException
247
     */
248
    public function channelSearch($query, $limit = null, $offset = null)
249
    {
250
        $queryString = $this->helper->buildQueryString(array(
251
            'query' => $query,
252
            'limit' => $limit,
253
            'offset' => $offset,
254
        ));
255
256
        $search = new Methods\Search($this->request);
257
258
        return $search->channels($queryString);
259
    }
260
261
    /**
262
     * Get the specified team
263
     * @param $teamName
264
     * @return \stdClass
265
     * @throws TwitchSDKException
266
     */
267
    public function teamGet($teamName)
268
    {
269
        $team = new Methods\Team($this->request);
270
271
        return $team->getTeam($teamName);
272
    }
273
274
    /**
275
     * Returns a list of active teams
276
     * @param integer $limit
277
     * @param integer $offset
278
     * @return \stdClass
279
     * @throws TwitchSDKException
280
     */
281 View Code Duplication
    public function teamList($limit = null, $offset = null)
282
    {
283
        $queryString = $this->helper->buildQueryString(array(
284
            'limit' => $limit,
285
            'offset' => $offset,
286
        ));
287
288
        $team = new Methods\Team($this->request);
289
290
        return $team->getTeams($queryString);
291
    }
292
293
    /**
294
     * Get all team members
295
     * @param $teamName
296
     * @return mixed
297
     * @throws TwitchSDKException
298
     */
299
    public function teamMembersAll($teamName)
300
    {
301
        return $this->request->teamRequest($teamName . '/all_channels')->channels;
302
    }
303
304
    /**
305
     * Returns an array of users who follow the specified channel
306
     * @param string $channelName
307
     * @param integer $limit
308
     * @param integer $offset
309
     * @param string $cursor
310
     * @param string $direction
311
     * @return \stdClass
312
     * @throws TwitchSDKException
313
     */
314 View Code Duplication
    public function channelFollows($channelName, $limit = null, $offset = null, $cursor = null, $direction = null)
315
    {
316
        $queryString = $this->helper->buildQueryString(array(
317
            'limit' => $limit,
318
            'offset' => $offset,
319
            'cursor' => $cursor,
320
            'direction' => $direction,
321
        ));
322
323
        $follow = new Methods\Follow($this->request);
324
325
        return $follow->getChannelFollows($channelName, $queryString);
326
    }
327
328
    /**
329
     * Get the specified channel's stream
330
     * @param $channel
331
     * @return \stdClass
332
     * @throws TwitchSDKException
333
     */
334
    public function streamGet($channel)
335
    {
336
        $stream = new Methods\Stream($this->request);
337
338
        return $stream->getStream($channel);
339
    }
340
341
    /**
342
     * Search live streams
343
     * @param string $query
344
     * @param null $limit
345
     * @param null $offset
346
     * @param null $hls
347
     * @return \stdClass
348
     * @throws TwitchSDKException
349
     */
350 View Code Duplication
    public function streamSearch($query, $limit = null, $offset = null, $hls = null)
351
    {
352
        $queryString = $this->helper->buildQueryString(array(
353
            'query' => $query,
354
            'limit' => $limit,
355
            'offset' => $offset,
356
            'hls' => $hls,
357
        ));
358
359
        $search = new Methods\Search($this->request);
360
361
        return $search->streams($queryString);
362
    }
363
364
    /**
365
     * Summarize streams
366
     * @param null $game
367
     * @param array|null $channels
368
     * @param null $hls
369
     * @return \stdClass
370
     * @throws TwitchSDKException
371
     */
372 View Code Duplication
    public function streamsSummarize($game = null, array $channels = null, $hls = null)
373
    {
374
        if (!empty($channels)) {
375
            $channels = implode(',', $channels);
376
        }
377
378
        $queryString = $this->helper->buildQueryString(array(
379
            'game' => $game,
380
            'channel' => $channels,
381
            'hls' => $hls,
382
        ));
383
384
        $stream = new Methods\Stream($this->request);
385
386
        return $stream->getSummary($queryString);
387
    }
388
389
    /**
390
     * Get featured streams
391
     * @param null $limit
392
     * @param null $offset
393
     * @param null $hls
394
     * @return \stdClass
395
     * @throws TwitchSDKException
396
     */
397 View Code Duplication
    public function streamsFeatured($limit = null, $offset = null, $hls = null)
398
    {
399
        $queryString = $this->helper->buildQueryString(array(
400
            'limit' => $limit,
401
            'offset' => $offset,
402
            'hls' => $hls,
403
        ));
404
405
        $stream = new Methods\Stream($this->request);
406
407
        return $stream->getFeatured($queryString);
408
    }
409
410
    /**
411
     * Get video
412
     * @param string $videoId
413
     * @return \stdClass
414
     * @throws TwitchSDKException
415
     */
416
    public function videoGet($videoId)
417
    {
418
        $video = new Methods\Video($this->request);
419
420
        return $video->getVideo($videoId);
421
    }
422
423
    /**
424
     * Returns top videos
425
     * @param integer $limit
426
     * @param integer $offset
427
     * @param string $game
428
     * @param string $period
429
     * @return \stdClass
430
     * @throws TwitchSDKException
431
     */
432 View Code Duplication
    public function videosTop($limit = null, $offset = null, $game = null, $period = null)
433
    {
434
        $queryString = $this->helper->buildQueryString(array(
435
            'limit' => $limit,
436
            'offset' => $offset,
437
            'game' => $game,
438
            'period' => $period,
439
        ));
440
441
        $video = new Methods\Video($this->request);
442
443
        return $video->getTop($queryString);
444
    }
445
446
    /**
447
     * Get videos for a channel
448
     * @param $channel
449
     * @param null $limit
450
     * @param null $offset
451
     * @param bool $broadcasts
452
     * @param bool $hls
453
     * @return \stdClass
454
     * @throws TwitchSDKException
455
     */
456 View Code Duplication
    public function videosByChannel($channel, $limit = null, $offset = null, $broadcasts = null, $hls = null)
457
    {
458
        $queryString = $this->helper->buildQueryString(array(
459
            'limit' => $limit,
460
            'offset' => $offset,
461
            'broadcasts' => $broadcasts,
462
            'hls' => $hls,
463
        ));
464
465
        $video = new Methods\Video($this->request);
466
467
        return $video->getChannelVideos($channel, $queryString);
468
    }
469
470
    /**
471
     * Returns a links object to all other chat endpoints
472
     * @param string $channelName
473
     * @return \stdClass
474
     * @throws TwitchSDKException
475
     */
476
    public function chatGet($channelName)
477
    {
478
        $chat = new Methods\Chat($this->request);
479
480
        return $chat->getChat($channelName);
481
    }
482
483
    /**
484
     * Get a chat's emoticons
485
     * @return \stdClass
486
     * @throws TwitchSDKException
487
     */
488
    public function chatEmoticons()
489
    {
490
        $chat = new Methods\Chat($this->request);
491
492
        return $chat->getEmoticons();
493
    }
494
495
    /**
496
     * Returns a list of emoticons
497
     * @param string $emoteset
498
     * @return \stdClass
499
     * @throws TwitchSDKException
500
     */
501 View Code Duplication
    public function chatEmoticonsImages($emoteset = null)
502
    {
503
        $queryString = $this->helper->buildQueryString(array(
504
            'emotesets' => $emoteset,
505
        ));
506
507
        $chat = new Methods\Chat($this->request);
508
509
        return $chat->getEmoticonImages($queryString);
510
    }
511
512
    /**
513
     * Returns a list of chat badges
514
     * @param string $channelName
515
     * @return \stdClass
516
     * @throws TwitchSDKException
517
     */
518
    public function chatBadges($channelName)
519
    {
520
        $chat = new Methods\Chat($this->request);
521
522
        return $chat->getBadges($channelName);
523
    }
524
525
    /**
526
     * Get top games
527
     * @param integer $limit
528
     * @param integer $offset
529
     * @return \stdClass
530
     * @throws TwitchSDKException
531
     */
532 View Code Duplication
    public function gamesTop($limit = null, $offset = null)
533
    {
534
        $queryString = $this->helper->buildQueryString(array(
535
            'limit' => $limit,
536
            'offset' => $offset,
537
        ));
538
539
        $game = new Methods\Game($this->request);
540
541
        return $game->getTop($queryString);
542
    }
543
544
    /**
545
     * Search games
546
     * @param string $query
547
     * @param null $type
548
     * @param null $live
549
     * @return \stdClass
550
     * @throws TwitchSDKException
551
     */
552
    public function gameSearch($query, $type = null, $live = null)
553
    {
554
        $queryString = $this->helper->buildQueryString(array(
555
            'query' => $query,
556
            'type' => $type,
557
            'live' => $live,
558
        ));
559
560
        $search = new Methods\Search($this->request);
561
562
        return $search->games($queryString);
563
    }
564
565
    /**
566
     * Get HTML code for stream embedding
567
     * @param $channel
568
     * @param int $width
569
     * @param int $height
570
     * @param int $volume
571
     * @return string
572
     * @deprecated Will be replaced with Embed method
573
     */
574 View Code Duplication
    public function embedStream($channel, $width = 620, $height = 378, $volume = 25)
575
    {
576
        return '<object type="application/x-shockwave-flash"
577
                height="' . $height . '"
578
                width="' . $width . '"
579
                id="live_embed_player_flash"
580
                data="http://www.twitch.tv/widgets/live_embed_player.swf?channel=' . $channel . '"
581
                bgcolor="#000000">
582
                <param  name="allowFullScreen"
583
                    value="true" />
584
                <param  name="allowScriptAccess"
585
                    value="always" />
586
                <param  name="allowNetworking"
587
                    value="all" />
588
                <param  name="movie"
589
                    value="http://www.twitch.tv/widgets/live_embed_player.swf" />
590
                <param  name="flashvars"
591
                    value="hostname=www.twitch.tv&channel=' . $channel . '&auto_play=true&start_volume=' . $volume . '" />
592
                </object>';
593
    }
594
595
    /**
596
     * Get HTML code for video embedding
597
     * @param $channel
598
     * @param $chapterid
599
     * @param int $width
600
     * @param int $height
601
     * @param int $volume
602
     * @return string
603
     * @deprecated Will be replaced with Embed method
604
     */
605 View Code Duplication
    public function embedVideo($channel, $chapterid, $width = 400, $height = 300, $volume = 25)
606
    {
607
        return '<object bgcolor="#000000"
608
                    data="http://www.twitch.tv/widgets/archive_embed_player.swf"
609
                    width="' . $width . '"
610
                    height="' . $height . '"
611
                    id="clip_embed_player_flash"
612
                    type="application/x-shockwave-flash">
613
                <param  name="movie"
614
                    value="http://www.twitch.tv/widgets/archive_embed_player.swf" />
615
                <param  name="allowScriptAccess"
616
                    value="always" />
617
                <param  name="allowNetworking"
618
                    value="all" />
619
                <param name="allowFullScreen"
620
                    value="true" />
621
                <param  name="flashvars"
622
                    value="channel=' . $channel . '&start_volume=' . $volume . '&auto_play=false&chapter_id=' . $chapterid . '" />
623
                </object>';
624
    }
625
626
    /**
627
     * Get HTML code for chat embedding
628
     * @param $channel
629
     * @param int $width
630
     * @param int $height
631
     * @return string
632
     * @deprecated Will be replaced with Embed method
633
     */
634
    public function embedChat($channel, $width = 400, $height = 300)
635
    {
636
        return '<iframe frameborder="0"
637
                    scrolling="no"
638
                    id="chat_embed"
639
                    src="http://twitch.tv/chat/embed?channel=' . $channel . '&amp;popout_chat=true"
640
                    height="' . $height . '"
641
                    width="' . $width . '">
642
                </iframe>';
643
    }
644
645
    /**
646
     * Get login URL for authentication
647
     * @param string $scope Specify which permissions your app requires (space separated list)
648
     * @return \stdClass
649
     * @throws TwitchSDKException
650
     */
651 View Code Duplication
    public function authLoginURL($scope)
652
    {
653
        $this->checkConfig();
654
655
        $queryString = $this->helper->buildQueryString(array(
656
            'response_type' => 'code',
657
            'client_id' => $this->getConfigParam('client_id'),
658
            'redirect_uri' => $this->getConfigParam('redirect_uri'),
659
            'scope' => $scope,
660
        ));
661
662
        $auth = new Methods\Auth($this->request);
663
664
        return $auth->getLoginURL($queryString);
665
    }
666
667
    /**
668
     * Get authentication access token
669
     * @param string $code returned after app authorization by user
670
     * @return \stdClass
671
     * @throws TwitchSDKException
672
     */
673 View Code Duplication
    public function authAccessTokenGet($code)
674
    {
675
        $this->checkConfig();
676
677
        $queryString = $this->helper->buildQueryString(array(
678
            'client_id' => $this->getConfigParam('client_id'),
679
            'client_secret' => $this->getConfigParam('client_secret'),
680
            'grant_type' => 'authorization_code',
681
            'redirect_uri' => $this->getConfigParam('redirect_uri'),
682
            'code' => $code,
683
        ));
684
685
        $auth = new Methods\Auth($this->request);
686
687
        return $auth->getAccessToken($queryString);
688
    }
689
690
    /**
691
     * Get the authenticated user
692
     *  - requires scope 'user_read'
693
     * @param string
694
     * @return \stdClass
695
     * @throws TwitchSDKException
696
     */
697
    public function authUserGet($token)
698
    {
699
        $this->checkConfig();
700
        $queryString = $this->getAuthString($token);
701
        $user = new Methods\User($this->request);
702
703
        return $user->getUserAuth($queryString);
704
    }
705
706
    /**
707
     * Returns a list of blocks
708
     *  - required scope 'user_blocks_read'
709
     * @param string $token
710
     * @param string $user
711
     * @param integer $limit
712
     * @param integer $offset
713
     * @return \stdClass
714
     * @throws TwitchSDKException
715
     */
716
    public function authUserBlocks($token, $user, $limit = null, $offset = null)
717
    {
718
        $this->checkConfig();
719
720
        $queryString = $this->helper->buildQueryString(array(
721
            'oauth_token' => $token,
722
            'client_id' => $this->getConfigParam('client_id'),
723
            'limit' => $limit,
724
            'offset' => $offset,
725
        ));
726
727
        $block = new Methods\Block($this->request);
728
729
        return $block->getBlocks($user, $queryString);
730
    }
731
732
    /**
733
     * Adds $target to $user block list
734
     *  - requires scope 'user_blocks_edit'
735
     * @param string $token
736
     * @param string $user
737
     * @param string $target
738
     * @return \stdClass
739
     * @throws TwitchSDKException
740
     */
741 View Code Duplication
    public function authBlockTarget($token, $user, $target)
742
    {
743
        $this->checkConfig();
744
        $queryString = $this->getAuthString($token);
745
        $block = new Methods\Block($this->request);
746
747
        return $block->blockTarget($user, $target, $queryString);
748
    }
749
750
    /**
751
     * Removes $target from $user block list
752
     *  - requires scope 'user_blocks_edit'
753
     * @param string $token
754
     * @param string $user
755
     * @param string $target
756
     * @return \stdClass
757
     * @throws TwitchSDKException
758
     */
759 View Code Duplication
    public function authRemoveTarget($token, $user, $target)
760
    {
761
        $this->checkConfig();
762
        $queryString = $this->getAuthString($token);
763
        $block = new Methods\Block($this->request);
764
765
        return $block->removeTarget($user, $target, $queryString);
766
    }
767
768
    /**
769
     * Get the authenticated channel
770
     *  - requires scope 'channel_read'
771
     * @param string
772
     * @return \stdClass
773
     * @throws TwitchSDKException
774
     */
775 View Code Duplication
    public function authChannelGet($token)
776
    {
777
        $this->checkConfig();
778
        $queryString = $this->getAuthString($token);
779
        $channels = new Methods\Channel($this->request);
780
781
        return $channels->getChannel($queryString);
782
    }
783
784
    /**
785
     * Update channel's status or game
786
     *  - requires scope 'channel_editor'
787
     * @param $token
788
     * @param string $channelName
789
     * @param string $status
790
     * @param string $game
791
     * @param integer $delay
792
     * @return \stdClass
793
     * @throws TwitchSDKException
794
     */
795
    public function authChannelSet($token, $channelName, $status = null, $game = null, $delay = null)
796
    {
797
        $this->checkConfig();
798
        $queryString = $this->getAuthString($token);
799
800
        $data = $this->helper->buildQueryString(array(
801
            'channel[status]' => $status,
802
            'channel[game]' => $game,
803
            'channel[delay]' => $delay,
804
        ));
805
806
        $channel = new Methods\Channel($this->request);
807
808
        return $channel->setChannel($channelName, $queryString, $data);
809
    }
810
811
    /**
812
     * Resets channel's stream key
813
     *  - requires scope 'channel_stream'
814
     * @param string $token
815
     * @param string $channelName
816
     * @return \stdClass
817
     * @throws TwitchSDKException
818
     */
819 View Code Duplication
    public function authChannelResetKey($token, $channelName)
820
    {
821
        $this->checkConfig();
822
        $queryString = $this->getAuthString($token);
823
        $channel = new Methods\Channel($this->request);
824
825
        return $channel->resetStreamKey($channelName, $queryString);
826
    }
827
828
    /**
829
     * Returns an array of users who are editors of specified channel
830
     *  - requires scope 'channel_read'
831
     * @param string
832
     * @param string
833
     * @return \stdClass
834
     * @throws TwitchSDKException
835
     */
836 View Code Duplication
    public function authChannelEditors($token, $channel)
837
    {
838
        $this->checkConfig();
839
        $queryString = $this->getAuthString($token);
840
        $channels = new Methods\Channel($this->request);
841
842
        return $channels->getEditors($channel, $queryString);
843
    }
844
845
    /**
846
     * Returns an array of subscriptions who are subscribed to specified channel
847
     *  - requires scope 'channel_subscriptions'
848
     * @param string $token - user's access token
849
     * @param string $channel
850
     * @param integer $limit - can be up to 100
851
     * @param integer $offset
852
     * @param string $direction can be DESC|ASC, if DESC - lasts will be showed first
853
     * @return \stdClass
854
     * @throws TwitchSDKException
855
     */
856 View Code Duplication
    public function authChannelSubscriptions($token, $channel, $limit = 25, $offset = 0, $direction = 'DESC')
857
    {
858
        $this->checkConfig();
859
860
        $queryString = $this->helper->buildQueryString(array(
861
            'oauth_token' => $token,
862
            'client_id' => $this->getConfigParam('client_id'),
863
            'direction' => $direction,
864
            'limit' => $limit,
865
            'offset' => $offset
866
        ));
867
868
        $subscription = new Methods\Subscription($this->request);
869
870
        return $subscription->getSubscriptions($channel, $queryString);
871
    }
872
873
    /**
874
     * Returns user object if that user is subscribed
875
     *  - requires scope 'channel_check_subscription' for channel
876
     * @param string $token
877
     * @param string $channel
878
     * @param string $user
879
     * @return \stdClass
880
     * @throws TwitchSDKException
881
     */
882 View Code Duplication
    public function authSubscribedUser($token, $channel, $user)
883
    {
884
        $this->checkConfig();
885
        $queryString = $this->getAuthString($token);
886
        $subscription = new Methods\Subscription($this->request);
887
888
        return $subscription->getSubscribedUser($channel, $user, $queryString);
889
    }
890
891
    /**
892
     * Returns a channel object that user subscribes to
893
     *  - requires scope 'user_subscriptions' for user
894
     * @param string $token
895
     * @param string $user
896
     * @param string $channel
897
     * @return \stdClass
898
     * @throws TwitchSDKException
899
     */
900 View Code Duplication
    public function authSubscribedToChannel($token, $user, $channel)
901
    {
902
        $this->checkConfig();
903
        $queryString = $this->getAuthString($token);
904
        $subscription = new Methods\Subscription($this->request);
905
906
        return $subscription->getSubscribedToChannel($user, $channel, $queryString);
907
    }
908
909
    /**
910
     * List the live streams that the authenticated user is following
911
     *  - requires scope 'user_read'
912
     * @param string
913
     * @param integer $limit
914
     * @param integer $offset
915
     * @param bool $hls
916
     * @return \stdClass
917
     * @throws TwitchSDKException
918
     */
919 View Code Duplication
    public function authStreamsFollowed($token, $limit = 25, $offset = 0, $hls = null)
920
    {
921
        $this->checkConfig();
922
923
        $queryString = $this->helper->buildQueryString(array(
924
            'oauth_token' => $token,
925
            'client_id' => $this->getConfigParam('client_id'),
926
            'limit' => $limit,
927
            'offset' => $offset,
928
            'hls' => $hls,
929
        ));
930
931
        $user = new Methods\User($this->request);
932
933
        return $user->getFollowedStreams($queryString);
934
    }
935
936
    /**
937
     * Get streams helper
938
     * @param null $game
939
     * @param string|null $channels
940
     * @param null $limit
941
     * @param null $offset
942
     * @param null $embeddable
943
     * @param null $hls
944
     * @return \stdClass
945
     * @throws TwitchSDKException
946
     */
947
    public function getStreams($game = null, $channels = null, $limit = null, $offset = null, $embeddable = null, $hls = null)
948
    {
949
        $params = array(
950
            'game' => $game,
951
            'channel' => !empty($channels) ? $channels : null,
952
            'limit' => $limit,
953
            'offset' => $offset,
954
            'embeddable' => $embeddable,
955
            'hls' => $hls,
956
        );
957
958
        $queryString = $this->helper->buildQueryString($params);
959
        $stream = new Methods\Stream($this->request);
960
961
        return $stream->getStreams($queryString);
962
    }
963
964
    /**
965
     * Validate parameters for authentication
966
     * @param array
967
     * @return boolean
968
     */
969
    private function configValidate($config)
970
    {
971
        $check = array('client_id', 'client_secret', 'redirect_uri');
972
973
        foreach ($check as $val) {
974
            if (!array_key_exists($val, $config) ||
975
                (empty($config[$val]) ||
976
                    !is_string($config[$val]))
977
            ) {
978
                return false;
979
            }
980
        }
981
982
        return true;
983
    }
984
985
    /**
986
     * Returns string for auth
987
     * @param string $token
988
     * @return null|string
989
     * @throws TwitchSDKException
990
     */
991
    private function getAuthString($token)
992
    {
993
        return $this->helper->buildQueryString(array(
994
            'oauth_token' => $token,
995
            'client_id' => $this->getConfigParam('client_id'),
996
        ));
997
    }
998
999
    /**
1000
     * Configuration exception
1001
     * @throws TwitchSDKException
1002
     */
1003
    private function configException()
1004
    {
1005
        throw new TwitchSDKException('Cannot call authenticate functions without valid API configuration');
1006
    }
1007
}
1008