Total Complexity | 51 |
Total Lines | 610 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 2 | Features | 0 |
Complex classes like MalScraper 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.
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 MalScraper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
55 | class MalScraper |
||
56 | { |
||
57 | /** |
||
58 | * Cache class. |
||
59 | * |
||
60 | * @var Cache |
||
61 | */ |
||
62 | private $_cache; |
||
63 | |||
64 | /** |
||
65 | * Cache feature. |
||
66 | * |
||
67 | * @var bool |
||
68 | */ |
||
69 | private $_enable_cache = false; |
||
70 | |||
71 | /** |
||
72 | * Cache expiration time. |
||
73 | * |
||
74 | * @var int |
||
75 | */ |
||
76 | private $_cache_time = 86400; |
||
77 | |||
78 | /** |
||
79 | * Cache path. |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | private $_cache_path = __DIR__.'/Cache/'; |
||
84 | |||
85 | /** |
||
86 | * Convert to http response. |
||
87 | * |
||
88 | * @var bool |
||
89 | */ |
||
90 | private $_to_api = false; |
||
91 | |||
92 | /** |
||
93 | * Default constructor. |
||
94 | * |
||
95 | * @param array [optional] $config |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | public function __construct($config = false) |
||
100 | { |
||
101 | if (!empty($config['enable_cache']) && $config['enable_cache'] === true) { |
||
102 | // enable cache function |
||
103 | $this->_enable_cache = $config['enable_cache']; |
||
104 | |||
105 | // create cache class |
||
106 | $this->_cache = new Cache(); |
||
107 | |||
108 | // set cache path |
||
109 | if (!empty($config['cache_path'])) { |
||
110 | $this->_cache_path = $config['cache_path']; |
||
111 | } |
||
112 | $this->_cache->setCachePath($this->_cache_path); |
||
113 | |||
114 | // set cache time |
||
115 | if (!empty($config['cache_time'])) { |
||
116 | $this->_cache_time = $config['cache_time']; |
||
117 | } |
||
118 | } |
||
119 | |||
120 | // to http response function |
||
121 | if (!empty($config['to_api']) && $config['to_api'] === true) { |
||
122 | $this->_to_api = $config['to_api']; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Default call. |
||
128 | * |
||
129 | * @param string $method |
||
130 | * @param array $arguments |
||
131 | * |
||
132 | * @return string|array |
||
133 | */ |
||
134 | public function __call($method, $arguments) |
||
135 | { |
||
136 | $result = ''; |
||
137 | |||
138 | // if cache function enabled |
||
139 | if ($this->_enable_cache === true) { |
||
140 | $this->_cache->setCache(str_replace('get', '', $method)); |
||
141 | $this->_cache->eraseExpired($this->_cache_time); |
||
142 | |||
143 | $cacheName = $method.'('.implode(',', $arguments).')'; |
||
144 | $isCached = $this->_cache->isCached($cacheName); |
||
145 | |||
146 | // if cached |
||
147 | if ($isCached) { |
||
148 | $result = $this->_cache->retrieve($cacheName); |
||
149 | } else { |
||
150 | $data = call_user_func_array([$this, $method], $arguments); |
||
151 | $this->_cache->store($cacheName, $data, $this->_cache_time); |
||
152 | $result = $data; |
||
153 | } |
||
154 | } else { |
||
155 | $result = call_user_func_array([$this, $method], $arguments); |
||
156 | } |
||
157 | |||
158 | // if to api function enabled |
||
159 | if ($this->_to_api === true) { |
||
160 | return Helper::response($result); |
||
161 | } |
||
162 | |||
163 | return Helper::toResponse($result); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Get anime/manga information. |
||
168 | * |
||
169 | * @param string $type anime or manga |
||
170 | * @param int|string $id id of the anime or manga |
||
171 | * |
||
172 | * @return array |
||
173 | */ |
||
174 | private function getInfo($type, $id) |
||
175 | { |
||
176 | return (new Info($type, $id))->getAllInfo(); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Get character information. |
||
181 | * |
||
182 | * @param int|string $id id of the character |
||
183 | * |
||
184 | * @return array |
||
185 | */ |
||
186 | private function getCharacter($id) |
||
187 | { |
||
188 | return (new Character($id))->getAllInfo(); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Get people information. |
||
193 | * |
||
194 | * @param int|string $id id of the people |
||
195 | * |
||
196 | * @return array |
||
197 | */ |
||
198 | private function getPeople($id) |
||
199 | { |
||
200 | return (new People($id))->getAllInfo(); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Get review information. |
||
205 | * |
||
206 | * @param int|string $id id of the review |
||
207 | * |
||
208 | * @return array |
||
209 | */ |
||
210 | private function getReview($id) |
||
211 | { |
||
212 | return (new Review($id))->getAllInfo(); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Get recommendation information. |
||
217 | * |
||
218 | * @param string $type Either anime or manga |
||
219 | * @param int|string $id id of the first anime/manga |
||
220 | * @param int|string $id id of the second anime/manga |
||
221 | * |
||
222 | * @return array |
||
223 | */ |
||
224 | private function getRecommendation($type, $id1, $id2) |
||
225 | { |
||
226 | return (new Recommendation($type, $id1, $id2))->getAllInfo(); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Get anime/manga character + staff complete list. |
||
231 | * |
||
232 | * @param string $type Either anime or manga |
||
233 | * @param int|string $id id of the anime or manga |
||
234 | * |
||
235 | * @return array |
||
236 | */ |
||
237 | private function getCharacterStaff($type, $id) |
||
238 | { |
||
239 | return (new CharacterStaff($type, $id))->getAllInfo(); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Get anime/manga detail stat. |
||
244 | * |
||
245 | * @param string $type Either anime or manga |
||
246 | * @param int|string $id id of the anime or manga |
||
247 | * |
||
248 | * @return array |
||
249 | */ |
||
250 | private function getStat($type, $id) |
||
251 | { |
||
252 | return (new Stat($type, $id))->getAllInfo(); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Get anime video. |
||
257 | * |
||
258 | * @param int|string $id id of the anime |
||
259 | * @param int|string $page (Optional) Page number |
||
260 | * |
||
261 | * @return array |
||
262 | */ |
||
263 | private function getVideo($id, $page = 1) |
||
264 | { |
||
265 | return (new Video($id, $page))->getAllInfo(); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Get anime episode. |
||
270 | * |
||
271 | * @param int|string $id id of the anime |
||
272 | * @param int|string $page (Optional) Page number |
||
273 | * |
||
274 | * @return array |
||
275 | */ |
||
276 | private function getEpisode($id, $page = 1) |
||
277 | { |
||
278 | return (new Episode($id, $page))->getAllInfo(); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Get anime/manga additional pictures. |
||
283 | * |
||
284 | * @param string $type Either anime or manga |
||
285 | * @param int|string $id id of the anime or manga |
||
286 | * |
||
287 | * @return array |
||
288 | */ |
||
289 | private function getPicture($type, $id) |
||
290 | { |
||
291 | return (new Picture($type, $id))->getAllInfo(); |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * Get character additional pictures. |
||
296 | * |
||
297 | * @param int|string $id id of the character |
||
298 | * |
||
299 | * @return array |
||
300 | */ |
||
301 | private function getCharacterPicture($id) |
||
302 | { |
||
303 | return (new CharacterPeoplePicture('character', $id))->getAllInfo(); |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Get people additional pictures. |
||
308 | * |
||
309 | * @param int|string $id id of the people |
||
310 | * |
||
311 | * @return array |
||
312 | */ |
||
313 | private function getPeoplePicture($id) |
||
314 | { |
||
315 | return (new CharacterPeoplePicture('people', $id))->getAllInfo(); |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Get anime additional review. |
||
320 | * |
||
321 | * @param int|string $id id of the anime |
||
322 | * @param int|string $page (Optional) Page number |
||
323 | * |
||
324 | * @return array |
||
325 | */ |
||
326 | private function getAnimeReview($id, $page = 1) |
||
327 | { |
||
328 | return (new AnimeMangaReview('anime', $id, $page))->getAllInfo(); |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Get manga additional review. |
||
333 | * |
||
334 | * @param int|string $id id of the manga |
||
335 | * @param int|string $page (Optional) Page number |
||
336 | * |
||
337 | * @return array |
||
338 | */ |
||
339 | private function getMangaReview($id, $page = 1) |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Get anime additional recommendation. |
||
346 | * |
||
347 | * @param int|string $id id of the anime |
||
348 | * |
||
349 | * @return array |
||
350 | */ |
||
351 | private function getAnimeRecommendation($id) |
||
352 | { |
||
353 | return (new AnimeMangaRecommendation('anime', $id))->getAllInfo(); |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * Get manga additional recommendation. |
||
358 | * |
||
359 | * @param int|string $id id of the manga |
||
360 | * |
||
361 | * @return array |
||
362 | */ |
||
363 | private function getMangaRecommendation($id) |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * Get all anime produced by the studio/producer. |
||
370 | * |
||
371 | * @param int|string $id id of the studio/producer |
||
372 | * @param int|string $page (Optional) Page number |
||
373 | * |
||
374 | * @return array |
||
375 | */ |
||
376 | private function getStudioProducer($id, $page = 1) |
||
377 | { |
||
378 | return (new Producer('anime', 'producer', $id, $page))->getAllInfo(); |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Get all manga serialized by the magazine. |
||
383 | * |
||
384 | * @param int|string $id id of the magazine |
||
385 | * @param int|string $page (Optional) Page number |
||
386 | * |
||
387 | * @return array |
||
388 | */ |
||
389 | private function getMagazine($id, $page = 1) |
||
390 | { |
||
391 | return (new Producer('manga', 'producer', $id, $page))->getAllInfo(); |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Get all anime or manga that has the genre. |
||
396 | * |
||
397 | * @param string $type Either anime or manga |
||
398 | * @param int|string $id id of the genre |
||
399 | * @param int|string $page (Optional) Page number |
||
400 | * |
||
401 | * @return array |
||
402 | */ |
||
403 | private function getGenre($type, $id, $page = 1) |
||
404 | { |
||
405 | return (new Producer($type, 'genre', $id, $page))->getAllInfo(); |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Get list of all anime genre. |
||
410 | * |
||
411 | * @return array |
||
412 | */ |
||
413 | private function getAllAnimeGenre() |
||
414 | { |
||
415 | return (new AllGenre('anime'))->getAllInfo(); |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * Get list of all manga genre. |
||
420 | * |
||
421 | * @return array |
||
422 | */ |
||
423 | private function getAllMangaGenre() |
||
424 | { |
||
425 | return (new AllGenre('manga'))->getAllInfo(); |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * Get list of all anime studio/producer. |
||
430 | * |
||
431 | * @return array |
||
432 | */ |
||
433 | private function getAllStudioProducer() |
||
434 | { |
||
435 | return (new AllProducer('anime'))->getAllInfo(); |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Get list of all manga magazine. |
||
440 | * |
||
441 | * @return array |
||
442 | */ |
||
443 | private function getAllMagazine() |
||
444 | { |
||
445 | return (new AllProducer('manga'))->getAllInfo(); |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Get list of all review. |
||
450 | * |
||
451 | * @param int|string $type Type of review |
||
452 | * @param int|string $page (Optional) Page number |
||
453 | * |
||
454 | * @return array |
||
455 | */ |
||
456 | private function getAllReview($type = 'anime', $page = 1) |
||
457 | { |
||
458 | return (new AllReview($type, $page))->getAllInfo(); |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * Get list of all recommendation. |
||
463 | * |
||
464 | * @param int|string $type Either anime or manga |
||
465 | * @param int|string $page (Optional) Page number |
||
466 | * |
||
467 | * @return array |
||
468 | */ |
||
469 | private function getAllRecommendation($type = 'anime', $page = 1) |
||
470 | { |
||
471 | return (new AllRecommendation($type, $page))->getAllInfo(); |
||
472 | } |
||
473 | |||
474 | /** |
||
475 | * Get anime search result. |
||
476 | * |
||
477 | * @param string $query Search query |
||
478 | * @param int|string $page (Optional) Page number |
||
479 | * |
||
480 | * @return array |
||
481 | */ |
||
482 | private function searchAnime($query, $page = 1) |
||
483 | { |
||
484 | return (new SearchAnimeManga('anime', $query, $page))->getAllInfo(); |
||
485 | } |
||
486 | |||
487 | /** |
||
488 | * Get manga search result. |
||
489 | * |
||
490 | * @param string $query Search query |
||
491 | * @param int|string $page (Optional) Page number |
||
492 | * |
||
493 | * @return array |
||
494 | */ |
||
495 | private function searchManga($query, $page = 1) |
||
496 | { |
||
497 | return (new SearchAnimeManga('manga', $query, $page))->getAllInfo(); |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * Get character search result. |
||
502 | * |
||
503 | * @param string $query Search query |
||
504 | * @param int|string $page (Optional) Page number |
||
505 | * |
||
506 | * @return array |
||
507 | */ |
||
508 | private function searchCharacter($query, $page = 1) |
||
509 | { |
||
510 | return (new SearchCharacterPeople('character', $query, $page))->getAllInfo(); |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * Get people search result. |
||
515 | * |
||
516 | * @param string $query Search query |
||
517 | * @param int|string $page (Optional) Page number |
||
518 | * |
||
519 | * @return array |
||
520 | */ |
||
521 | private function searchPeople($query, $page = 1) |
||
522 | { |
||
523 | return (new SearchCharacterPeople('people', $query, $page))->getAllInfo(); |
||
524 | } |
||
525 | |||
526 | /** |
||
527 | * Get user search result. |
||
528 | * |
||
529 | * @param string $query Search query |
||
530 | * @param int|string $page (Optional) Page number |
||
531 | * |
||
532 | * @return array |
||
533 | */ |
||
534 | private function searchUser($query, $page = 1) |
||
535 | { |
||
536 | return (new SearchUser($query, $page))->getAllInfo(); |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * Get seasonal anime. |
||
541 | * |
||
542 | * @param string|int|bool $year (Optional) Season year |
||
543 | * @param string|bool $season (Optional) Season (summer,spring,fall,winter) |
||
544 | * |
||
545 | * @return array |
||
546 | */ |
||
547 | private function getSeason($year = false, $season = false) |
||
548 | { |
||
549 | return (new Season($year, $season))->getAllInfo(); |
||
550 | } |
||
551 | |||
552 | /** |
||
553 | * Get top anime. |
||
554 | * |
||
555 | * @param string $type (Optional) Type of anime |
||
556 | * @param int|string $page (Optional) Page number |
||
557 | * |
||
558 | * @return array |
||
559 | */ |
||
560 | private function getTopAnime($type = 0, $page = 1) |
||
561 | { |
||
562 | return (new Top('anime', $type, $page))->getAllInfo(); |
||
563 | } |
||
564 | |||
565 | /** |
||
566 | * Get top manga. |
||
567 | * |
||
568 | * @param string $type (Optional) Type of manga |
||
569 | * @param int|string $page (Optional) Page number |
||
570 | * |
||
571 | * @return array |
||
572 | */ |
||
573 | private function getTopManga($type = 0, $page = 1) |
||
574 | { |
||
575 | return (new Top('manga', $type, $page))->getAllInfo(); |
||
576 | } |
||
577 | |||
578 | /** |
||
579 | * Get top character. |
||
580 | * |
||
581 | * @param int|string $page (Optional) Page number |
||
582 | * |
||
583 | * @return array |
||
584 | */ |
||
585 | private function getTopCharacter($page = 1) |
||
588 | } |
||
589 | |||
590 | /** |
||
591 | * Get top people. |
||
592 | * |
||
593 | * @param int|string $page (Optional) Page number |
||
594 | * |
||
595 | * @return array |
||
596 | */ |
||
597 | private function getTopPeople($page = 1) |
||
600 | } |
||
601 | |||
602 | /** |
||
603 | * Get user info. |
||
604 | * |
||
605 | * @param string $user Username |
||
606 | * |
||
607 | * @return array |
||
608 | */ |
||
609 | private function getUser($user) |
||
610 | { |
||
611 | return (new User($user))->getAllInfo(); |
||
612 | } |
||
613 | |||
614 | /** |
||
615 | * Get user friend list. |
||
616 | * |
||
617 | * @param string $user Username |
||
618 | * |
||
619 | * @return array |
||
620 | */ |
||
621 | private function getUserFriend($user) |
||
622 | { |
||
623 | return (new Friend($user))->getAllInfo(); |
||
624 | } |
||
625 | |||
626 | /** |
||
627 | * Get user history. |
||
628 | * |
||
629 | * @param string $user Username |
||
630 | * @param string|bool $type (Optional) Either anime or manga |
||
631 | * |
||
632 | * @return array |
||
633 | */ |
||
634 | private function getUserHistory($user, $type = false) |
||
635 | { |
||
636 | return (new History($user, $type))->getAllInfo(); |
||
637 | } |
||
638 | |||
639 | /** |
||
640 | * Get user list. |
||
641 | * |
||
642 | * @param string $user Username |
||
643 | * @param string $type (Optional) Either anime or manga |
||
644 | * @param string $status (Optional) Anime/manga status |
||
645 | * |
||
646 | * @return array |
||
647 | */ |
||
648 | private function getUserList($user, $type = 'anime', $status = 7) |
||
651 | } |
||
652 | |||
653 | /** |
||
654 | * Get user cover. |
||
655 | * |
||
656 | * @param string $user Username |
||
657 | * @param string $type (Optional) Either anime or manga |
||
658 | * @param string|bool $style (Optional) CSS style for the cover |
||
659 | * |
||
660 | * @return string |
||
661 | */ |
||
662 | private function getUserCover($user, $type = 'anime', $style = false) |
||
663 | { |
||
664 | return (new UserCover($user, $type, $style))->getAllInfo(); |
||
665 | } |
||
666 | } |
||
667 |