Completed
Push — develop ( 3563b6...93cfb3 )
by Josef
03:08
created

TwitchSDK   D

Complexity

Total Complexity 63

Size/Duplication

Total Lines 879
Duplicated Lines 27.3 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 55
Bugs 3 Features 4
Metric Value
wmc 63
c 55
b 3
f 4
lcom 1
cbo 13
dl 240
loc 879
rs 4.8484

43 Methods

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