Completed
Pull Request — develop (#9)
by
unknown
03:52
created

TwitchSDK::status()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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