Passed
Push — master ( 88da23...15886b )
by axel
03:22 queued 44s
created

MalScraper::searchAnime()   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 0
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.3.1
13
 *
14
 * @license MIT https://opensource.org/licenses/MIT
15
 */
16
17
namespace MalScraper;
18
19
require 'scraper/mal_scraper.php';
20
require_once 'scraper/cache.php';
21
22
use scraper\Cache;
23
24
/**
25
 * Class MalScraper.
26
 */
27
class MalScraper
28
{
29
    /**
30
     * Cache class.
0 ignored issues
show
Bug introduced by
The type MalScraper\class was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
     *
32
     * @var class
33
     */
34
    private $_cache;
35
36
    /**
37
     * Cache feature.
38
     *
39
     * @var bool
40
     */
41
    private $_enable_cache = false;
42
43
    /**
44
     * Cache expiration time.
45
     *
46
     * @var int
47
     */
48
    private $_cache_time = 86400;
49
50
    /**
51
     * Convert to http response.
52
     *
53
     * @var bool
54
     */
55
    private $_to_api = false;
56
57
    /**
58
     * Default constructor.
59
     *
60
     * @param array [optional] $config
61
     *
62
     * @return void
63
     */
64
    public function __construct($config = false)
65
    {
66
        if (!empty($config['enable_cache'])) {
67
            // enable cache function
68
            $this->_enable_cache = $config['enable_cache'];
69
70
            // create cache class
71
            $this->_cache = new Cache();
0 ignored issues
show
Documentation Bug introduced by
It seems like new scraper\Cache() of type scraper\Cache is incompatible with the declared type MalScraper\class of property $_cache.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
72
            $this->_cache->setCachePath(dirname(__FILE__).'/cache/');
73
            $this->_cache->setCache('malScraper');
74
            $this->_cache->eraseExpired();
75
76
            // set cache time
77
            if (!empty($config['cache_time'])) {
78
                $this->_cache_time = $config['cache_time'];
79
                $this->_cache->eraseExpired($this->_cache_time);
80
            }
81
        }
82
83
        // to http response function
84
        if (!empty($config['to_api'])) {
85
            $this->_to_api = $config['to_api'];
86
        }
87
    }
88
89
    /**
90
     * Default call.
91
     *
92
     * @param string $method
93
     * @param array  $arguments
94
     *
95
     * @return json
0 ignored issues
show
Bug introduced by
The type MalScraper\json was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
96
     */
97
    public function __call($method, $arguments)
98
    {
99
        $result = '';
100
101
        // if cache function enabled
102
        if ($this->_enable_cache === true) {
103
            $cacheName = $method.'('.implode(',', $arguments).')';
104
            $isCached = $this->_cache->isCached($cacheName);
105
106
            // if cached
107
            if ($isCached) {
108
                $result = $this->_cache->retrieve($cacheName);
109
            } else {
110
                $data = call_user_func_array([$this, $method], $arguments);
111
                $this->_cache->store($cacheName, $data, $this->_cache_time);
112
                $result = $data;
113
            }
114
        } else {
115
            $result = call_user_func_array([$this, $method], $arguments);
116
        }
117
118
        // if to api function enabled
119
        if ($this->_to_api === true) {
120
            $result = self::response($result);
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\MalScraper::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

120
            /** @scrutinizer ignore-call */ 
121
            $result = self::response($result);
Loading history...
121
        } else {
122
            $result = self::toResponse($result);
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\MalScraper::toResponse() 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

122
            /** @scrutinizer ignore-call */ 
123
            $result = self::toResponse($result);
Loading history...
123
        }
124
125
        return $result;
126
    }
127
128
    /**
129
     * Convert return result into easy-to-read result.
130
     *
131
     * @param string/array $response
0 ignored issues
show
Documentation Bug introduced by
The doc comment string/array at position 0 could not be parsed: Unknown type name 'string/array' at position 0 in string/array.
Loading history...
132
     *
133
     * @return string/array
0 ignored issues
show
Documentation Bug introduced by
The doc comment string/array at position 0 could not be parsed: Unknown type name 'string/array' at position 0 in string/array.
Loading history...
134
     */
135
    private function toResponse($response)
136
    {
137
        switch ($response) {
138
            case 400:
139
                return 'Search query needs at least 3 letters';
140
            case 403:
141
                return 'Private user list';
142
            case 404:
143
                return 'Page not found';
144
            default:
145
                return $response;
146
        }
147
    }
148
149
    /**
150
     * Convert return result into http response.
151
     *
152
     * @param string/array $response
0 ignored issues
show
Documentation Bug introduced by
The doc comment string/array at position 0 could not be parsed: Unknown type name 'string/array' at position 0 in string/array.
Loading history...
153
     *
154
     * @return json
155
     */
156
    private function response($response)
157
    {
158
        if (is_numeric($response)) {
159
            header('HTTP/1.1 '.$response);
160
            $result['status'] = $response;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.
Loading history...
161
            $result['status_message'] = self::toResponse($response);
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\MalScraper::toResponse() 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

161
            /** @scrutinizer ignore-call */ 
162
            $result['status_message'] = self::toResponse($response);
Loading history...
162
            $result['data'] = [];
163
        } else {
164
            header('HTTP/1.1 '. 200);
165
            $result['status'] = 200;
166
            $result['status_message'] = 'Success';
167
            $result['data'] = self::superEncode($response);
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\MalScraper::superEncode() 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

167
            /** @scrutinizer ignore-call */ 
168
            $result['data'] = self::superEncode($response);
Loading history...
168
        }
169
170
        $json_response = json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
171
        $json_response = str_replace('\\\\', '', $json_response);
172
173
        return $json_response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $json_response returns the type string which is incompatible with the documented return type MalScraper\json.
Loading history...
174
    }
175
176
    /**
177
     * Convert characters to UTF-8.
178
     *
179
     * @param array $array
180
     *
181
     * @return array
182
     */
183
    private function superEncode($array)
184
    {
185
        if (is_array($array) && $array) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $array of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
186
            foreach ($array as $key => $value) {
187
                if (is_array($value)) {
188
                    $array[$key] = self::superEncode($value);
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\MalScraper::superEncode() 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

188
                    /** @scrutinizer ignore-call */ 
189
                    $array[$key] = self::superEncode($value);
Loading history...
189
                } else {
190
                    $array[$key] = mb_convert_encoding($value, 'UTF-8', 'UTF-8');
191
                }
192
            }
193
        }
194
195
        return $array;
196
    }
