Passed
Push — feature/super-model ( c51526...6844b5 )
by axel
03:18
created

MalScraper2::searchCharacter()   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
/**
40
 * Class MalScraper.
41
 */
42
class MalScraper2
43
{
44
	/**
45
     * Cache class.
46
     *
47
     * @var Cache
48
     */
49
    private $_cache;
50
51
    /**
52
     * Cache feature.
53
     *
54
     * @var bool
55
     */
56
    private $_enable_cache = false;
57
58
    /**
59
     * Cache expiration time.
60
     *
61
     * @var int
62
     */
63
    private $_cache_time = 86400;
64
65
    /**
66
     * Convert to http response.
67
     *
68
     * @var bool
69
     */
70
    private $_to_api = false;
71
72
    /**
73
     * Default constructor.
74
     *
75
     * @param array [optional] $config
76
     *
77
     * @return void
78
     */
79
    public function __construct($config = false)
80
    {
81
        if (!empty($config['enable_cache']) && $config['enable_cache'] === true) {
82
            // enable cache function
83
            $this->_enable_cache = $config['enable_cache'];
84
85
            // create cache class
86
            $this->_cache = new Cache();
87
            $this->_cache->setCachePath(dirname(__FILE__).'/Cache/');
88
89
            // set cache time
90
            if (!empty($config['cache_time'])) {
91
                $this->_cache_time = $config['cache_time'];
92
            }
93
        }
94
95
        // to http response function
96
        if (!empty($config['to_api']) && $config['to_api'] === true) {
97
            $this->_to_api = $config['to_api'];
98
        }
99
    }
100
101
    /**
102
     * Default call.
103
     *
104
     * @param string $method
105
     * @param array  $arguments
106
     *
107
     * @return string
108
     */
109
    public function __call($method, $arguments)
110
    {
111
        $result = '';
112
113
        // if cache function enabled
114
        if ($this->_enable_cache === true) {
115
        	$this->_cache->setCache(str_replace('get', '', $method));
116
        	$this->_cache->eraseExpired($this->_cache_time);
117
118
            $cacheName = $method.'('.implode(',', $arguments).')';
119
            $isCached = $this->_cache->isCached($cacheName);
120
121
            // if cached
122
            if ($isCached) {
123
                $result = $this->_cache->retrieve($cacheName);
124
            } else {
125
                $data = call_user_func_array([$this, $method], $arguments);
126
                $this->_cache->store($cacheName, $data, $this->_cache_time);
127
                $result = $data;
128
            }
129
        } else {
130
            $result = call_user_func_array([$this, $method], $arguments);
131
        }
132
133
        // if to api function enabled
134
        if ($this->_to_api === true)
135
            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

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