Passed
Push — master ( c893b5...77095f )
by axel
04:38 queued 02:27
created

MalScraper::getTopPeople()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * rl404 - MalScraper.
4
 *
5
 * Unofficial PHP API which scraps and parses page source of MyAnimeList.
6
 * API Documentation: https://github.com/rl404/MAL-Scraper
7
 *
8
 * @author Axel Oktavian Antonio
9
 *
10
 * @since 26-09-2018
11
 *
12
 * @version 1.4.1
13
 *
14
 * @license MIT https://opensource.org/licenses/MIT
15
 */
16
17
namespace MalScraper;
18
19
use Cache;
20
use MalScraper\Helper\Helper;
21
use MalScraper\Model\Additional\CharacterPeoplePictureModel as CharacterPeoplePicture;
22
use MalScraper\Model\Additional\CharacterStaffModel as CharacterStaff;
23
use MalScraper\Model\Additional\PictureModel as Picture;
24
use MalScraper\Model\Additional\StatModel as Stat;
25
use MalScraper\Model\General\CharacterModel as Character;
26
use MalScraper\Model\General\InfoModel as Info;
27
use MalScraper\Model\General\PeopleModel as People;
28
use MalScraper\Model\General\ProducerModel as Producer;
29
use MalScraper\Model\Lists\AllGenreModel as AllGenre;
30
use MalScraper\Model\Lists\AllProducerModel as AllProducer;
31
use MalScraper\Model\Search\SearchAnimeMangaModel as SearchAnimeManga;
32
use MalScraper\Model\Search\SearchCharacterPeopleModel as SearchCharacterPeople;
33
use MalScraper\Model\Search\SearchUserModel as SearchUser;
34
use MalScraper\Model\Seasonal\SeasonModel as Season;
35
use MalScraper\Model\Top\TopModel as Top;
36
use MalScraper\Model\Top\TopCharacterModel as TopCharacter;
37
use MalScraper\Model\Top\TopPeopleModel as TopPeople;
38
use MalScraper\Model\User\FriendModel as Friend;
39
use MalScraper\Model\User\HistoryModel as History;
40
use MalScraper\Model\User\UserCoverModel as UserCover;
41
use MalScraper\Model\User\UserListModel as UserList;
42
use MalScraper\Model\User\UserModel as User;
43
44
/**
45
 * Class MalScraper.
46
 */