197
198
    /**
199
     * Get anime/manga information.
200
     *
201
     * @param string $type anime or manga
202
     * @param int    $id   id of the anime or manga
203
     *
204
     * @return json \scraper\getInfo
205
     */
206
    private function getInfo()
207
    {
208
        return call_user_func_array('\scraper\getInfo', func_get_args());
209
    }
210
211
    /**
212
     * Get character information.
213
     *
214
     * @param int $id id of character
215
     *
216
     * @return json \scraper\getCharacter
217
     */
218
    private function getCharacter()
219
    {
220
        return call_user_func_array('\scraper\getCharacter', func_get_args());
221
    }
222
223
    /**
224
     * Get people information.
225
     *
226
     * @param int $id id of people
227
     *
228
     * @return json \scraper\getPeople
229
     */
230
    private function getPeople()
231
    {
232
        return call_user_func_array('\scraper\getPeople', func_get_args());
233
    }
234
235
    /**
236
     * Get complete list of character and staff of anime or manga.
237
     *
238
     * @param string $type anime or manga
239
     * @param int    $id   id of the anime or manga
240
     *
241
     * @return json \scraper\getCharacterStaff
242
     */
243
    private function getCharacterStaff()
244
    {
245
        return call_user_func_array('\scraper\getCharacterStaff', func_get_args());
246
    }
247
248
    /**
249
     * Get detail stat of anime or manga.
250
     *
251
     * @param string $type anime or manga
252
     * @param int    $id   id of the anime or manga
253
     *
254
     * @return json \scraper\getStat
255
     */
