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