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 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 |
||
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
|
|||
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
|
|||
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
|
|||
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
|
|||
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
|
|||
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
|
|||
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
|
|||
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
|
|||
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
|
|||
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
|
|||
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
|
|||
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
|
|||
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
|
|||
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 . '&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
|
|||
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
|
|||
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 | View Code Duplication | public function authUserGet($token) |
|
1 ignored issue
–
show
|
|||
681 | { |
||
682 | $this->checkConfig(); |
||
683 | |||
684 | $queryString = $this->helper->buildQueryString(array( |
||
685 | 'oauth_token' => $token, |
||
686 | 'client_id' => $this->getConfigParam('client_id'), |
||
687 | )); |
||
688 | |||
689 | $user = new Methods\User($this->request); |
||
690 | return $user->getUserAuth($queryString); |
||
691 | } |
||
692 | |||
693 | /** |
||
694 | * Get the authenticated channel |
||
695 | * - requires scope 'channel_read' |
||
696 | * @param string |
||
697 | * @return \stdClass |
||
698 | * @throws TwitchException |
||
699 | */ |
||
700 | View Code Duplication | public function authChannelGet($token) |
|
1 ignored issue
–
show
|
|||
701 | { |
||
702 | $this->checkConfig(); |
||
703 | |||
704 | $queryString = $this->helper->buildQueryString(array( |
||
705 | 'oauth_token' => $token, |
||
706 | 'client_id' => $this->getConfigParam('client_id'), |
||
707 | )); |
||
708 | |||
709 | $channels = new Methods\Channel($this->request); |
||
710 | |||
711 | return $channels->getChannel($queryString); |
||
712 | } |
||
713 | |||
714 | /** |
||
715 | * Update channel's status or game |
||
716 | * - requires scope 'channel_editor' |
||
717 | * @param $token |
||
718 | * @param string $channelName |
||
719 | * @param string $status |
||
720 | * @param string $game |
||
721 | * @param integer $delay |
||
722 | * @return \stdClass |
||
723 | * @throws TwitchException |
||
724 | */ |
||
725 | public function authChannelSet($token, $channelName, $status = null, $game = null, $delay = null) |
||
726 | { |
||
727 | $this->checkConfig(); |
||
728 | |||
729 | $queryString = $this->helper->buildQueryString(array( |
||
730 | 'oauth_token' => $token, |
||
731 | 'client_id' => $this->getConfigParam('client_id'), |
||
732 | )); |
||
733 | |||
734 | $data = $this->helper->buildQueryString(array( |
||
735 | 'channel[status]' => $status, |
||
736 | 'channel[game]' => $game, |
||
737 | 'channel[delay]' => $delay, |
||
738 | )); |
||
739 | |||
740 | $channel = new Methods\Channel($this->request); |
||
741 | |||
742 | return $channel->setChannel($channelName, $queryString, $data); |
||
743 | } |
||
744 | |||
745 | /** |
||
746 | * Resets channel's stream key |
||
747 | * - requires scope 'channel_stream' |
||
748 | * @param string $token |
||
749 | * @param string $channelName |
||
750 | * @return \stdClass |
||
751 | * @throws TwitchException |
||
752 | */ |
||
753 | View Code Duplication | public function authChannelResetKey($token, $channelName) |
|
1 ignored issue
–
show
|
|||
754 | { |
||
755 | $this->checkConfig(); |
||
756 | |||
757 | $queryString = $this->helper->buildQueryString(array( |
||
758 | 'oauth_token' => $token, |
||
759 | 'client_id' => $this->getConfigParam('client_id'), |
||
760 | )); |
||
761 | |||
762 | $channel = new Methods\Channel($this->request); |
||
763 | |||
764 | return $channel->resetStreamKey($channelName, $queryString); |
||
765 | } |
||
766 | |||
767 | /** |
||
768 | * Returns an array of users who are editors of specified channel |
||
769 | * - requires scope 'channel_read' |
||
770 | * @param string |
||
771 | * @param string |
||
772 | * @return \stdClass |
||
773 | * @throws TwitchException |
||
774 | */ |
||
775 | View Code Duplication | public function authChannelEditors($token, $channel) |
|
1 ignored issue
–
show
|
|||
776 | { |
||
777 | $this->checkConfig(); |
||
778 | |||
779 | $queryString = $this->helper->buildQueryString(array( |
||
780 | 'oauth_token' => $token, |
||
781 | 'client_id' => $this->getConfigParam('client_id'), |
||
782 | )); |
||
783 | |||
784 | $channels = new Methods\Channel($this->request); |
||
785 | |||
786 | return $channels->getEditors($channel, $queryString); |
||
787 | } |
||
788 | |||
789 | /** |
||
790 | * Returns an array of subscriptions who are subscribed to specified channel |
||
791 | * - requires scope 'channel_subscriptions' |
||
792 | * @param string $token - user's access token |
||
793 | * @param string $channel |
||
794 | * @param integer $limit - can be up to 100 |
||
795 | * @param integer $offset |
||
796 | * @param string $direction can be DESC|ASC, if DESC - lasts will be showed first |
||
797 | * @return \stdClass |
||
798 | * @throws TwitchException |
||
799 | */ |
||
800 | View Code Duplication | public function authChannelSubscriptions($token, $channel, $limit = 25, $offset = 0, $direction = 'DESC') |
|
1 ignored issue
–
show
|
|||
801 | { |
||
802 | $this->checkConfig(); |
||
803 | |||
804 | $queryString = $this->helper->buildQueryString(array( |
||
805 | 'oauth_token' => $token, |
||
806 | 'client_id' => $this->getConfigParam('client_id'), |
||
807 | 'direction' => $direction, |
||
808 | 'limit' => $limit, |
||
809 | 'offset' => $offset |
||
810 | )); |
||
811 | |||
812 | $subscription = new Methods\Subscription($this->request); |
||
813 | |||
814 | $subscription->getSubscriptions($channel, $queryString); |
||
815 | } |
||
816 | |||
817 | /** |
||
818 | * Returns user object if that user is subscribed |
||
819 | * - requires scope 'channel_check_subscription' for channel |
||
820 | * @param string $token |
||
821 | * @param string $channel |
||
822 | * @param string $user |
||
823 | * @return \stdClass |
||
824 | * @throws TwitchException |
||
825 | */ |
||
826 | View Code Duplication | public function authSubscribedUser($token, $channel, $user) |
|
1 ignored issue
–
show
|
|||
827 | { |
||
828 | $this->checkConfig(); |
||
829 | |||
830 | $queryString = $this->helper->buildQueryString(array( |
||
831 | 'oauth_token' => $token, |
||
832 | 'client_id' => $this->getConfigParam('client_id'), |
||
833 | )); |
||
834 | |||
835 | $subscription = new Methods\Subscription($this->request); |
||
836 | |||
837 | return $subscription->getSubscribedUser($channel, $user, $queryString); |
||
838 | } |
||
839 | |||
840 | /** |
||
841 | * Returns a channel object that user subscribes to |
||
842 | * - requires scope 'user_subscriptions' for user |
||
843 | * @param string $token |
||
844 | * @param string $user |
||
845 | * @param string $channel |
||
846 | * @return \stdClass |
||
847 | * @throws TwitchException |
||
848 | */ |
||
849 | View Code Duplication | public function authSubscribedToChannel($token, $user, $channel) |
|
1 ignored issue
–
show
|
|||
850 | { |
||
851 | $this->checkConfig(); |
||
852 | |||
853 | $queryString = $this->helper->buildQueryString(array( |
||
854 | 'oauth_token' => $token, |
||
855 | 'client_id' => $this->getConfigParam('client_id'), |
||
856 | )); |
||
857 | |||
858 | $subscription = new Methods\Subscription($this->request); |
||
859 | |||
860 | return $subscription->getSubscribedToChannel($user, $channel, $queryString); |
||
861 | } |
||
862 | |||
863 | /** |
||
864 | * List the live streams that the authenticated user is following |
||
865 | * - requires scope 'user_read' |
||
866 | * @param string |
||
867 | * @param integer $limit |
||
868 | * @param integer $offset |
||
869 | * @param bool $hls |
||
870 | * @return \stdClass |
||
871 | * @throws TwitchException |
||
872 | */ |
||
873 | View Code Duplication | public function authStreamsFollowed($token, $limit = 25, $offset = 0, $hls = null) |
|
1 ignored issue
–
show
|
|||
874 | { |
||
875 | $this->checkConfig(); |
||
876 | |||
877 | $queryString = $this->helper->buildQueryString(array( |
||
878 | 'oauth_token' => $token, |
||
879 | 'client_id' => $this->getConfigParam('client_id'), |
||
880 | 'limit' => $limit, |
||
881 | 'offset' => $offset, |
||
882 | 'hls' => $hls, |
||
883 | )); |
||
884 | |||
885 | $user = new Methods\User($this->request); |
||
886 | return $user->getFollowedStreams($queryString); |
||
887 | } |
||
888 | |||
889 | /** |
||
890 | * Get streams helper |
||
891 | * @param null $game |
||
892 | * @param null $limit |
||
893 | * @param null $offset |
||
894 | * @param string|null $channels |
||
895 | * @param null $embeddable |
||
896 | * @param null $hls |
||
897 | * @return \stdClass |
||
898 | * @throws TwitchException |
||
899 | */ |
||
900 | public function getStreams($game = null, $limit = null, $offset = null, $channels = null, $embeddable = null, $hls = null) |
||
916 | |||
917 | /** |
||
918 | * Validate parameters for authentication |
||
919 | * @param array |
||
920 | * @return boolean |
||
921 | */ |
||
922 | private function configValidate($config) |
||
937 | |||
938 | /** |
||
939 | * Configuration exception |
||
940 | * @throws TwitchException |
||
941 | */ |
||
942 | private function configException() |
||
943 | { |
||
944 | throw new TwitchException('Cannot call authenticate functions without valid API configuration'); |
||
945 | } |
||
946 | } |
||
947 |
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.