Completed
Push — develop ( bfb93a...d5c989 )
by Josef
01:49
created

TwitchSDK::authChannelSubscriptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Importance

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