Passed
Push — feature/super-model ( 0f230d...88c1f6 )
by axel
02:19
created

MalScraper2   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 391
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 391
rs 9.68
c 0
b 0
f 0
wmc 34

26 Methods

Rating   Name   Duplication   Size   Complexity  
A getStat() 0 3 1
A getTopAnime() 0 3 1
A getCharacterPicture() 0 3 1
A getAllMagazine() 0 3 1
A searchManga() 0 3 1
A getPeople() 0 3 1
A searchPeople() 0 3 1
A searchAnime() 0 3 1
A getTopManga() 0 3 1
A getAllStudioProducer() 0 3 1
A getStudioProducer() 0 3 1
A getGenre() 0 3 1
A getUser() 0 3 1
A getMagazine() 0 3 1
A getInfo() 0 3 1
A getPeoplePicture() 0 3 1
A __call() 0 28 4
A getSeason() 0 3 1
A getAllAnimeGenre() 0 3 1
A getPicture() 0 3 1
A searchUser() 0 3 1
A __construct() 0 19 6
A getAllMangaGenre() 0 3 1
A getCharacter() 0 3 1
A getCharacterStaff() 0 3 1
A searchCharacter() 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
22
use MalScraper\Model\General\InfoModel as Info;
23
use MalScraper\Model\General\CharacterModel as Character;
24
use MalScraper\Model\General\PeopleModel as People;
25
use MalScraper\Model\General\ProducerModel as Producer;
26
27
use MalScraper\Model\Additional\CharacterStaffModel as CharacterStaff;
28
use MalScraper\Model\Additional\StatModel as Stat;
29
use MalScraper\Model\Additional\PictureModel as Picture;
30
use MalScraper\Model\Additional\CharacterPeoplePictureModel as CharacterPeoplePicture;
31
32
use MalScraper\Model\Lists\AllGenreModel as AllGenre;
33
use MalScraper\Model\Lists\AllProducerModel as AllProducer;
34
35
use MalScraper\Model\Search\SearchAnimeMangaModel as SearchAnimeManga;
36
use MalScraper\Model\Search\SearchCharacterPeopleModel as SearchCharacterPeople;
37
use MalScraper\Model\Search\SearchUserModel as SearchUser;
38
39
use MalScraper\Model\Seasonal\SeasonModel as Season;
40
use MalScraper\Model\Top\TopModel as Top;
41
42
use MalScraper\Model\User\UserModel as User;
43
44
/**
45
 * Class MalScraper.
46
 */
47
class MalScraper2
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
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);
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

140
            return Helper::/** @scrutinizer ignore-call */ response($result);
Loading history...
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 $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...
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...
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
}