47
class MalScraper
48
{
49
    /**
50
     * Cache class.
51
     *
52
     * @var Cache
53
     */
54
    private $_cache;
55
56
    /**
57
     * Cache feature.
58
     *
59
     * @var bool
60
     */
61
    private $_enable_cache = false;
62
63
    /**
64
     * Cache expiration time.
65
     *
66
     * @var int
67
     */
68
    private $_cache_time = 86400;
69
70
    /**
71
     * Convert to http response.
72
     *
73
     * @var bool
74
     */
75
    private $_to_api = false;
76
77
    /**
78
     * Default constructor.
79
     *
80
     * @param array [optional] $config
81
     *
82
     * @return void
83
     */
84
    public function __construct($config = false)
85
    {
86
        if (!empty($config['enable_cache']) && $config['enable_cache'] === true) {
87
            // enable cache function
88
            $this->_enable_cache = $config['enable_cache'];
89
90
            // create cache class
91
            $this->_cache = new Cache();
92
            $this->_cache->setCachePath(dirname(__FILE__).'/Cache/');
93
94
            // set cache time
95
            if (!empty($config['cache_time'])) {
96
                $this->_cache_time = $config['cache_time'];
97
            }
98
        }
99
100
        // to http response function
101
        if (!empty($config['to_api']) && $config['to_api'] === true) {
102
            $this->_to_api = $config['to_api'];
103
        }
104
    }
105
106
    /**
107
     * Default call.
108
     *
109
     * @param string $method
110
     * @param array  $arguments
111
     *
112
     * @return string|array
113
     */
114
    public function __call($method, $arguments)
115
    {
116
        $result = '';
117
118
        // if cache function enabled
119
        if ($this->_enable_cache === true) {
120
            $this->_cache->setCache(str_replace('get', '', $method));
121
            $this->_cache->eraseExpired($this->_cache_time);
122
123
            $cacheName = $method.'('.implode(',', $arguments).')';
124
            $isCached = $this->_cache->isCached($cacheName);
125
126
            // if cached
127
            if ($isCached) {
128
                $result = $this->_cache->retrieve($cacheName);
129
            } else {
130
                $data = call_user_func_array([$this, $method], $arguments);
131
                $this->_cache->store($cacheName, $data, $this->_cache_time);
132
                $result = $data;
133
            }
134
        } else {
135
            $result = call_user_func_array([$this, $method], $arguments);
136
        }
137
138
        // if to api function enabled
139
        if ($this->_to_api === true) {
140
            return Helper::response($result);
141
        }
142
143
        return Helper::toResponse($result);
144
    }
145
146
    /**
147
     * Get anime/manga information.
148
     *
149
     * @param string $type anime or manga
150
     * @param int    $id   id of the anime or manga
151
     *
152
     * @return array
153
     */
154
    private function getInfo($type, $id)
155
    {
156
        return (new Info($type, $id))->getAllInfo();
157
    }
158
159
    /**
160
     * Get character information.
161
     *
162
     * @param int $id id of the character
163
     *
164
     * @return array
165
     */
166
    private function getCharacter($id)
167
    {
168
        return (new Character($id))->getAllInfo();
169
    }
170
171
    /**
172
     * Get people information.
173
     *
174
     * @param int $id id of the people
175
     *
176
     * @return array
177
     */
178
    private function getPeople($id)
179
    {
180
        return (new People($id))->getAllInfo();
181
    }
182
183
    /**
184
     * Get anime/manga character + staff complete list.
185
     *
186
     * @param string $type Either anime or manga
187
     * @param int    $id   id of the anime or manga
188
     *
189
     * @return array
190
     */
191
    private function getCharacterStaff($type, $id)
192
    {
193
        return (new CharacterStaff($type, $id))->getAllInfo();
194
    }
195
196
    /**
197
     * Get anime/manga detail stat.
198
     *
199
     * @param string $type Either anime or manga
200
     * @param int    $id   id of the anime or manga
201
     *
202
     * @return array
203
     */
204
    private function getStat($type, $id)
205
    {
206
        return (new Stat($type, $id))->getAllInfo();
207
    }
208
209
    /**
210
     * Get anime/manga additional pictures.
211
     *
212
     * @param string $type Either anime or manga
213
     * @param int    $id   id of the anime or manga
214
     *
215
     * @return array
216
     */
217
    private function getPicture($type, $id)
218
    {
219
        return (new Picture($type, $id))->getAllInfo();
220
    }
221
222
    /**
223
     * Get character additional pictures.
224
     *
225
     * @param int $id id of the character
226
     *
227
     * @return array
228
     */
229
    private function getCharacterPicture($id)
230
    {
231
        return (new CharacterPeoplePicture('character', $id))->getAllInfo();
232
    }
233
234
    /**
235
     * Get people additional pictures.
236
     *
237
     * @param int $id id of the people
238
     *
239
     * @return array
240
     */
241
    private function getPeoplePicture($id)
242
    {
243
        return (new CharacterPeoplePicture('people', $id))->getAllInfo();
244
    }
245
246
    /**
247
     * Get all anime produced by the studio/producer.
248
     *
249
     * @param int $id   id of the studio/producer
250
     * @param int $page (Optional) Page number
251
     *
252
     * @return array
253
     */
254
    private function getStudioProducer($id, $page = 1)
255
    {
256
        return (new Producer('anime', 'producer', $id, $page))->getAllInfo();
257
    }
258
259
    /**
260
     * Get all manga serialized by the magazine.
261
     *
262
     * @param int $id   id of the magazine
263
     * @param int $page (Optional) Page number
264
     *
265
     * @return array
266
     */
267
    private function getMagazine($id, $page = 1)
268
    {
269
        return (new Producer('manga', 'producer', $id, $page))->getAllInfo();
270
    }
271
272
    /**
273
     * Get all anime or manga that has the genre.
274
     *
275
     * @param string $type Either anime or manga
276
     * @param int    $id   id of the genre
277
     * @param int    $page (Optional) Page number
278
     *
279
     * @return array
280
     */
281
    private function getGenre($type, $id, $page = 1)
282
    {
283
        return (new Producer($type, 'genre', $id, $page))->getAllInfo();
284
    }
285
286
    /**
287
     * Get list of all anime genre.
288
     *
289
     * @return array
290
     */
291
    private function getAllAnimeGenre()
292
    {
293
        return (new AllGenre('anime'))->getAllInfo();
294
    }
295
296
    /**
297
     * Get list of all manga genre.
298
     *
299
     * @return array
300
     */
301
    private function getAllMangaGenre()
302
    {
303
        return (new AllGenre('manga'))->getAllInfo();
304
    }
305
306
    /**
307
     * Get list of all anime studio/producer.
308
     *
309
     * @return array
310
     */
311
    private function getAllStudioProducer()
312
    {
313
        return (new AllProducer('anime'))->getAllInfo();
314
    }
315
316
    /**
317
     * Get list of all manga magazine.
318
     *
319
     * @return array
320
     */
321
    private function getAllMagazine()
322
    {
323
        return (new AllProducer('manga'))->getAllInfo();
324
    }
325
326
    /**
327
     * Get anime search result.
328
     *
329
     * @param string $query Search query
330
     * @param int    $page  (Optional) Page number
331
     *
332
     * @return array
333
     */
334
    private function searchAnime($query, $page = 1)
335
    {
336
        return (new SearchAnimeManga('anime', $query, $page))->getAllInfo();
337
    }
338
339
    /**
340
     * Get manga search result.
341
     *
342
     * @param string $query Search query
343
     * @param int    $page  (Optional) Page number
344
     *
345
     * @return array
346
     */
347
    private function searchManga($query, $page = 1)
348
    {
349
        return (new SearchAnimeManga('manga', $query, $page))->getAllInfo();
350
    }
351
352
    /**
353
     * Get character search result.
354
     *
355
     * @param string $query Search query
356
     * @param int    $page  (Optional) Page number
357
     *
358
     * @return array
359
     */
360
    private function searchCharacter($query, $page = 1)
361
    {
362
        return (new SearchCharacterPeople('character', $query, $page))->getAllInfo();
363
    }
364
365
    /**
366
     * Get people search result.
367
     *
368
     * @param string $query Search query
369
     * @param int    $page  (Optional) Page number
370
     *
371
     * @return array
372
     */
373
    private function searchPeople($query, $page = 1)
374
    {
375
        return (new SearchCharacterPeople('people', $query, $page))->getAllInfo();
376
    }
377
378
    /**
379
     * Get user search result.
380
     *
381
     * @param string $query Search query
382
     * @param int    $page  (Optional) Page number
383
     *
384
     * @return array
385
     */
386
    private function searchUser($query, $page = 1)
387
    {
388
        return (new SearchUser($query, $page))->getAllInfo();
389
    }
390
391
    /**
392
     * Get seasonal anime.
393
     *
394
     * @param string|int|bool $year   (Optional) Season year
395
     * @param string|bool     $season (Optional) Season (summer,spring,fall,winter)
396
     *
397
     * @return array
398
     */
399
    private function getSeason($year = false, $season = false)
400
    {
401
        return (new Season($year, $season))->getAllInfo();
402
    }
403
404
    /**
405
     * Get top anime.
406
     *
407
     * @param string $type (Optional) Type of anime
408
     * @param int    $page (Optional) Page number
409
     *
410
     * @return array
411
     */
412
    private function getTopAnime($type = 0, $page = 1)
413
    {
414
        return (new Top('anime', $type, $page))->getAllInfo();
415
    }
416
417
    /**
418
     * Get top manga.
419
     *
420
     * @param string $type (Optional) Type of manga
421
     * @param int    $page (Optional) Page number
422
     *
423
     * @return array
424
     */
425
    private function getTopManga($type = 0, $page = 1)
426
    {
427
        return (new Top('manga', $type, $page))->getAllInfo();
428
    }
429
430
    /**
431
     * Get top character.
432
     *
433
     * @param int    $page (Optional) Page number
434
     *
435
     * @return array
436
     */
437
    private function getTopCharacter($page = 1)
438
    {
439
        return (new TopCharacter($page))->getAllInfo();
440
    }
441
442
    /**
443
     * Get top people.
444
     *
445
     * @param int    $page (Optional) Page number
446
     *
447
     * @return array
448
     */
449
    private function getTopPeople($page = 1)
450
    {
451
        return (new TopPeople($page))->getAllInfo();
452
    }
453
454
    /**
455
     * Get user info.
456
     *
457
     * @param string $user Username
458
     *
459
     * @return array
460
     */
461
    private function getUser($user)
462
    {
463
        return (new User($user))->getAllInfo();
464
    }
465
466
    /**
467
     * Get user friend list.
468
     *
469
     * @param string $user Username
470
     *
471
     * @return array
472
     */
473
    private function getUserFriend($user)
474
    {
475
        return (new Friend($user))->getAllInfo();
476
    }
477
478
    /**
479
     * Get user history.
480
     *
481
     * @param string $user Username
482
     * @param string $type (Optional) Either anime or manga
483
     *
484
     * @return array
485
     */
486
    private function getUserHistory($user, $type = false)
487
    {
488
        return (new History($user, $type))->getAllInfo();
0 ignored issues
show
Bug introduced by
It seems like $type can also be of type false; however, parameter $type of MalScraper\Model\User\HistoryModel::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

488
        return (new History($user, /** @scrutinizer ignore-type */ $type))->getAllInfo();
Loading history...
489
    }
490
491
    /**
492
     * Get user list.
493
     *
494
     * @param string $user   Username
495
     * @param string $type   (Optional) Either anime or manga
496
     * @param string $status (Optional) Anime/manga status
497
     *
498
     * @return array
499
     */
500
    private function getUserList($user, $type = 'anime', $status = 7)
501
    {
502
        return (new UserList($user, $type, $status))->getAllInfo();
503
    }
504
505
    /**
506
     * Get user cover.
507
     *
508
     * @param string      $user  Username
509
     * @param string      $type  (Optional) Either anime or manga
510
     * @param string|bool $style (Optional) CSS style for the cover
511
     *
512
     * @return string
513
     */
514
    private function getUserCover($user, $type = 'anime', $style = false)
515
    {
516
        return (new UserCover($user, $type, $style))->getAllInfo();
517
    }
518
}
519