Completed
Push — develop ( 6f3aea...5d5e6b )
by Josef
01:51
created

TwitchSDK   C

Complexity

Total Complexity 61

Size/Duplication

Total Lines 922
Duplicated Lines 30.59 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 65
Bugs 3 Features 4
Metric Value
wmc 61
c 65
b 3
f 4
lcom 1
cbo 13
dl 282
loc 922
rs 5

48 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 3
A setConfig() 0 10 2
A checkConfig() 0 6 2
A getConfigParam() 0 8 2
A status() 0 14 3
A userGet() 0 5 1
A userFollowChannels() 13 13 1
A userFollowRelationship() 0 6 1
A userFollowChannel() 11 11 1
A userUnfollowChannel() 10 10 1
A channelGet() 0 6 1
A channelTeamsGet() 0 6 1
A teamGet() 0 6 1
A teamList() 11 11 1
A teamMembersAll() 0 4 1
A channelFollows() 13 13 1
A streamGet() 0 6 1
A streamSearch() 10 10 1
A streamsSummarize() 16 16 2
A streamsFeatured() 12 12 1
A streamsByChannels() 0 6 1
A streamsByGame() 0 4 1
A videoGet() 0 6 1
A videosTop() 0 13 1
A videosByChannel() 13 13 1
A chatGet() 0 6 1
A chatEmoticons() 0 6 1
A chatEmoticonsImages() 10 10 1
A chatBadges() 0 6 1
A gamesTop() 11 11 1
A embedStream() 20 20 1
A embedVideo() 20 20 1
A embedChat() 0 10 1
A authLoginURL() 15 15 1
A authAccessTokenGet() 16 16 1
A authUserGet() 0 9 1
A authChannelGet() 10 10 1
A authChannelSet() 0 16 1
A authChannelResetKey() 10 10 1
A authChannelEditors() 10 10 1
A authChannelSubscriptions() 16 16 1
A authSubscribedUser() 10 10 1
A authSubscribedToChannel() 10 10 1
A authStreamsFollowed() 15 15 1
A getStreams() 0 16 2
B configValidate() 0 15 5
A getAuthString() 0 7 1
A configException() 0 4 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like TwitchSDK often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use TwitchSDK, and based on these observations, apply Extract Interface, too.

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