256
    private function getStat()
257
    {
258
        return call_user_func_array('\scraper\getStat', func_get_args());
259
    }
260
261
    /**
262
     * Get addition picture of anime or manga.
263
     *
264
     * @param string $type anime or manga
265
     * @param int    $id   id of the anime or manga
266
     *
267
     * @return json \scraper\getPicture
268
     */
269
    private function getPicture()
270
    {
271
        return call_user_func_array('\scraper\getPicture', func_get_args());
272
    }
273
274
    /**
275
     * Get addition picture of character.
276
     *
277
     * @param int $id id of the character
278
     *
279
     * @return json \scraper\getCharacterPicture
280
     */
281
    private function getCharacterPicture()
282
    {
283
        return call_user_func_array('\scraper\getCharacterPicture', func_get_args());
284
    }
285
286
    /**
287
     * Get addition picture of people.
288
     *
289
     * @param int $id id of the people
290
     *
291
     * @return json \scraper\getPeoplePicture
292
     */
293
    private function getPeoplePicture()
294
    {
295
        return call_user_func_array('\scraper\getPeoplePicture', func_get_args());
296
    }
297
298
    /**
299
     * Get list of anime produced by selected studio/producer.
300
     *
301
     * @param int $id   id of studio/producer
302
     * @param int $page page of result list
303
     *
304
     * @return json \scraper\getStudioProducer
305
     */
306
    private function getStudioProducer()
307
    {
308
        return call_user_func_array('\scraper\getStudioProducer', func_get_args());
309
    }
310
311
    /**
312
     * Get list of manga produced by selected magazine.
313
     *
314
     * @param int $id   id of magazine
315
     * @param int $page page of result list
316
     *
317
     * @return json \scraper\getMagazine
318
     */
319
    private function getMagazine()
320
    {
321
        return call_user_func_array('\scraper\getMagazine', func_get_args());
322
    }
323
324
    /**
325
     * Get list of anime contain selected genre.
326
     *
327
     * @param string $type anime or manga
328
     * @param int    $id   id of genre
329
     * @param int    $page page of result list
330
     *
331
     * @return json \scraper\getGenre
332
     */
333
    private function getGenre()
334
    {
335
        return call_user_func_array('\scraper\getGenre', func_get_args());
336
    }
337
338
    /**
339
     * Get list of all anime genre.
340
     *
341
     * @return json \scraper\getAllAnimeGenre
342
     */
343
    private function getAllAnimeGenre()
344
    {
345
        return call_user_func_array('\scraper\getAllAnimeGenre', func_get_args());
346
    }
347
348
    /**
349
     * Get list of all manga genre.
350
     *
351
     * @return json \scraper\getAllMangaGenre
352
     */
353
    private function getAllMangaGenre()
354
    {
355
        return call_user_func_array('\scraper\getAllMangaGenre', func_get_args());
356
    }
357
358
    /**
359
     * Get list of all anime studio/producer.
360
     *
361
     * @return json \scraper\getAllStudioProducer
362
     */
363
    private function getAllStudioProducer()
364
    {
365
        return call_user_func_array('\scraper\getAllStudioProducer', func_get_args());
366
    }
367
368
    /**
369
     * Get list of all manga magazine.
370
     *
371
     * @return json \scraper\getAllMagazine
372
     */
373
    private function getAllMagazine()
374
    {
375
        return call_user_func_array('\scraper\getAllMagazine', func_get_args());
376
    }
377
378
    /**
379
     * Get list of result of anime search.
380
     *
381
     * @param string $q    search query
382
     * @param int    $page page of result list
383
     *
384
     * @return json \scraper\searchAnime
385
     */
386
    private function searchAnime()
387
    {
388
        return call_user_func_array('\scraper\searchAnime', func_get_args());
389
    }
390
391
    /**
392
     * Get list of result of manga search.
393
     *
394
     * @param string $q    search query
395
     * @param int    $page page of result list
396
     *
397
     * @return json \scraper\searchManga
398
     */
