Completed
Push — develop ( 02d355...41c776 )
by Josef
01:54
created

TwitchSDK::authChannelResetKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
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
 */
18
class TwitchSDK
19
{
20
    /**
21
     * @var array|bool
22
     * @todo Create setter and getter with data validation
23
     */
24
    protected $authConfig = false;
25
26
    /** @var TwitchRequest */
27
    protected $request;
28
29
    /** @var Helper */
30
    protected $helper;
31
32
    /**
33
     * TwitchAPI URI's
34
     */
35
    const URI_USER_FOLLOWS_CHANNEL = '/users/%s/follows/channels';
36
    const URI_USER_FOLLOW_RELATION = '/users/%s/follows/channels/%s';
37
    const URI_CHANNEL = 'channels/';
38
    const URI_CHANNEL_FOLLOWS = 'channels/%s/follows';
39
    const URI_STREAMS_SEARCH = 'search/streams/';
40
    const URI_VIDEO = 'videos/';
41
    const URI_CHAT = 'chat/';
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 $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
     * Get the specified channel's chat
411
     * @param $channel
412
     * @return \stdClass
413
     * @throws TwitchException
414
     */
415
    public function chatGet($channel)
416
    {
417
        return $this->request->request(self::URI_CHAT . $channel);
418
    }
419
420
    /**
421
     * Get a chat's emoticons
422
     * @return \stdClass
423
     * @throws TwitchException
424
     */
425
    public function chatEmoticons()
426
    {
427
        return $this->request->request(self::URI_CHAT_EMOTICONS);
428
    }
429
430
    /**
431
     * Get top games
432
     * @param null $limit
433
     * @param null $offset
434
     * @return \stdClass
435
     * @throws TwitchException
436
     */
437 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...
438
    {
439
        $queryString = $this->helper->buildQueryString(array(
440
            'limit' => $limit,
441
            'offset' => $offset,
442
        ));
443
444
        return $this->request->request(self::URI_GAMES_TOP . $queryString);
445
    }
446
447
    /**
448
     * Get HTML code for stream embedding
449
     * @param $channel
450
     * @param int $width
451
     * @param int $height
452
     * @param int $volume
453
     * @return string
454
     */
455 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...
456
    {
457
        return '<object type="application/x-shockwave-flash"
458
                height="' . $height . '"
459
                width="' . $width . '"
460
                id="live_embed_player_flash"
461
                data="http://www.twitch.tv/widgets/live_embed_player.swf?channel=' . $channel . '"
462
                bgcolor="#000000">
463
                <param  name="allowFullScreen"
464
                    value="true" />
465
                <param  name="allowScriptAccess"
466
                    value="always" />
467
                <param  name="allowNetworking"
468
                    value="all" />
469
                <param  name="movie"
470
                    value="http://www.twitch.tv/widgets/live_embed_player.swf" />
471
                <param  name="flashvars"
472
                    value="hostname=www.twitch.tv&channel=' . $channel . '&auto_play=true&start_volume=' . $volume . '" />
473
                </object>';
474
    }
475
476
    /**
477
     * Get HTML code for video embedding
478
     * @param $channel
479
     * @param $chapterid
480
     * @param int $width
481
     * @param int $height
482
     * @param int $volume
483
     * @return string
484
     */
485 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...
486
    {
487
        return '<object bgcolor="#000000"
488
                    data="http://www.twitch.tv/widgets/archive_embed_player.swf"
489
                    width="' . $width . '"
490
                    height="' . $height . '"
491
                    id="clip_embed_player_flash"
492
                    type="application/x-shockwave-flash">
493
                <param  name="movie"
494
                    value="http://www.twitch.tv/widgets/archive_embed_player.swf" />
495
                <param  name="allowScriptAccess"
496
                    value="always" />
497
                <param  name="allowNetworking"
498
                    value="all" />
499
                <param name="allowFullScreen"
500
                    value="true" />
501
                <param  name="flashvars"
502
                    value="channel=' . $channel . '&start_volume=' . $volume . '&auto_play=false&chapter_id=' . $chapterid . '" />
503
                </object>';
504
    }
505
506
    /**
507
     * Get HTML code for chat embedding
508
     * @param $channel
509
     * @param int $width
510
     * @param int $height
511
     * @return string
512
     */
513
    public function embedChat($channel, $width = 400, $height = 300)
514
    {
515
        return '<iframe frameborder="0"
516
                    scrolling="no"
517
                    id="chat_embed"
518
                    src="http://twitch.tv/chat/embed?channel=' . $channel . '&amp;popout_chat=true"
519
                    height="' . $height . '"
520
                    width="' . $width . '">
521
                </iframe>';
522
    }
523
524
    /**
525
     * Get login URL for authentication
526
     * @param string $scope Specify which permissions your app requires (space separated list)
527
     * @return string
528
     * @throws TwitchException
529
     */
530 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...
531
    {
532
        if ($this->authConfig === false) {
533
            $this->authConfigException();
534
        }
535
536
        $queryString = $this->helper->buildQueryString(array(
537
            'response_type' => 'code',
538
            'client_id' => $this->authConfig['client_id'],
539
            'redirect_uri' => $this->authConfig['redirect_uri'],
540
            'scope' => $scope,
541
        ));
542
543
        $auth = new Methods\Auth;
544
545
        return $auth->getLoginURL($queryString);
546
    }
547
548
    /**
549
     * Get authentication access token
550
     * @param string $code returned after app authorization by user
551
     * @return \stdClass
552
     * @throws TwitchException
553
     */
554 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...
555
    {
556
        if ($this->authConfig === false) {
557
            $this->authConfigException();
558
        }
559
560
        $queryString = $this->helper->buildQueryString(array(
561
            'client_id' => $this->authConfig['client_id'],
562
            'client_secret' => $this->authConfig['client_secret'],
563
            'grant_type' => 'authorization_code',
564
            'redirect_uri' => $this->authConfig['redirect_uri'],
565
            'code' => $code,
566
        ));
567
568
        $auth = new Methods\Auth;
569
570
        return $auth->getAccessToken($queryString);
571
    }
572
573
    /**
574
     * Get the authenticated user
575
     *  - requires scope 'user_read'
576
     * @param string
577
     * @return \stdClass
578
     * @throws TwitchException
579
     */
580
    public function authUserGet($token)
581
    {
582
        if ($this->authConfig === false) {
583
            $this->authConfigException();
584
        }
585
586
        $queryString = $this->helper->buildQueryString(array(
587
            'oauth_token' => $token,
588
            'client_id' => $this->authConfig['client_id'],
589
        ));
590
591
        $user = new Methods\User($this->request);
592
        return $user->getUserAuth($queryString);
593
    }
594
595
    /**
596
     * Get the authenticated channel
597
     *  - requires scope 'channel_read'
598
     * @param string
599
     * @return \stdClass
600
     * @throws TwitchException
601
     */
602
    public function authChannelGet($token)
603
    {
604
        if ($this->authConfig === false) {
605
            $this->authConfigException();
606
        }
607
608
        $queryString = $this->helper->buildQueryString(array(
609
            'oauth_token' => $token,
610
            'client_id' => $this->authConfig['client_id'],
611
        ));
612
613
        $channels = new Methods\Channel($this->request);
614
615
        return $channels->getChannel($queryString);
616
    }
617
618
    /**
619
     * Update channel's status or game
620
     *  - requires scope 'channel_editor'
621
     * @param $token
622
     * @param string $channelName
623
     * @param string $status
624
     * @param string $game
625
     * @param integer $delay
626
     * @return \stdClass
627
     * @throws TwitchException
628
     */
629
    public function authChannelSet($token, $channelName, $status = null, $game = null, $delay = null)
630
    {
631
        if ($this->authConfig === false) {
632
            $this->authConfigException();
633
        }
634
635
        $queryString = $this->helper->buildQueryString(array(
636
            'oauth_token' => $token,
637
            'client_id' => $this->authConfig['client_id'],
638
        ));
639
640
        $data = $this->helper->buildQueryString(array(
641
            'channel[status]' => $status,
642
            'channel[game]' => $game,
643
            'channel[delay]' => $delay,
644
        ));
645
646
        $channel = new Methods\Channel($this->request);
647
648
        return $channel->setChannel($channelName, $queryString, $data);
649
    }
650
651
    /**
652
     * Resets channel's stream key
653
     *  - requires scope 'channel_stream'
654
     * @param string $channelName
655
     * @return \stdClass
656
     * @throws TwitchException
657
     */
658
    public function authChannelResetKey($channelName)
659
    {
660
        if ($this->authConfig === false) {
661
            $this->authConfigException();
662
        }
663
664
        $queryString = $this->helper->buildQueryString(array(
665
            '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...
666
            'client_id' => $this->authConfig['client_id'],
667
        ));
668
669
        $channel = new Methods\Channel($this->request);
670
671
        return $channel->resetStreamKey($channelName, $queryString);
672
    }
673
674
    /**
675
     * Returns an array of users who are editors of specified channel
676
     *  - requires scope 'channel_read'
677
     * @param string
678
     * @param string
679
     * @return \stdClass
680
     * @throws TwitchException
681
     */
682
    public function authChannelEditors($token, $channel)
683
    {
684
        if ($this->authConfig === false) {
685
            $this->authConfigException();
686
        }
687
688
        $queryString = $this->helper->buildQueryString(array(
689
            'oauth_token' => $token,
690
            'client_id' => $this->authConfig['client_id'],
691
        ));
692
693
        $channels = new Methods\Channel($this->request);
694
695
        return $channels->getEditors($channel, $queryString);
696
    }
697
698
    /**
699
     * Returns an array of subscriptions who are subscribed to specified channel
700
     *  - requires scope 'channel_subscriptions'
701
     * @param string $token - user's access token
702
     * @param string $channel
703
     * @param integer $limit - can be up to 100
704
     * @param integer $offset
705
     * @param string $direction can be DESC|ASC, if DESC - lasts will be showed first
706
     * @return \stdClass
707
     * @throws TwitchException
708
     */
709 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...
710
    {
711
        if ($this->authConfig === false) {
712
            $this->authConfigException();
713
        }
714
715
        $queryString = $this->helper->buildQueryString(array(
716
            'oauth_token' => $token,
717
            'client_id' => $this->authConfig['client_id'],
718
            'direction' => $direction,
719
            'limit' => $limit,
720
            'offset' => $offset
721
        ));
722
723
        $channels = new Methods\Subscription;
724
725
        $channels->getSubscriptions($channel, $queryString);
726
    }
727
728
    /**
729
     * List the live streams that the authenticated user is following
730
     *  - requires scope 'user_read'
731
     * @param string
732
     * @param integer $limit
733
     * @param integer $offset
734
     * @param bool $hls
735
     * @return \stdClass
736
     * @throws TwitchException
737
     */
738 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...
739
    {
740
        if ($this->authConfig === false) {
741
            $this->authConfigException();
742
        }
743
744
        $queryString = $this->helper->buildQueryString(array(
745
            'oauth_token' => $token,
746
            'client_id' => $this->authConfig['client_id'],
747
            'limit' => $limit,
748
            'offset' => $offset,
749
            'hls' => $hls,
750
        ));
751
752
        $user = new Methods\User($this->request);
753
        return $user->getFollowedStreams($queryString);
754
    }
755
756
    /**
757
     * Get streams helper
758
     * @param null $game
759
     * @param null $limit
760
     * @param null $offset
761
     * @param null $channels
762
     * @param null $embeddable
763
     * @param null $hls
764
     * @return \stdClass
765
     * @throws TwitchException
766
     */
767
    public function getStreams($game = null, $limit = null, $offset = null, $channels = null, $embeddable = null, $hls = null)
768
    {
769
        $params = array(
770
            'game' => $game,
771
            'limit' => $limit,
772
            'offset' => $offset,
773
            'channel' => !empty($channels) ? $channels : null,
774
            'embeddable' => $embeddable,
775
            'hls' => $hls,
776
        );
777
778
        $queryString = $this->helper->buildQueryString($params);
779
        $stream = new Methods\Stream($this->request);
780
781
        return $stream->getStreams($queryString);
782
    }
783
784
    /**
785
     * Validate parameters for authentication
786
     * @param array
787
     * @return boolean
788
     */
789
    private function configValidate($config)
790
    {
791
        $check = array('client_id', 'client_secret', 'redirect_uri');
792
793
        foreach ($check AS $val) {
794
            if (!array_key_exists($val, $config) ||
795
                (empty($config[$val]) ||
796
                    !is_string($config[$val]))
797
            ) {
798
                return false;
799
            }
800
        }
801
802
        return true;
803
    }
804
805
    /**
806
     * Configuration exception
807
     * @throws TwitchException
808
     */
809
    private function authConfigException()
810
    {
811
        throw new TwitchException('Cannot call authenticate functions without valid API configuration');
812
    }
813
}
814