Completed
Push — develop ( 41c776...122f3f )
by Josef
02:03
created

TwitchSDK::gamesTop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace ritero\SDK\TwitchTV;
4
5
use ritero\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
 * @todo All methods - revisit the setApiVersion() function
17
 * @todo refactoring for all methods of SDK, revision by methods
18
 */
19
class TwitchSDK
20
{
21
    /**
22
     * @var array|bool
23
     * @todo Create setter and getter with data validation
24
     */
25
    protected $authConfig = false;
26
27
    /** @var TwitchRequest */
28
    protected $request;
29
30
    /** @var Helper */
31
    protected $helper;
32
33
    /**
34
     * TwitchAPI URI's
35
     */
36
    const URI_USER_FOLLOWS_CHANNEL = '/users/%s/follows/channels';
37
    const URI_USER_FOLLOW_RELATION = '/users/%s/follows/channels/%s';
38
    const URI_CHANNEL = 'channels/';
39
    const URI_CHANNEL_FOLLOWS = 'channels/%s/follows';
40
    const URI_STREAMS_SEARCH = 'search/streams/';
41
    const URI_VIDEO = 'videos/';
42
    const URI_CHAT_EMOTICONS = 'chat/emoticons';
43
    const URI_GAMES_TOP = 'games/top/';
44
45
    /**
46
     * TwitchSDK constructor
47
     * @param array $config
48
     * @throws TwitchException
49
     */
50
    public function __construct(array $config = array())
51
    {
52
        if (!in_array('curl', get_loaded_extensions())) {
53
            throw new TwitchException('cURL extension is not installed and is required');
54
        }
55
56
        if (count($config) > 0) {
57
            $this->setAuthConfig($config);
58
        }
59
60
        /**
61
         * Develop workaround for requests
62
         * @todo class calls refactoring needed for future use
63
         */
64
        $this->request = new TwitchRequest;
65
        $this->helper = new Helper;
66
    }
67
68
    /**
69
     * authConfig setter
70
     * @param array $config
71
     * @return TwitchSDK
72
     * @throws TwitchException
73
     */
74
    public function setAuthConfig(array $config)
75
    {
76
        if ($this->configValidate($config) === true) {
77
            $this->authConfig = $config;
78
        } else {
79
            throw new TwitchException('Wrong Twitch API config parameters');
80
        }
81
82
        return $this;
83
    }
84
85
    /**
86
     * Basic information about the API and authentication status
87
     * @param null $token
88
     * @return \stdClass
89
     * @throws TwitchException
90
     */
91
    public function status($token = null)
92
    {
93
        $auth = null;
94
95
        if ($token !== null) {
96
            if ($this->authConfig === false) {
97
                $this->authConfigException();
98
            } else {
99
                $auth = $this->helper->buildQueryString(array('oauth_token' => $token));
100
            }
101
        }
102
103
        return $this->request->request($auth);
104
    }
105
106
    /**
107
     * Get the specified user
108
     * @param $username
109
     * @return \stdClass
110
     * @throws TwitchException
111
     */
112
    public function userGet($username)
113
    {
114
        $user = new Methods\User($this->request);
115
        return $user->getUser($username);
116
    }
117
118
    /**
119
     * Get a user's list of followed channels
120
     * @param $user
121
     * @param null $limit
122
     * @param null $offset
123
     * @return \stdClass
124
     * @throws TwitchException
125
     */
126 View Code Duplication
    public function userFollowChannels($user, $limit = null, $offset = null)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
    {
128
        $queryString = $this->helper->buildQueryString(array(
129
            'limit' => $limit,
130
            'offset' => $offset,
131
        ));
132
133
        return $this->request->request(sprintf(self::URI_USER_FOLLOWS_CHANNEL, $user) . $queryString);
134
    }
135
136
    /**
137
     * Get the status of a follow relationship
138
     * @param $user
139
     * @param $channel
140
     * @return \stdClass
141
     * @throws TwitchException
142
     */
143
    public function userFollowRelationship($user, $channel)
144
    {
145
        return $this->request->request(sprintf(self::URI_USER_FOLLOW_RELATION, $user, $channel));
146
    }
147
148
    /**
149
     * Set user to follow given channel
150
     * @param $user
151
     * @param $channel
152
     * @param $userToken
153
     * @return \stdClass
154
     * @throws TwitchException
155
     */
156 View Code Duplication
    public function userFollowChannel($user, $channel, $userToken)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
    {
158
        $queryString = $this->helper->buildQueryString(array(
159
            'oauth_token' => $userToken,
160
        ));
161
162
        return $this->request->request(sprintf(self::URI_USER_FOLLOW_RELATION, $user, $channel) . $queryString, 'PUT');
163
    }
164
165
    /**
166
     * Set user to unfollow given channel
167
     * @param $user
168
     * @param $channel
169
     * @param $userToken
170
     * @return \stdClass
171
     * @throws TwitchException
172
     */
173 View Code Duplication
    public function userUnfollowChannel($user, $channel, $userToken)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
    {
175
        $queryString = $this->helper->buildQueryString(array(
176
            'oauth_token' => $userToken,
177
        ));
178
179
        return $this->request->request(sprintf(self::URI_USER_FOLLOW_RELATION, $user, $channel) . $queryString, 'DELETE');
180
    }
181
182
    /**
183
     * Get the specified channel
184
     * @param string $channelName
185
     * @return \stdClass
186
     * @throws TwitchException
187
     */
188
    public function channelGet($channelName)
189
    {
190
        $channel = new Methods\Channel($this->request);
191
192
        return $channel->getChannels($channelName);
193
    }
194
195
    /**
196
     * Return team list for specified channel
197
     * @param string $channelName
198
     * @return \stdClass
199
     * @throws TwitchException
200
     */
201
    public function channelTeamsGet($channelName)
202
    {
203
        $channel = new Methods\Channel($this->request);
204
205
        return $channel->getTeams($channelName);
206
    }
207
208
    /**
209
     * Get the specified team
210
     * @param $teamName
211
     * @return \stdClass
212
     * @throws TwitchException
213
     */
214
    public function teamGet($teamName)
215
    {
216
        $team = new Methods\Team($this->request);
217
218
        return $team->getTeam($teamName);
219
    }
220
221
    /**
222
     * Returns a list of active teams
223
     * @param integer $limit
224
     * @param integer $offset
225
     * @return \stdClass
226
     * @throws TwitchException
227
     */
228 View Code Duplication
    public function teamList($limit = null, $offset = null)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
229
    {
230
        $queryString = $this->helper->buildQueryString(array(
231
            'limit' => $limit,
232
            'offset' => $offset,
233
        ));
234
235
        $team = new Methods\Team($this->request);
236
237
        return $team->getTeams($queryString);
238
    }
239
240
    /**
241
     * Get all team members
242
     * @param $teamName
243
     * @return mixed
244
     * @throws TwitchException
245
     */
246
    public function teamMembersAll($teamName)
247
    {
248
        return $this->request->teamRequest($teamName . '/all_channels')->channels;
249
    }
250
251
    /**
252
     * Returns an array of users who follow the specified channel
253
     * @param $channel
254
     * @param null $limit
255
     * @param null $offset
256
     * @return \stdClass
257
     * @throws TwitchException
258
     */
259 View Code Duplication
    public function channelFollows($channel, $limit = null, $offset = null)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
260
    {
261
        $queryString = $this->helper->buildQueryString(array(
262
            'limit' => $limit,
263
            'offset' => $offset,
264
        ));
265
266
        return $this->request->request(sprintf(self::URI_CHANNEL_FOLLOWS, $channel) . $queryString);
267
    }
268
269
    /**
270
     * Get the specified channel's stream
271
     * @param $channel
272
     * @return \stdClass
273
     * @throws TwitchException
274
     */
275
    public function streamGet($channel)
276
    {
277
        $stream = new Methods\Stream($this->request);
278
279
        return $stream->getStream($channel);
280
    }
281
282
    /**
283
     * Search live streams
284
     * @param $query
285
     * @param null $limit
286
     * @param null $offset
287
     * @return \stdClass
288
     * @throws TwitchException
289
     * @deprecated will be replaced by getStreams() function
290
     */
291 View Code Duplication
    public function streamSearch($query, $limit = null, $offset = null)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
292
    {
293
        $queryString = $this->helper->buildQueryString(array(
294
            'query' => $query,
295
            'limit' => $limit,
296
            'offset' => $offset,
297
        ));
298
299
        return $this->request->request(self::URI_STREAMS_SEARCH . $queryString);
300
    }
301
302
    /**
303
     * Summarize streams
304
     * @param null $game
305
     * @param array|null $channels
306
     * @param null $hls
307
     * @return \stdClass
308
     * @throws TwitchException
309
     */
310
    public function streamsSummarize($game = null, array $channels = null, $hls = null)
311
    {
312
        if (!empty($channels)) {
313
            $channels = implode(',', $channels);
314
        }
315
316
        $queryString = $this->helper->buildQueryString(array(
317
            'game' => $game,
318
            'channel' => $channels,
319
            'hls' => $hls,
320
        ));
321
322
        $stream = new Methods\Stream($this->request);
323
324
        return $stream->getSummary($queryString);
325
    }
326
327
    /**
328
     * Get featured streams
329
     * @param null $limit
330
     * @param null $offset
331
     * @param null $hls
332
     * @return \stdClass
333
     * @throws TwitchException
334
     */
335
    public function streamsFeatured($limit = null, $offset = null, $hls = null)
336
    {
337
        $queryString = $this->helper->buildQueryString(array(
338
            'limit' => $limit,
339
            'offset' => $offset,
340
            'hls' => $hls,
341
        ));
342
343
        $stream = new Methods\Stream($this->request);
344
345
        return $stream->getFeatured($queryString);
346
    }
347
348
    /**
349
     * Get streams by channel
350
     * @param $channels
351
     * @param null $limit
352
     * @param null $offset
353
     * @param null $embeddable
354
     * @param null $hls
355
     * @return \stdClass
356
     * @deprecated will be replaced by getStreams() function
357
     */
358
    public function streamsByChannels($channels, $limit = null, $offset = null, $embeddable = null, $hls = null)
359
    {
360
        $channelsString = implode(',', $channels);
361
362
        return $this->getStreams(null, $limit, $offset, $channelsString, $embeddable, $hls);
363
    }
364
365
    /**
366
     * Get streams by game
367
     * @param $game
368
     * @param null $limit
369
     * @param null $offset
370
     * @param null $embeddable
371
     * @param null $hls
372
     * @return \stdClass
373
     * @deprecated will be replaced by getStreams() function
374
     */
375
    public function streamsByGame($game, $limit = null, $offset = null, $embeddable = null, $hls = null)
376
    {
377
        return $this->getStreams($game, $limit, $offset, null, $embeddable, $hls);
378
    }
379
380
    /**
381
     * Get video
382
     * @param $video
383
     * @return \stdClass
384
     * @throws TwitchException
385
     */
386
    public function videoGet($video)
387
    {
388
        return $this->request->request(self::URI_VIDEO . $video);
389
    }
390
391
    /**
392
     * Get videos for a channel
393
     * @param $channel
394
     * @param null $limit
395
     * @param null $offset
396
     * @return \stdClass
397
     * @throws TwitchException
398
     */
399
    public function videosByChannel($channel, $limit = null, $offset = null)
400
    {
401
        $queryString = $this->helper->buildQueryString(array(
402
            'limit' => $limit,
403
            'offset' => $offset,
404
        ));
405
406
        return $this->request->request(self::URI_CHANNEL . $channel . '/' . self::URI_VIDEO . $queryString);
407
    }
408
409
    /**
410
     * Returns a links object to all other chat endpoints
411
     * @param string $channelName
412
     * @return \stdClass
413
     * @throws TwitchException
414
     */
415
    public function chatGet($channelName)
416
    {
417
        $chat = new Methods\Chat($this->request);
418
419
        return $chat->getChat($channelName);
420
    }
421
422
    /**
423
     * Get a chat's emoticons
424
     * @return \stdClass
425
     * @throws TwitchException
426
     */
427
    public function chatEmoticons()
428
    {
429
        $chat = new Methods\Chat($this->request);
430
431
        return $chat->getEmoticons();
432
    }
433
434
    /**
435
     * Returns a list of emoticons
436
     * @param string $emoteset
437
     * @return \stdClass
438
     * @throws TwitchException
439
     */
440 View Code Duplication
    public function chatEmoticonsImages($emoteset = null)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
441
    {
442
        $queryString = $this->helper->buildQueryString(array(
443
            'emotesets' => $emoteset,
444
        ));
445
446
        $chat = new Methods\Chat($this->request);
447
448
        return $chat->getEmoticonImages($queryString);
449
    }
450
451
    /**
452
     * Returns a list of chat badges
453
     * @param string $channelName
454
     * @return \stdClass
455
     * @throws TwitchException
456
     */
457
    public function chatBadges($channelName)
458
    {
459
        $chat = new Methods\Chat($this->request);
460
461
        return $chat->getBadges($channelName);
462
    }
463
464
    /**
465
     * Get top games
466
     * @param null $limit
467
     * @param null $offset
468
     * @return \stdClass
469
     * @throws TwitchException
470
     */
471 View Code Duplication
    public function gamesTop($limit = null, $offset = null)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
472
    {
473
        $queryString = $this->helper->buildQueryString(array(
474
            'limit' => $limit,
475
            'offset' => $offset,
476
        ));
477
478
        return $this->request->request(self::URI_GAMES_TOP . $queryString);
479
    }
480
481
    /**
482
     * Get HTML code for stream embedding
483
     * @param $channel
484
     * @param int $width
485
     * @param int $height
486
     * @param int $volume
487
     * @return string
488
     */
489 View Code Duplication
    public function embedStream($channel, $width = 620, $height = 378, $volume = 25)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
490
    {
491
        return '<object type="application/x-shockwave-flash"
492
                height="' . $height . '"
493
                width="' . $width . '"
494
                id="live_embed_player_flash"
495
                data="http://www.twitch.tv/widgets/live_embed_player.swf?channel=' . $channel . '"
496
                bgcolor="#000000">
497
                <param  name="allowFullScreen"
498
                    value="true" />
499
                <param  name="allowScriptAccess"
500
                    value="always" />
501
                <param  name="allowNetworking"
502
                    value="all" />
503
                <param  name="movie"
504
                    value="http://www.twitch.tv/widgets/live_embed_player.swf" />
505
                <param  name="flashvars"
506
                    value="hostname=www.twitch.tv&channel=' . $channel . '&auto_play=true&start_volume=' . $volume . '" />
507
                </object>';
508
    }
509
510
    /**
511
     * Get HTML code for video embedding
512
     * @param $channel
513
     * @param $chapterid
514
     * @param int $width
515
     * @param int $height
516
     * @param int $volume
517
     * @return string
518
     */
519 View Code Duplication
    public function embedVideo($channel, $chapterid, $width = 400, $height = 300, $volume = 25)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
520
    {
521
        return '<object bgcolor="#000000"
522
                    data="http://www.twitch.tv/widgets/archive_embed_player.swf"
523
                    width="' . $width . '"
524
                    height="' . $height . '"
525
                    id="clip_embed_player_flash"
526
                    type="application/x-shockwave-flash">
527
                <param  name="movie"
528
                    value="http://www.twitch.tv/widgets/archive_embed_player.swf" />
529
                <param  name="allowScriptAccess"
530
                    value="always" />
531
                <param  name="allowNetworking"
532
                    value="all" />
533
                <param name="allowFullScreen"
534
                    value="true" />
535
                <param  name="flashvars"
536
                    value="channel=' . $channel . '&start_volume=' . $volume . '&auto_play=false&chapter_id=' . $chapterid . '" />
537
                </object>';
538
    }
539
540
    /**
541
     * Get HTML code for chat embedding
542
     * @param $channel
543
     * @param int $width
544
     * @param int $height
545
     * @return string
546
     */
547
    public function embedChat($channel, $width = 400, $height = 300)
548
    {
549
        return '<iframe frameborder="0"
550
                    scrolling="no"
551
                    id="chat_embed"
552
                    src="http://twitch.tv/chat/embed?channel=' . $channel . '&amp;popout_chat=true"
553
                    height="' . $height . '"
554
                    width="' . $width . '">
555
                </iframe>';
556
    }
557
558
    /**
559
     * Get login URL for authentication
560
     * @param string $scope Specify which permissions your app requires (space separated list)
561
     * @return string
562
     * @throws TwitchException
563
     */
564 View Code Duplication
    public function authLoginURL($scope)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
565
    {
566
        if ($this->authConfig === false) {
567
            $this->authConfigException();
568
        }
569
570
        $queryString = $this->helper->buildQueryString(array(
571
            'response_type' => 'code',
572
            'client_id' => $this->authConfig['client_id'],
573
            'redirect_uri' => $this->authConfig['redirect_uri'],
574
            'scope' => $scope,
575
        ));
576
577
        $auth = new Methods\Auth;
578
579
        return $auth->getLoginURL($queryString);
580
    }
581
582
    /**
583
     * Get authentication access token
584
     * @param string $code returned after app authorization by user
585
     * @return \stdClass
586
     * @throws TwitchException
587
     */
588 View Code Duplication
    public function authAccessTokenGet($code)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
589
    {
590
        if ($this->authConfig === false) {
591
            $this->authConfigException();
592
        }
593
594
        $queryString = $this->helper->buildQueryString(array(
595
            'client_id' => $this->authConfig['client_id'],
596
            'client_secret' => $this->authConfig['client_secret'],
597
            'grant_type' => 'authorization_code',
598
            'redirect_uri' => $this->authConfig['redirect_uri'],
599
            'code' => $code,
600
        ));
601
602
        $auth = new Methods\Auth;
603
604
        return $auth->getAccessToken($queryString);
605
    }
606
607
    /**
608
     * Get the authenticated user
609
     *  - requires scope 'user_read'
610
     * @param string
611
     * @return \stdClass
612
     * @throws TwitchException
613
     */
614
    public function authUserGet($token)
615
    {
616
        if ($this->authConfig === false) {
617
            $this->authConfigException();
618
        }
619
620
        $queryString = $this->helper->buildQueryString(array(
621
            'oauth_token' => $token,
622
            'client_id' => $this->authConfig['client_id'],
623
        ));
624
625
        $user = new Methods\User($this->request);
626
        return $user->getUserAuth($queryString);
627
    }
628
629
    /**
630
     * Get the authenticated channel
631
     *  - requires scope 'channel_read'
632
     * @param string
633
     * @return \stdClass
634
     * @throws TwitchException
635
     */
636
    public function authChannelGet($token)
637
    {
638
        if ($this->authConfig === false) {
639
            $this->authConfigException();
640
        }
641
642
        $queryString = $this->helper->buildQueryString(array(
643
            'oauth_token' => $token,
644
            'client_id' => $this->authConfig['client_id'],
645
        ));
646
647
        $channels = new Methods\Channel($this->request);
648
649
        return $channels->getChannel($queryString);
650
    }
651
652
    /**
653
     * Update channel's status or game
654
     *  - requires scope 'channel_editor'
655
     * @param $token
656
     * @param string $channelName
657
     * @param string $status
658
     * @param string $game
659
     * @param integer $delay
660
     * @return \stdClass
661
     * @throws TwitchException
662
     */
663
    public function authChannelSet($token, $channelName, $status = null, $game = null, $delay = null)
664
    {
665
        if ($this->authConfig === false) {
666
            $this->authConfigException();
667
        }
668
669
        $queryString = $this->helper->buildQueryString(array(
670
            'oauth_token' => $token,
671
            'client_id' => $this->authConfig['client_id'],
672
        ));
673
674
        $data = $this->helper->buildQueryString(array(
675
            'channel[status]' => $status,
676
            'channel[game]' => $game,
677
            'channel[delay]' => $delay,
678
        ));
679
680
        $channel = new Methods\Channel($this->request);
681
682
        return $channel->setChannel($channelName, $queryString, $data);
683
    }
684
685
    /**
686
     * Resets channel's stream key
687
     *  - requires scope 'channel_stream'
688
     * @param string $channelName
689
     * @return \stdClass
690
     * @throws TwitchException
691
     */
692
    public function authChannelResetKey($channelName)
693
    {
694
        if ($this->authConfig === false) {
695
            $this->authConfigException();
696
        }
697
698
        $queryString = $this->helper->buildQueryString(array(
699
            'oauth_token' => $token,
0 ignored issues
show
Bug introduced by
The variable $token does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
700
            'client_id' => $this->authConfig['client_id'],
701
        ));
702
703
        $channel = new Methods\Channel($this->request);
704
705
        return $channel->resetStreamKey($channelName, $queryString);
706
    }
707
708
    /**
709
     * Returns an array of users who are editors of specified channel
710
     *  - requires scope 'channel_read'
711
     * @param string
712
     * @param string
713
     * @return \stdClass
714
     * @throws TwitchException
715
     */
716
    public function authChannelEditors($token, $channel)
717
    {
718
        if ($this->authConfig === false) {
719
            $this->authConfigException();
720
        }
721
722
        $queryString = $this->helper->buildQueryString(array(
723
            'oauth_token' => $token,
724
            'client_id' => $this->authConfig['client_id'],
725
        ));
726
727
        $channels = new Methods\Channel($this->request);
728
729
        return $channels->getEditors($channel, $queryString);
730
    }
731
732
    /**
733
     * Returns an array of subscriptions who are subscribed to specified channel
734
     *  - requires scope 'channel_subscriptions'
735
     * @param string $token - user's access token
736
     * @param string $channel
737
     * @param integer $limit - can be up to 100
738
     * @param integer $offset
739
     * @param string $direction can be DESC|ASC, if DESC - lasts will be showed first
740
     * @return \stdClass
741
     * @throws TwitchException
742
     */
743 View Code Duplication
    public function authChannelSubscriptions($token, $channel, $limit = 25, $offset = 0, $direction = 'DESC')
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
744
    {
745
        if ($this->authConfig === false) {
746
            $this->authConfigException();
747
        }
748
749
        $queryString = $this->helper->buildQueryString(array(
750
            'oauth_token' => $token,
751
            'client_id' => $this->authConfig['client_id'],
752
            'direction' => $direction,
753
            'limit' => $limit,
754
            'offset' => $offset
755
        ));
756
757
        $channels = new Methods\Subscription;
758
759
        $channels->getSubscriptions($channel, $queryString);
760
    }
761
762
    /**
763
     * List the live streams that the authenticated user is following
764
     *  - requires scope 'user_read'
765
     * @param string
766
     * @param integer $limit
767
     * @param integer $offset
768
     * @param bool $hls
769
     * @return \stdClass
770
     * @throws TwitchException
771
     */
772 View Code Duplication
    public function authStreamsFollowed($token, $limit = 25, $offset = 0, $hls = null)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
773
    {
774
        if ($this->authConfig === false) {
775
            $this->authConfigException();
776
        }
777
778
        $queryString = $this->helper->buildQueryString(array(
779
            'oauth_token' => $token,
780
            'client_id' => $this->authConfig['client_id'],
781
            'limit' => $limit,
782
            'offset' => $offset,
783
            'hls' => $hls,
784
        ));
785
786
        $user = new Methods\User($this->request);
787
        return $user->getFollowedStreams($queryString);
788
    }
789
790
    /**
791
     * Get streams helper
792
     * @param null $game
793
     * @param null $limit
794
     * @param null $offset
795
     * @param null $channels
796
     * @param null $embeddable
797
     * @param null $hls
798
     * @return \stdClass
799
     * @throws TwitchException
800
     */
801
    public function getStreams($game = null, $limit = null, $offset = null, $channels = null, $embeddable = null, $hls = null)
802
    {
803
        $params = array(
804
            'game' => $game,
805
            'limit' => $limit,
806
            'offset' => $offset,
807
            'channel' => !empty($channels) ? $channels : null,
808
            'embeddable' => $embeddable,
809
            'hls' => $hls,
810
        );
811
812
        $queryString = $this->helper->buildQueryString($params);
813
        $stream = new Methods\Stream($this->request);
814
815
        return $stream->getStreams($queryString);
816
    }
817
818
    /**
819
     * Validate parameters for authentication
820
     * @param array
821
     * @return boolean
822
     */
823
    private function configValidate($config)
824
    {
825
        $check = array('client_id', 'client_secret', 'redirect_uri');
826
827
        foreach ($check AS $val) {
828
            if (!array_key_exists($val, $config) ||
829
                (empty($config[$val]) ||
830
                    !is_string($config[$val]))
831
            ) {
832
                return false;
833
            }
834
        }
835
836
        return true;
837
    }
838
839
    /**
840
     * Configuration exception
841
     * @throws TwitchException
842
     */
843
    private function authConfigException()
844
    {
845
        throw new TwitchException('Cannot call authenticate functions without valid API configuration');
846
    }
847
}
848