Passed
Branch feature/super-model (24c950)
by axel
02:55
created

MalScraper2::getStudioProducer()   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
use MalScraper\Model\InfoModel as Info;
22
use MalScraper\Model\CharacterModel as Character;
23
use MalScraper\Model\PeopleModel as People;
24
use MalScraper\Model\CharacterStaffModel as CharacterStaff;
25
use MalScraper\Model\StatModel as Stat;
26
use MalScraper\Model\PictureModel as Picture;
27
use MalScraper\Model\CharacterPictureModel as CharacterPicture;
28
use MalScraper\Model\PeoplePictureModel as PeoplePicture;
29
use MalScraper\Model\StudioProducerModel as StudioProducer;
30
31
/**
32
 * Class MalScraper.
33
 */
34
class MalScraper2
35
{
36
	/**
37
     * Cache class.
38
     *
39
     * @var Cache
40
     */
41
    private $_cache;
42
43
    /**
44
     * Cache feature.
45
     *
46
     * @var bool
47
     */
48
    private $_enable_cache = false;
49
50
    /**
51
     * Cache expiration time.
52
     *
53
     * @var int
54
     */
55
    private $_cache_time = 86400;
56
57
    /**
58
     * Convert to http response.
59
     *
60
     * @var bool
61
     */
62
    private $_to_api = false;
63
64
    /**
65
     * Default constructor.
66
     *
67
     * @param array [optional] $config
68
     *
69
     * @return void
70
     */
71
    public function __construct($config = false)
72
    {
73
        if (!empty($config['enable_cache']) && $config['enable_cache'] === true) {
74
            // enable cache function
75
            $this->_enable_cache = $config['enable_cache'];
76
77
            // create cache class
78
            $this->_cache = new Cache();
79
            $this->_cache->setCachePath(dirname(__FILE__).'/Cache/');
80
81
            // set cache time
82
            if (!empty($config['cache_time'])) {
83
                $this->_cache_time = $config['cache_time'];
84
            }
85
        }
86
87
        // to http response function
88
        if (!empty($config['to_api']) && $config['to_api'] === true) {
89
            $this->_to_api = $config['to_api'];
90
        }
91
    }
92
93
    /**
94
     * Default call.
95
     *
96
     * @param string $method
97
     * @param array  $arguments
98
     *
99
     * @return string
100
     */
101
    public function __call($method, $arguments)
102
    {
103
        $result = '';
104
105
        // if cache function enabled
106
        if ($this->_enable_cache === true) {
107
        	$this->_cache->setCache(str_replace('get', '', $method));
108
        	$this->_cache->eraseExpired($this->_cache_time);
109
110
            $cacheName = $method.'('.implode(',', $arguments).')';
111
            $isCached = $this->_cache->isCached($cacheName);
112
113
            // if cached
114
            if ($isCached) {
115
                $result = $this->_cache->retrieve($cacheName);
116
            } else {
117
                $data = call_user_func_array([$this, $method], $arguments);
118
                $this->_cache->store($cacheName, $data, $this->_cache_time);
119
                $result = $data;
120
            }
121
        } else {
122
            $result = call_user_func_array([$this, $method], $arguments);
123
        }
124
125
        // if to api function enabled
126
        if ($this->_to_api === true)
127
            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

127
            return Helper::/** @scrutinizer ignore-call */ response($result);
Loading history...
128
        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...
129
    }
130
131
    /**
132
     * Get anime/manga information.
133
     *
134
     * @param string $type anime or manga
135
     * @param int    $id   id of the anime or manga
136
     *
137
     * @return array
138
     */
139
	private function getInfo($type, $id)
140
	{
141
		return (new Info($type, $id))->getAllInfo();
142
	}
143
144
    /**
145
     * Get character information.
146
     *
147
     * @param int    $id   id of the character
148
     *
149
     * @return array
150
     */
151
    private function getCharacter($id)
152
    {
153
        return (new Character($id))->getAllInfo();
154
    }
155
156
    /**
157
     * Get people information.
158
     *
159
     * @param int    $id   id of the people
160
     *
161
     * @return array
162
     */
163
    private function getPeople($id)
164
    {
165
        return (new People($id))->getAllInfo();
166
    }
167
168
    /**
169
     * Get anime/manga character + staff complete list.
170
     *
171
     * @param string    $type   Either anime or manga
172
     * @param int    $id   id of the anime or manga
173
     *
174
     * @return array
175
     */
176
    private function getCharacterStaff($type, $id)
177
    {
178
        return (new CharacterStaff($type, $id))->getAllInfo();
179
    }
180
181
    /**
182
     * Get anime/manga detail stat.
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 getStat($type, $id)
190
    {
191
        return (new Stat($type, $id))->getAllInfo();
192
    }
193
194
    /**
195
     * Get anime/manga additional pictures.
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 getPicture($type, $id)
203
    {
204
        return (new Picture($type, $id))->getAllInfo();
205
    }
206
207
    /**
208
     * Get character additional pictures.
209
     *
210
     * @param int    $id   id of the character
211
     *
212
     * @return array
213
     */
214
    private function getCharacterPicture($id)
215
    {
216
        return (new CharacterPicture($id))->getAllInfo();
217
    }
218
219
    /**
220
     * Get people additional pictures.
221
     *
222
     * @param int    $id   id of the people
223
     *
224
     * @return array
225
     */
226
    private function getPeoplePicture($id)
227
    {
228
        return (new PeoplePicture($id))->getAllInfo();
229
    }
230
231
    /**
232
     * Get all anime produced by the studio/producer.
233
     *
234
     * @param int    $id   id of the studio/producer
235
     * @param int    $page   (Optional) Page number
236
     *
237
     * @return array
238
     */
239
    private function getStudioProducer($id, $page = 1)
240
    {
241
        return (new StudioProducer($id, $page))->getAllInfo();
242
    }
243
}