Test Setup Failed
Push — feature/super-model ( 86f2ea...9eaa75 )
by axel
09:29 queued 07:56
created

MalScraper   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 446
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 446
rs 9.36
c 0
b 0
f 0
wmc 38

30 Methods

Rating   Name   Duplication   Size   Complexity  
A getSeason() 0 3 1
A getAllAnimeGenre() 0 3 1
A getUserList() 0 3 1
A getAllStudioProducer() 0 3 1
A getCharacterPicture() 0 3 1
A getInfo() 0 3 1
A getStat() 0 3 1
A getAllMangaGenre() 0 3 1
A getPeople() 0 3 1
A getTopManga() 0 3 1
A __call() 0 30 4
A searchManga() 0 3 1
A searchUser() 0 3 1
A searchPeople() 0 3 1
A getUserHistory() 0 3 1
A __construct() 0 19 6
A getUser() 0 3 1
A getGenre() 0 3 1
A getTopAnime() 0 3 1
A getPeoplePicture() 0 3 1
A getPicture() 0 3 1
A searchCharacter() 0 3 1
A getAllMagazine() 0 3 1
A getCharacter() 0 3 1
A searchAnime() 0 3 1
A getUserCover() 0 3 1
A getMagazine() 0 3 1
A getUserFriend() 0 3 1
A getCharacterStaff() 0 3 1
A getStudioProducer() 0 3 1
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.0
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\User\FriendModel as Friend;
37
use MalScraper\Model\User\HistoryModel as History;
38
use MalScraper\Model\User\UserCoverModel as UserCover;
39
use MalScraper\Model\User\UserListModel as UserList;
40
use MalScraper\Model\User\UserModel as User;
41
42
/**
43
 * Class MalScraper.
44
 */
