Passed
Push — feature/super-model ( 6844b5...0f230d )
by axel
02:12
created

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

397
        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

397
        return (new Season(/** @scrutinizer ignore-type */ $year, $season))->getAllInfo();
Loading history...
398
    }
399
400
    /**
401
     * Get top anime.
402
     *
403
     * @param string   $type   (Optional) Type of anime
404
     * @param int        $page   (Optional) Page number
405
     *
406
     * @return array
407
     */
408
    private function getTopAnime($type = 0, $page = 1)
409
    {
410
        return (new Top('anime', $type, $page))->getAllInfo();
411
    }
412
413
    /**
414
     * Get top manga.
415
     *
416
     * @param string   $type   (Optional) Type of manga
417
     * @param int        $page   (Optional) Page number
418
     *
419
     * @return array
420
     */
421
    private function getTopManga($type = 0, $page = 1)
422
    {
423
        return (new Top('manga', $type, $page))->getAllInfo();
424
    }
425
}