1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace vfalies\tmdb; |
4
|
|
|
|
5
|
|
|
use vfalies\tmdb\Interfaces\TmdbInterface; |
6
|
|
|
use vfalies\tmdb\Interfaces\HttpRequestInterface; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use vfalies\tmdb\lib\Guzzle\Client as HttpClient; |
9
|
|
|
use vfalies\tmdb\Exceptions\IncorrectParamException; |
10
|
|
|
use vfalies\tmdb\Exceptions\ServerErrorException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Tmdb wrapper core class |
14
|
|
|
*/ |
15
|
|
|
class Tmdb implements TmdbInterface |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
// Private variables |
19
|
|
|
private $api_key = null; // API Key |
20
|
|
|
private $language = 'fr-FR'; // Default language for API response'; // Base URL of the API |
21
|
|
|
private $include_adult = false; // Include adult content in search result |
22
|
|
|
private $page = 1; // API Page result |
23
|
|
|
// Protected variables |
24
|
|
|
protected $configuration = null; // API Configuration |
25
|
|
|
protected $genres = null; // API Genres |
26
|
|
|
// Public variables |
27
|
|
|
public $base_api_url = 'https://api.themoviedb.org/3/'; // Base URL of the API |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* |
31
|
|
|
* @var LoggerInterface |
32
|
|
|
*/ |
33
|
|
|
public $logger = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor |
37
|
|
|
* @param string $api_key TMDB API Key |
38
|
|
|
* @param LoggerInterface $logger Logger used in the class |
39
|
|
|
*/ |
40
|
155 |
|
public function __construct(string $api_key, LoggerInterface $logger) |
41
|
|
|
{ |
42
|
155 |
|
$this->api_key = $api_key; |
43
|
155 |
|
$this->logger = $logger; |
44
|
155 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Send request to TMDB API |
48
|
|
|
* @param HttpRequestInterface $http_request |
49
|
|
|
* @param string $action API action to request |
50
|
|
|
* @param string $query Query of the request (optional) |
51
|
|
|
* @param array $options Array of options of the request (optional) |
52
|
|
|
* @return \stdClass |
53
|
|
|
*/ |
54
|
4 |
|
public function sendRequest(HttpRequestInterface $http_request, string $action, string $query = null, array $options = array()): \stdClass |
55
|
|
|
{ |
56
|
4 |
|
$this->logger->debug('Start sending HTTP request'); |
57
|
4 |
|
$url = $this->buildHTTPUrl($action, $query, $options); |
58
|
4 |
|
$res = $http_request->getResponse($url); |
59
|
|
|
|
60
|
4 |
|
$response = json_decode($res->getBody()); |
61
|
4 |
|
if (empty($response)) { |
62
|
3 |
|
$this->logger->error('Request Body can not be decode', array('action' => $action, 'query' => $query, 'options' => $options)); |
63
|
3 |
|
throw new ServerErrorException(); |
64
|
|
|
} |
65
|
1 |
|
return $response; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Build URL for HTTP Call |
70
|
|
|
* @param string $action API action to request |
71
|
|
|
* @param string $query Query of the request (optional) |
72
|
|
|
* @param array $options Array of options of the request (optional) |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
4 |
|
private function buildHTTPUrl($action, $query, $options) |
76
|
|
|
{ |
77
|
|
|
// Url construction |
78
|
4 |
|
$url = $this->base_api_url . $action; |
79
|
|
|
|
80
|
|
|
// Parameters |
81
|
4 |
|
$params = []; |
82
|
4 |
|
$params['api_key'] = $this->api_key; |
83
|
4 |
|
if (!is_null($query)) { |
84
|
1 |
|
$params['query'] = $query; |
85
|
|
|
} |
86
|
|
|
|
87
|
4 |
|
$params = array_merge($params, $options); |
88
|
|
|
|
89
|
|
|
// URL with paramters construction |
90
|
4 |
|
$url = $url . '?' . http_build_query($params); |
91
|
|
|
|
92
|
4 |
|
return $url; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get API Configuration |
97
|
|
|
* @return \stdClass |
98
|
|
|
* @throws TmdbException |
99
|
|
|
*/ |
100
|
8 |
|
public function getConfiguration(): \stdClass |
101
|
|
|
{ |
102
|
|
|
try { |
103
|
8 |
|
$this->logger->debug('Start getting configuration'); |
104
|
8 |
|
if (is_null($this->configuration)) { |
105
|
8 |
|
$this->logger->debug('No configuration found, sending HTTP request to get it'); |
106
|
8 |
|
$this->configuration = $this->sendRequest(new HttpClient(new \GuzzleHttp\Client()), 'configuration'); |
107
|
|
|
} |
108
|
7 |
|
return $this->configuration; |
109
|
1 |
|
} catch (TmdbException $ex) { |
110
|
1 |
|
throw $ex; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Check options rules before send request |
116
|
|
|
* @param array $options Array of options to validate |
117
|
|
|
* @return array |
118
|
|
|
* @throws IncorrectParamException |
119
|
|
|
*/ |
120
|
135 |
|
public function checkOptions(array $options): array |
121
|
|
|
{ |
122
|
135 |
|
$params = []; |
123
|
|
|
// Set default options |
124
|
135 |
|
$params['language'] = $this->language; |
125
|
135 |
|
$params['include_adult'] = $this->include_adult; |
126
|
135 |
|
$params['page'] = $this->page; |
127
|
|
|
// Check options |
128
|
135 |
|
foreach ($options as $key => $value) { |
129
|
|
|
switch ($key) { |
130
|
28 |
|
case 'year': |
131
|
2 |
|
$params[$key] = $this->checkYear($value); |
132
|
1 |
|
break; |
133
|
27 |
|
case 'language': |
134
|
24 |
|
$params[$key] = $this->checkLanguage($value); |
135
|
23 |
|
break; |
136
|
4 |
|
case 'include_adult': |
137
|
1 |
|
$params[$key] = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
138
|
1 |
|
break; |
139
|
4 |
|
case 'page': |
140
|
1 |
|
$params[$key] = (int) $value; |
141
|
1 |
|
break; |
142
|
|
|
default: |
143
|
3 |
|
$this->logger->error('Unknown param options', array('options', $options)); |
144
|
26 |
|
throw new IncorrectParamException; |
145
|
|
|
} |
146
|
|
|
} |
147
|
130 |
|
return $params; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Check year format |
152
|
|
|
* @param mixed $year year to validate |
153
|
|
|
* @return int year validated |
154
|
|
|
* @throws \Exception |
155
|
|
|
*/ |
156
|
1 |
|
private function checkYear(int $year): int |
157
|
|
|
{ |
158
|
1 |
|
$year = (int) $year; |
159
|
1 |
|
return $year; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Check language |
164
|
|
|
* @param string $language Language string with format ISO 639-1 |
165
|
|
|
* @return string Language string validated |
166
|
|
|
* @throws IncorrectParamException |
167
|
|
|
*/ |
168
|
24 |
|
private function checkLanguage(string $language): string |
169
|
|
|
{ |
170
|
24 |
|
$check = preg_match("#([a-z]{2})-([A-Z]{2})#", $language); |
171
|
24 |
|
if ($check === 0 || $check === false) { |
172
|
1 |
|
$this->logger->error('Incorrect language param option', array('language' => $language)); |
173
|
1 |
|
throw new IncorrectParamException; |
174
|
|
|
} |
175
|
23 |
|
return $language; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|