45
class MalScraper
46
{
47
    /**
48
     * Cache class.
49
     *
50
     * @var Cache
51
     */
52
    private $_cache;
53
54
    /**
55
     * Cache feature.
56
     *
57
     * @var bool
58
     */
59
    private $_enable_cache = false;
60
61
    /**
62
     * Cache expiration time.
63
     *
64
     * @var int
65
     */
66
    private $_cache_time = 86400;
67
68
    /**
69
     * Convert to http response.
70
     *
71
     * @var bool
72
     */
73
    private $_to_api = false;
74
75
    /**
76
     * Default constructor.
77
     *
78
     * @param array [optional] $config
79
     *
80
     * @return void
81
     */
82
    public function __construct($config = false)
83
    {
84
        if (!empty($config['enable_cache']) && $config['enable_cache'] === true) {
85
            // enable cache function
86
            $this->_enable_cache = $config['enable_cache'];
87
88
            // create cache class
89
            $this->_cache = new Cache();
90
            $this->_cache->setCachePath(dirname(__FILE__).'/Cache/');
91
92
            // set cache time
93
            if (!empty($config['cache_time'])) {
94
                $this->_cache_time = $config['cache_time'];
95
            }
96
        }
97
98
        // to http response function
99
        if (!empty($config['to_api']) && $config['to_api'] === true) {
100
            $this->_to_api = $config['to_api'];
101
        }
102
    }
103
104
    /**
105
     * Default call.
106
     *
107
     * @param string $method
108
     * @param array  $arguments
109
     *
110
     * @return string
111
     */
112
    public function __call($method, $arguments)
113
    {
114
        $result = '';
115
116
        // if cache function enabled
117
        if ($this->_enable_cache === true) {
118
            $this->_cache->setCache(str_replace('get', '', $method));
119
            $this->_cache->eraseExpired($this->_cache_time);
120
121
            $cacheName = $method.'('.implode(',', $arguments).')';
122
            $isCached = $this->_cache->isCached($cacheName);
123
124
            // if cached
125
            if ($isCached) {
126
                $result = $this->_cache->retrieve($cacheName);
127
            } else {
128
                $data = call_user_func_array([$this, $method], $arguments);
129
                $this->_cache->store($cacheName, $data, $this->_cache_time);
130
                $result = $data;
131
            }
132
        } else {
133
            $result = call_user_func_array([$this, $method], $arguments);
134
        }
135
136
        // if to api function enabled
137
        if ($this->_to_api === true) {
138
            return Helper::response($result);
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Helper\Helper::response() is not static, but was called statically. ( Ignorable by Annotation )

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

138
            return Helper::/** @scrutinizer ignore-call */ response($result);
Loading history...
139
        }
140
141
        return Helper::toResponse($result);
0 ignored issues
show
Bug Best Practice introduced by
The expression return MalScraper\Helper...er::toResponse($result) also could return the type array which is incompatible with the documented return type string.
Loading history...
142
    }
143
144
    /**
145
     * Get anime/manga information.
146
     *
147
     * @param string $type anime or manga
148
     * @param int    $id   id of the anime or manga
149
     *
150
     * @return array
151
     */
152
    private function getInfo($type, $id)
153
    {
154
        return (new Info($type, $id))->getAllInfo();
155
    }
156
157
    /**
158
     * Get character information.
159
     *
160
     * @param int $id id of the character
161
     *
162
     * @return array
163
     */
164
    private function getCharacter($id)
165
    {
166
        return (new Character($id))->getAllInfo();
167
    }
168
169
    /**
170
     * Get people information.
171
     *
172
     * @param int $id id of the people
173
     *
174
     * @return array
175
     */
176
    private function getPeople($id)
177
    {
178
        return (new People($id))->getAllInfo();
179
    }
180
181
    /**
182
     * Get anime/manga character + staff complete list.
183
     *
184
     * @param string $type Either anime or manga
185
     * @param int    $id   id of the anime or manga
186
     *
187
     * @return array
188
     */
189
    private function getCharacterStaff($type, $id)
190
    {
191
        return (new CharacterStaff($type, $id))->getAllInfo();
192
    }
193
194
    /**
195
     * Get anime/manga detail stat.
196
     *
197
     * @param string $type Either anime or manga
198
     * @param int    $id   id of the anime or manga
199
     *
200
     * @return array
201
     */
202
    private function getStat($type, $id)
203
    {
204
        return (new Stat($type, $id))->getAllInfo();
205
    }
206
207
    /**
208
     * Get anime/manga additional pictures.
209
     *
210
     * @param string $type Either anime or manga
211
     * @param int    $id   id of the anime or manga
212
     *
213
     * @return array
214
     */
215
    private function getPicture($type, $id)
216
    {
217
        return (new Picture($type, $id))->getAllInfo();
218
    }
219
220
    /**
221
     * Get character additional pictures.
222
     *
223
     * @param int $id id of the character
224
     *
225
     * @return array
226
     */
227
    private function getCharacterPicture($id)
228
    {
229
        return (new CharacterPeoplePicture('character', $id))->getAllInfo();
230
    }
231
232
    /**
233
     * Get people additional pictures.
234
     *
235
     * @param int $id id of the people
236
     *
237
     * @return array
238
     */
239
    private function getPeoplePicture($id)
240
    {
241
        return (new CharacterPeoplePicture('people', $id))->getAllInfo();
242
    }
243
244
    /**
245
     * Get all anime produced by the studio/producer.
246
     *
247
     * @param int $id   id of the studio/producer
248
     * @param int $page (Optional) Page number
249
     *
250
     * @return array
251
     */
252
    private function getStudioProducer($id, $page = 1)
253
    {
254
        return (new Producer('anime', 'producer', $id, $page))->getAllInfo();
255
    }
256
257
    /**
258
     * Get all manga serialized by the magazine.
259
     *
260
     * @param int $id   id of the magazine
261
     * @param int $page (Optional) Page number
262
     *
263
     * @return array
264
     */
265
    private function getMagazine($id, $page = 1)
266
    {
267
        return (new Producer('manga', 'producer', $id, $page))->getAllInfo();
268
    }
269
270
    /**
271
     * Get all anime or manga that has the genre.
272
     *
273
     * @param string $type Either anime or manga
274
     * @param int    $id   id of the genre
275
     * @param int    $page (Optional) Page number
276
     *
277
     * @return array
278
     */
279
    private function getGenre($type, $id, $page = 1)
280
    {
281
        return (new Producer($type, 'genre', $id, $page))->getAllInfo();
282
    }
283
284
    /**
285
     * Get list of all anime genre.
286
     *
287
     * @return array
288
     */
289
    private function getAllAnimeGenre()
290
    {
291
        return (new AllGenre('anime'))->getAllInfo();
292
    }
293
294
    /**
295
     * Get list of all manga genre.
296
     *
297
     * @return array
298
     */
299
    private function getAllMangaGenre()
300
    {
301
        return (new AllGenre('manga'))->getAllInfo();
302
    }
303
304
    /**
305
     * Get list of all anime studio/producer.
306
     *
307
     * @return array
308
     */
309
    private function getAllStudioProducer()
310
    {
311
        return (new AllProducer('anime'))->getAllInfo();
312
    }
313
314
    /**
315
     * Get list of all manga magazine.
316
     *
317
     * @return array
318
     */
319
    private function getAllMagazine()
320
    {
321
        return (new AllProducer('manga'))->getAllInfo();
322
    }
323
324
    /**
325
     * Get anime search result.
326
     *
327
     * @param string $query Search query
328
     * @param int    $page  (Optional) Page number
329
     *
330
     * @return array
331
     */
332
    private function searchAnime($query, $page = 1)
333
    {
334
        return (new SearchAnimeManga('anime', $query, $page))->getAllInfo();
335
    }
336
337
    /**
338
     * Get manga search result.
339
     *
340
     * @param string $query Search query
341
     * @param int    $page  (Optional) Page number
342
     *
343
     * @return array
344
     */
345
    private function searchManga($query, $page = 1)
346
    {
347
        return (new SearchAnimeManga('manga', $query, $page))->getAllInfo();
348
    }
349
350
    /**
351
     * Get character search result.
352
     *
353
     * @param string $query Search query
354
     * @param int    $page  (Optional) Page number
355
     *
356
     * @return array
357
     */
358
    private function searchCharacter($query, $page = 1)
359
    {
360
        return (new SearchCharacterPeople('character', $query, $page))->getAllInfo();
361
    }
362
363
    /**
364
     * Get people search result.
365
     *
366
     * @param string $query Search query
367
     * @param int    $page  (Optional) Page number
368
     *
369
     * @return array
370
     */
371
    private function searchPeople($query, $page = 1)
372
    {
373
        return (new SearchCharacterPeople('people', $query, $page))->getAllInfo();
374
    }
375
376
    /**
377
     * Get user search result.
378
     *
379
     * @param string $query Search query
380
     * @param int    $page  (Optional) Page number
381
     *
382
     * @return array
383
     */
384
    private function searchUser($query, $page = 1)
385
    {
386
        return (new SearchUser($query, $page))->getAllInfo();
387
    }
388
389
    /**
390
     * Get seasonal anime.
391
     *
392
     * @param string|int $year   (Optional) Season year
393
     * @param string     $season (Optional) Season (summer,spring,fall,winter)
394
     *
395
     * @return array
396
     */
397
    private function getSeason($year = false, $season = false)
398
    {
399
        return (new Season($year, $season))->getAllInfo();
0 ignored issues
show
Bug introduced by
It seems like $year can also be of type false; however, parameter $year of MalScraper\Model\Seasona...sonModel::__construct() does only seem to accept integer|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

399
        return (new Season(/** @scrutinizer ignore-type */ $year, $season))->getAllInfo();
Loading history...
Bug introduced by
It seems like $season can also be of type false; however, parameter $season of MalScraper\Model\Seasona...sonModel::__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

399
        return (new Season($year, /** @scrutinizer ignore-type */ $season))->getAllInfo();
Loading history...
400
    }
401
402
    /**
403
     * Get top anime.
404
     *
405
     * @param string $type (Optional) Type of anime
406
     * @param int    $page (Optional) Page number
407
     *
408
     * @return array
409
     */
410
    private function getTopAnime($type = 0, $page = 1)
411
    {
412
        return (new Top('anime', $type, $page))->getAllInfo();
413
    }
414
415
    /**
416
     * Get top manga.
417
     *
418
     * @param string $type (Optional) Type of manga
419
     * @param int    $page (Optional) Page number
420
     *
421
     * @return array
422
     */
423
    private function getTopManga($type = 0, $page = 1)
424
    {
425
        return (new Top('manga', $type, $page))->getAllInfo();
426
    }
427
428
    /**
429
     * Get user info.
430
     *
431
     * @param string $user Username
432
     *
433
     * @return array
434
     */
435
    private function getUser($user)
436
    {
437
        return (new User($user))->getAllInfo();
438
    }
439
440
    /**
441
     * Get user friend list.
442
     *
443
     * @param string $user Username
444
     *
445
     * @return array
446
     */
447
    private function getUserFriend($user)
448
    {
449
        return (new Friend($user))->getAllInfo();
0 ignored issues
show
Bug Best Practice introduced by
The expression return new MalScraper\Mo...el($user)->getAllInfo() returns the type string which is incompatible with the documented return type array.
Loading history...
450
    }
451
452
    /**
453
     * Get user history.
454
     *
455
     * @param string $user Username
456
     * @param string $type (Optional) Either anime or manga
457
     *
458
     * @return array
459
     */
460
    private function getUserHistory($user, $type = false)
461
    {
462
        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

462
        return (new History($user, /** @scrutinizer ignore-type */ $type))->getAllInfo();
Loading history...
Bug Best Practice introduced by
The expression return new MalScraper\Mo...r, $type)->getAllInfo() returns the type string which is incompatible with the documented return type array.
Loading history...
463
    }