399
    private function searchManga()
400
    {
401
        return call_user_func_array('\scraper\searchManga', func_get_args());
402
    }
403
404
    /**
405
     * Get list of result of character search.
406
     *
407
     * @param string $q    search query
408
     * @param int    $page page of result list
409
     *
410
     * @return json \scraper\searchCharacter
411
     */
412
    private function searchCharacter()
413
    {
414
        return call_user_func_array('\scraper\searchCharacter', func_get_args());
415
    }
416
417
    /**
418
     * Get list of result of people search.
419
     *
420
     * @param string $q    search query
421
     * @param int    $page page of result list
422
     *
423
     * @return json \scraper\searchPeople
424
     */
425
    private function searchPeople()
426
    {
427
        return call_user_func_array('\scraper\searchPeople', func_get_args());
428
    }
429
430
    /**
431
     * Get list of result of user search.
432
     *
433
     * @param string $q    search query
434
     * @param int    $page page of result list
435
     *
436
     * @return json \scraper\searchUser
437
     */
438
    private function searchUser()
439
    {
440
        return call_user_func_array('\scraper\searchUser', func_get_args());
441
    }
442
443
    /**
444
     * Get list of anime of the season.
445
     *
446
     * @param string $year   year of the season (current year for default)
447
     * @param string $season summer, spring, fall, winter (current season for default)
448
     *
449
     * @return json \scraper\getSeason
450
     */
451
    private function getSeason()
452
    {
453
        return call_user_func_array('\scraper\getSeason', func_get_args());
454
    }
455
456
    /**
457
     * Get list of top anime.
458
     *
459
     * @param int $type type of top anime
460
     * @param int $page page of top list
461
     *
462
     * @return json \scraper\getTopAnime
463
     */
464
    private function getTopAnime()
465
    {
466
        return call_user_func_array('\scraper\getTopAnime', func_get_args());
467
    }
468
469
    /**
470
     * Get list of top manga.
471
     *
472
     * @param int $type type of top manga
473
     * @param int $page page of top list
474
     *
475
     * @return json \scraper\getTopManga
476
     */
477
    private function getTopManga()
478
    {
479
        return call_user_func_array('\scraper\getTopManga', func_get_args());
480
    }
481
482
    /**
483
     * Get user information.
484
     *
485
     * @param string $user username
486
     *
487
     * @return json \scraper\getUser
488
     */
489
    private function getUser()
490
    {
491
        return call_user_func_array('\scraper\getUser', func_get_args());
492
    }
493
494
    /**
495
     * Get user's friend list.
496
     *
497
     * @param string $user username
498
     *
499
     * @return json \scraper\getUserFriend
500
     */
501
    private function getUserFriend()
502
    {
503
        return call_user_func_array('\scraper\getUserFriend', func_get_args());
504
    }
505
506
    /**
507
     * Get user's history information.
508
     *
509
     * @param string $user username
510
     * @param string $type anime or manga (optional) (both for default)
511
     *
512
     * @return json \scraper\getUserHistory
513
     */
514
    private function getUserHistory()
515
    {
516
        return call_user_func_array('\scraper\getUserHistory', func_get_args());
517
    }
518
519
    /**
520
     * Get user's anime or manga list.
521
     *
522
     * @param string $user   username
523
     * @param string $type   anime or manga (optional) (anime for default)
524
     * @param int    $status watching,completed,on hold, etc (optional)
525
     *
526
     * @return json \scraper\getUserList
527
     */
528
    private function getUserList()
529
    {
530
        return call_user_func_array('\scraper\getUserList', func_get_args());
531
    }
532
533
    /**
534
     * Get user's anime or manga list cover.
535
     *
536
     * @param string $user  username
537
     * @param string $type  anime or manga (optional) (anime for default)
538
     * @param string $style css for each cover (optional)
539
     *
540
     * @return json \scraper\getUserList
541
     */
542
    private function getUserCover()
543
    {
544
        return call_user_func_array('\scraper\getUserCover', func_get_args());
545
    }
546
}
547