Completed
Push — develop ( 2b0d56...088525 )
by Josef
02:15
created

TwitchSDK::setConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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