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
|
1095 |
|
public function __construct(string $api_key, int $version = 3, LoggerInterface $logger, HttpRequestInterface $http_request) |
93
|
|
|
{ |
94
|
1095 |
|
$this->api_key = $api_key; |
95
|
1095 |
|
$this->logger = $logger; |
96
|
1095 |
|
$this->version = $version; |
97
|
1095 |
|
$this->http_request = $http_request; |
98
|
1095 |
|
$this->request = new \stdClass; |
99
|
1095 |
|
} |
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
|
978 |
|
public function getRequest(string $action, array $options = array()) : ?\stdClass |
108
|
|
|
{ |
109
|
978 |
|
$this->logger->debug('Start sending HTTP request with GET method', array('action' => $action, 'options' => $options)); |
110
|
978 |
|
$this->url = $this->buildHTTPUrl($action, $options); |
111
|
978 |
|
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 |
|
$method_name = strtolower($method).'Response'; |
152
|
18 |
|
$res = $this->http_request->$method_name($url, [], $form_params); |
153
|
18 |
|
$response = $this->decodeRequest($res, $method, $url, $form_params); |
154
|
9 |
|
return $response; |
155
|
9 |
|
} catch (TmdbException $e) { |
156
|
9 |
|
$this->logger->error('sendRequest failed : '.$e->getMessage(), array('method' => $method, 'url' => $url, 'form_params' => $form_params)); |
157
|
9 |
|
throw $e; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Decode request response |
163
|
|
|
* @param mixed $res |
164
|
|
|
* @param string $method |
165
|
|
|
* @param string $url |
166
|
|
|
* @param array $form_params |
167
|
|
|
* @return \stdClass |
168
|
|
|
*/ |
169
|
18 |
|
private function decodeRequest($res, $method, $url, $form_params) : \stdClass |
170
|
|
|
{ |
171
|
18 |
|
$content = $res->getBody(); |
172
|
|
|
|
173
|
18 |
|
if (empty($content)) { |
174
|
6 |
|
$this->logger->error('Request Body empty', array('method' => $method, 'url' => $url, 'form_params' => $form_params)); |
175
|
6 |
|
throw new ServerErrorException(); |
176
|
|
|
} |
177
|
12 |
|
$response = json_decode($content); |
178
|
12 |
|
if (empty($response)) { |
179
|
3 |
|
$this->logger->error('Request Body can not be decode', array('method' => $method, 'url' => $url, 'form_params' => $form_params)); |
180
|
3 |
|
throw new ServerErrorException(); |
181
|
|
|
} |
182
|
9 |
|
return $response; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Build URL for HTTP Call |
187
|
|
|
* @param string $action API action to request |
188
|
|
|
* @param array $options Array of options of the request (optional) |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
984 |
|
private function buildHTTPUrl(string $action, array $options) : string |
192
|
|
|
{ |
193
|
|
|
// Url construction |
194
|
984 |
|
$url = $this->base_api_url . $this->version . '/' . $action; |
195
|
|
|
|
196
|
|
|
// Parameters |
197
|
984 |
|
$params = []; |
198
|
984 |
|
$params['api_key'] = $this->api_key; |
199
|
|
|
|
200
|
984 |
|
$params = array_merge($params, $options); |
201
|
|
|
|
202
|
|
|
// URL with paramters construction |
203
|
984 |
|
$url = $url . '?' . http_build_query($params); |
204
|
|
|
|
205
|
984 |
|
return $url; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get API Configuration |
210
|
|
|
* @return \stdClass |
211
|
|
|
* @throws TmdbException |
212
|
|
|
*/ |
213
|
150 |
|
public function getConfiguration() : \stdClass |
214
|
|
|
{ |
215
|
|
|
try { |
216
|
150 |
|
$this->logger->debug('Start getting configuration'); |
217
|
150 |
|
if (is_null($this->configuration)) { |
218
|
150 |
|
$this->logger->debug('No configuration found, sending HTTP request to get it'); |
219
|
150 |
|
$this->configuration = $this->getRequest('configuration'); |
220
|
|
|
} |
221
|
147 |
|
return $this->configuration; |
222
|
3 |
|
} catch (TmdbException $ex) { |
223
|
3 |
|
throw $ex; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Get logger |
229
|
|
|
* @return LoggerInterface |
230
|
|
|
*/ |
231
|
999 |
|
public function getLogger() : LoggerInterface |
232
|
|
|
{ |
233
|
999 |
|
return $this->logger; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Magical property getter |
238
|
|
|
* @param string $name Name of the property |
239
|
|
|
* @return string Value of the property |
240
|
|
|
*/ |
241
|
222 |
|
public function __get(string $name) : string |
242
|
|
|
{ |
243
|
148 |
|
switch ($name) { |
244
|
222 |
|
case 'url': |
245
|
219 |
|
return $this->$name; |
246
|
|
|
default: |
247
|
3 |
|
throw new IncorrectParamException; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Check year option and return correct value |
253
|
|
|
* @param array $options |
254
|
|
|
* @param array &$return Return array to save valid option |
255
|
|
|
* @return void |
256
|
|
|
*/ |
257
|
87 |
|
public function checkOptionYear(array $options, array &$return) : void |
258
|
|
|
{ |
259
|
87 |
|
if (isset($options['year'])) { |
260
|
3 |
|
$return['year'] = (int) $options['year']; |
261
|
|
|
} |
262
|
87 |
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Check Language string with format ISO 639-1 |
266
|
|
|
* @param array $options |
267
|
|
|
* @param array &$return Return array to save valid option |
268
|
|
|
* @return void |
269
|
|
|
*/ |
270
|
858 |
|
public function checkOptionLanguage(array $options, array &$return) : void |
271
|
|
|
{ |
272
|
858 |
|
if (isset($options['language'])) { |
273
|
105 |
|
$check = preg_match("#([a-z]{2})-([A-Z]{2})#", $options['language']); |
274
|
105 |
|
if ($check === 0 || $check === false) { |
275
|
3 |
|
$this->logger->error('Incorrect language param option', array('language' => $options['language'])); |
276
|
3 |
|
throw new IncorrectParamException; |
277
|
|
|
} |
278
|
102 |
|
$return['language'] = $options['language']; |
279
|
|
|
} |
280
|
855 |
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Check include adult option |
284
|
|
|
* @param array $options |
285
|
|
|
* @param array &$return Return array to save valid option |
286
|
|
|
* @return void |
287
|
|
|
*/ |
288
|
87 |
|
public function checkOptionIncludeAdult(array $options, array &$return) : void |
289
|
|
|
{ |
290
|
87 |
|
if (isset($options['include_adult'])) { |
291
|
3 |
|
$return['include_adult'] = filter_var($options['include_adult'], FILTER_VALIDATE_BOOLEAN); |
292
|
|
|
} |
293
|
87 |
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Check page option |
297
|
|
|
* @param array $options |
298
|
|
|
* @param array &$return Return array to save valid option |
299
|
|
|
* @return void |
300
|
|
|
*/ |
301
|
114 |
|
public function checkOptionPage(array $options, array &$return) : void |
302
|
|
|
{ |
303
|
114 |
|
if (isset($options['page'])) { |
304
|
3 |
|
$return['page'] = (int) $options['page']; |
305
|
|
|
} |
306
|
114 |
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Check sort by option |
310
|
|
|
* @param array $options |
311
|
|
|
* @param array &$return Return array to save valid option |
312
|
|
|
* @return void |
313
|
|
|
*/ |
314
|
27 |
|
public function checkOptionSortBy(array $options, array &$return) : void |
315
|
|
|
{ |
316
|
27 |
|
if (isset($options['sort_by'])) { |
317
|
6 |
|
switch ($options['sort_by']) { |
318
|
6 |
|
case 'asc': |
319
|
3 |
|
case 'desc': |
320
|
3 |
|
break; |
321
|
|
|
default: |
322
|
3 |
|
throw new IncorrectParamException; |
323
|
|
|
} |
324
|
3 |
|
$return['sort_by'] = 'created_at.'.$options['sort_by']; |
325
|
|
|
} |
326
|
24 |
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Check query option |
330
|
|
|
* @param array $options |
331
|
|
|
* @param array &$return Return array to save valid option |
332
|
|
|
* @return void |
333
|
|
|
*/ |
334
|
84 |
|
public function checkOptionQuery(array $options, array &$return) : void |
335
|
|
|
{ |
336
|
84 |
|
if (isset($options['query'])) { |
337
|
84 |
|
$return['query'] = trim($options['query']); |
338
|
|
|
} |
339
|
84 |
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Check session_id option |
343
|
|
|
* @param array $options |
344
|
|
|
* @param array &$return Return array to save valid option |
345
|
|
|
* @return void |
346
|
|
|
*/ |
347
|
21 |
|
public function checkOptionSessionId(array $options, array &$return) : void |
348
|
|
|
{ |
349
|
21 |
|
if (isset($options['session_id'])) { |
350
|
21 |
|
$return['session_id'] = trim($options['session_id']); |
351
|
|
|
} |
352
|
21 |
|
} |
353
|
|
|
} |
354
|
|
|
|