Passed
Push — feature/super-model ( a850c4...c51526 )
by axel
03:11
created

MalScraper2   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 301
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 301
rs 10
c 0
b 0
f 0
wmc 27

19 Methods

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

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