1 | <?php declare(strict_types = 1); |
||
2 | |||
3 | /** |
||
4 | * This file is part of the Tmdb package. |
||
5 | * |
||
6 | * (c) Vincent Faliès <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | * |
||
11 | * @author Vincent Faliès <[email protected]> |
||
12 | * @copyright Copyright (c) 2017 |
||
13 | */ |
||
14 | |||
15 | namespace VfacTmdb; |
||
16 | |||
17 | use VfacTmdb\Interfaces\TmdbInterface; |
||
18 | use VfacTmdb\Interfaces\HttpRequestInterface; |
||
19 | use Psr\Log\LoggerInterface; |
||
20 | use VfacTmdb\Exceptions\TmdbException; |
||
21 | use VfacTmdb\Exceptions\IncorrectParamException; |
||
22 | use VfacTmdb\Exceptions\ServerErrorException; |
||
23 | |||
24 | /** |
||
25 | * Tmdb wrapper core class |
||
26 | * @package Tmdb |
||
27 | * @author Vincent Faliès <[email protected]> |
||
28 | * @copyright Copyright (c) 2017 |
||
29 | */ |
||
30 | class Tmdb implements TmdbInterface |
||
31 | { |
||
32 | |||
33 | /** |
||
34 | * API Key |
||
35 | * @var string |
||
36 | */ |
||
37 | private $api_key = null; |
||
38 | |||
39 | /** |
||
40 | * API configuration |
||
41 | * @var \stdClass |
||
42 | */ |
||
43 | protected $configuration = null; |
||
44 | |||
45 | /** |
||
46 | * API Genres |
||
47 | * @var \stdClass |
||
48 | */ |
||
49 | protected $genres = null; |
||
50 | |||
51 | /** |
||
52 | * Base URL of the API |
||
53 | * @var string |
||
54 | */ |
||
55 | public $base_api_url = 'https://api.themoviedb.org/'; |
||
56 | |||
57 | /** |
||
58 | * Logger |
||
59 | * @var LoggerInterface |
||
60 | */ |
||
61 | protected $logger = null; |
||
62 | |||
63 | /** |
||
64 | * API Version |
||
65 | * @var int |
||
66 | */ |
||
67 | protected $version = 3; |
||
68 | |||
69 | /** |
||
70 | * Http request object |
||
71 | * @var HttpRequestInterface |
||
72 | */ |
||
73 | protected $http_request = null; |
||
74 | /** |
||
75 | * Request object |
||
76 | * @var \stdClass |
||
77 | */ |
||
78 | protected $request; |
||
79 | /** |
||
80 | * Last request url |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $url = null; |
||
84 | |||
85 | /** |
||
86 | * Constructor |
||
87 | * @param string $api_key TMDB API Key |
||
88 | * @param int $version Version of API (Not yet used) |
||
89 | * @param LoggerInterface $logger Logger used in the class |
||
90 | * @param HttpRequestInterface $http_request |
||
91 | */ |
||
92 | 1071 | public function __construct(string $api_key, int $version = 3, LoggerInterface $logger, HttpRequestInterface $http_request) |
|
93 | { |
||
94 | 1071 | $this->api_key = $api_key; |
|
95 | 1071 | $this->logger = $logger; |
|
96 | 1071 | $this->version = $version; |
|
97 | 1071 | $this->http_request = $http_request; |
|
98 | 1071 | $this->request = new \stdClass; |
|
99 | 1071 | } |
|
100 | |||
101 | /** |
||
102 | * Send request to TMDB API with GET method |
||
103 | * @param string $action API action to request |
||
104 | * @param array $options Array of options of the request (optional) |
||
105 | * @return \stdClass|null |
||
106 | */ |
||
107 | 954 | public function getRequest(string $action, array $options = array()) : ?\stdClass |
|
108 | { |
||
109 | 954 | $this->logger->debug('Start sending HTTP request with GET method', array('action' => $action, 'options' => $options)); |
|
110 | 954 | $this->url = $this->buildHTTPUrl($action, $options); |
|
111 | 954 | return $this->sendRequest('GET', $this->url); |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * Send request to TMDB API with POST method |
||
116 | * @param string $action API action to request |
||
117 | * @param array $options Array of options of the request (optional) |
||
118 | * @param array $form_params form_params for request options |
||
119 | * @return \stdClass|null |
||
120 | */ |
||
121 | 45 | public function postRequest(string $action, array $options = array(), array $form_params = array()) : ?\stdClass |
|
122 | { |
||
123 | 45 | $this->logger->debug('Start sending HTTP request with POST method', array('action' => $action, 'options' => $options, 'form_params' => $form_params)); |
|
124 | 45 | $this->url = $this->buildHTTPUrl($action, $options); |
|
125 | 45 | return $this->sendRequest('POST', $this->url, $form_params); |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * Send request to TMDB API with DELETE method |
||
130 | * @param string $action API action to request |
||
131 | * @param array $options Array of options of the request (optional) |
||
132 | * @return \stdClass|null |
||
133 | */ |
||
134 | 15 | public function deleteRequest(string $action, array $options = array()) : ?\stdClass |
|
135 | { |
||
136 | 15 | $this->logger->debug('Start sending HTTP request with DELETE method', array('action' => $action, 'options' => $options)); |
|
137 | 15 | $this->url = $this->buildHTTPUrl($action, $options); |
|
138 | 15 | return $this->sendRequest('DELETE', $this->url); |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * Send request to TMDB API with GET method |
||
143 | * @param string $method HTTP method (GET, POST) |
||
144 | * @param string $url API url to request |
||
145 | * @param array $form_params form params request options |
||
146 | * @return \stdClass|null |
||
147 | */ |
||
148 | 18 | protected function sendRequest(string $method, string $url, array $form_params = array()) : ?\stdClass |
|
149 | { |
||
150 | try { |
||
151 | 18 | $res = new \stdClass(); |
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
152 | 18 | $method_name = strtolower($method).'Response'; |
|
153 | 18 | $res = $this->http_request->$method_name($url, [], $form_params); |
|
154 | 18 | $response = $this->decodeRequest($res, $method, $url, $form_params); |
|
155 | 9 | return $response; |
|
156 | 9 | } catch (TmdbException $e) { |
|
157 | 9 | $this->logger->error('sendRequest failed : '.$e->getMessage(), array('method' => $method, 'url' => $url, 'form_params' => $form_params)); |
|
158 | 9 | throw $e; |
|
159 | } |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Decode request response |
||
164 | * @param mixed $res |
||
165 | * @param string $method |
||
166 | * @param string $url |
||
167 | * @param array $form_params |
||
168 | * @return \stdClass |
||
169 | */ |
||
170 | 18 | private function decodeRequest($res, $method, $url, $form_params) : \stdClass |
|
171 | { |
||
172 | 18 | $content = $res->getBody(); |
|
173 | 18 | if (is_object($content)) { |
|
174 | $content = $content->getContents(); |
||
175 | } |
||
176 | |||
177 | 18 | if (empty($content)) { |
|
178 | 6 | $this->logger->error('Request Body empty', array('method' => $method, 'url' => $url, 'form_params' => $form_params)); |
|
179 | 6 | throw new ServerErrorException(); |
|
180 | } |
||
181 | 12 | $response = json_decode($content); |
|
182 | 12 | if (empty($response)) { |
|
183 | 3 | $this->logger->error('Request Body can not be decode', array('method' => $method, 'url' => $url, 'form_params' => $form_params)); |
|
184 | 3 | throw new ServerErrorException(); |
|
185 | } |
||
186 | 9 | return $response; |
|
187 | } |
||
188 | |||
189 | /** |
||
190 | * Build URL for HTTP Call |
||
191 | * @param string $action API action to request |
||
192 | * @param array $options Array of options of the request (optional) |
||
193 | * @return string |
||
194 | */ |
||
195 | 960 | private function buildHTTPUrl(string $action, array $options) : string |
|
196 | { |
||
197 | // Url construction |
||
198 | 960 | $url = $this->base_api_url . $this->version . '/' . $action; |
|
199 | |||
200 | // Parameters |
||
201 | 960 | $params = []; |
|
202 | 960 | $params['api_key'] = $this->api_key; |
|
203 | |||
204 | 960 | $params = array_merge($params, $options); |
|
205 | |||
206 | // URL with paramters construction |
||
207 | 960 | $url = $url . '?' . http_build_query($params); |
|
208 | |||
209 | 960 | return $url; |
|
210 | } |
||
211 | |||
212 | /** |
||
213 | * Get API Configuration |
||
214 | * @return \stdClass |
||
215 | * @throws TmdbException |
||
216 | */ |
||
217 | 150 | public function getConfiguration() : \stdClass |
|
218 | { |
||
219 | try { |
||
220 | 150 | $this->logger->debug('Start getting configuration'); |
|
221 | 150 | if (is_null($this->configuration)) { |
|
222 | 150 | $this->logger->debug('No configuration found, sending HTTP request to get it'); |
|
223 | 150 | $this->configuration = $this->getRequest('configuration'); |
|
224 | } |
||
225 | 147 | return $this->configuration; |
|
226 | 3 | } catch (TmdbException $ex) { |
|
227 | 3 | throw $ex; |
|
228 | } |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Get logger |
||
233 | * @return LoggerInterface |
||
234 | */ |
||
235 | 975 | public function getLogger() : LoggerInterface |
|
236 | { |
||
237 | 975 | return $this->logger; |
|
238 | } |
||
239 | |||
240 | /** |
||
241 | * Magical property getter |
||
242 | * @param string $name Name of the property |
||
243 | * @return string Value of the property |
||
244 | */ |
||
245 | 219 | public function __get(string $name) : string |
|
246 | { |
||
247 | 146 | switch ($name) { |
|
248 | 219 | case 'url': |
|
249 | 216 | return $this->$name; |
|
250 | default: |
||
251 | 3 | throw new IncorrectParamException; |
|
252 | } |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Check year option and return correct value |
||
257 | * @param array $options |
||
258 | * @param array &$return Return array to save valid option |
||
259 | * @return void |
||
260 | */ |
||
261 | 87 | public function checkOptionYear(array $options, array &$return) : void |
|
262 | { |
||
263 | 87 | if (isset($options['year'])) { |
|
264 | 3 | $return['year'] = (int) $options['year']; |
|
265 | } |
||
266 | 87 | } |
|
267 | |||
268 | /** |
||
269 | * Check Language string with format ISO 639-1 |
||
270 | * @param array $options |
||
271 | * @param array &$return Return array to save valid option |
||
272 | * @return void |
||
273 | */ |
||
274 | 834 | public function checkOptionLanguage(array $options, array &$return) : void |
|
275 | { |
||
276 | 834 | if (isset($options['language'])) { |
|
277 | 105 | $check = preg_match("#([a-z]{2})-([A-Z]{2})#", $options['language']); |
|
278 | 105 | if ($check === 0 || $check === false) { |
|
279 | 3 | $this->logger->error('Incorrect language param option', array('language' => $options['language'])); |
|
280 | 3 | throw new IncorrectParamException; |
|
281 | } |
||
282 | 102 | $return['language'] = $options['language']; |
|
283 | } |
||
284 | 831 | } |
|
285 | |||
286 | /** |
||
287 | * Check include adult option |
||
288 | * @param array $options |
||
289 | * @param array &$return Return array to save valid option |
||
290 | * @return void |
||
291 | */ |
||
292 | 87 | public function checkOptionIncludeAdult(array $options, array &$return) : void |
|
293 | { |
||
294 | 87 | if (isset($options['include_adult'])) { |
|
295 | 3 | $return['include_adult'] = filter_var($options['include_adult'], FILTER_VALIDATE_BOOLEAN); |
|
296 | } |
||
297 | 87 | } |
|
298 | |||
299 | /** |
||
300 | * Check page option |
||
301 | * @param array $options |
||
302 | * @param array &$return Return array to save valid option |
||
303 | * @return void |
||
304 | */ |
||
305 | 114 | public function checkOptionPage(array $options, array &$return) : void |
|
306 | { |
||
307 | 114 | if (isset($options['page'])) { |
|
308 | 3 | $return['page'] = (int) $options['page']; |
|
309 | } |
||
310 | 114 | } |
|
311 | |||
312 | /** |
||
313 | * Check sort by option |
||
314 | * @param array $options |
||
315 | * @param array &$return Return array to save valid option |
||
316 | * @return void |
||
317 | */ |
||
318 | 27 | public function checkOptionSortBy(array $options, array &$return) : void |
|
319 | { |
||
320 | 27 | if (isset($options['sort_by'])) { |
|
321 | 6 | switch ($options['sort_by']) { |
|
322 | 6 | case 'asc': |
|
323 | 3 | case 'desc': |
|
324 | 3 | break; |
|
325 | default: |
||
326 | 3 | throw new IncorrectParamException; |
|
327 | } |
||
328 | 3 | $return['sort_by'] = 'created_at.'.$options['sort_by']; |
|
329 | } |
||
330 | 24 | } |
|
331 | |||
332 | /** |
||
333 | * Check query option |
||
334 | * @param array $options |
||
335 | * @param array &$return Return array to save valid option |
||
336 | * @return void |
||
337 | */ |
||
338 | 84 | public function checkOptionQuery(array $options, array &$return) : void |
|
339 | { |
||
340 | 84 | if (isset($options['query'])) { |
|
341 | 84 | $return['query'] = trim($options['query']); |
|
342 | } |
||
343 | 84 | } |
|
344 | |||
345 | /** |
||
346 | * Check session_id option |
||
347 | * @param array $options |
||
348 | * @param array &$return Return array to save valid option |
||
349 | * @return void |
||
350 | */ |
||
351 | 21 | public function checkOptionSessionId(array $options, array &$return) : void |
|
352 | { |
||
353 | 21 | if (isset($options['session_id'])) { |
|
354 | 21 | $return['session_id'] = trim($options['session_id']); |
|
355 | } |
||
356 | 21 | } |
|
357 | } |
||
358 |