464
465
    /**
466
     * Get user list.
467
     *
468
     * @param string $user   Username
469
     * @param string $type   (Optional) Either anime or manga
470
     * @param string $status (Optional) Anime/manga status
471
     *
472
     * @return array
473
     */
474
    private function getUserList($user, $type = 'anime', $status = 7)
475
    {
476
        return (new UserList($user, $type, $status))->getAllInfo();
0 ignored issues
show
Bug Best Practice introduced by
The expression return new MalScraper\Mo... $status)->getAllInfo() returns the type string which is incompatible with the documented return type array.
Loading history...
477
    }
478
479
    /**
480
     * Get user cover.
481
     *
482
     * @param string $user  Username
483
     * @param string $type  (Optional) Either anime or manga
484
     * @param string $style (Optional) CSS style for the cover
485
     *
486
     * @return array
487
     */
488
    private function getUserCover($user, $type = 'anime', $style = false)
489
    {
490
        return (new UserCover($user, $type, $style))->getAllInfo();
0 ignored issues
show
Bug introduced by
It seems like $style can also be of type false; however, parameter $style of MalScraper\Model\User\Us...verModel::__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

490
        return (new UserCover($user, $type, /** @scrutinizer ignore-type */ $style))->getAllInfo();
Loading history...
Bug Best Practice introduced by
The expression return new MalScraper\Mo..., $style)->getAllInfo() returns the type string which is incompatible with the documented return type array.
Loading history...
491
    }
492
}
493