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