Total Complexity | 45 |
Total Lines | 325 |
Duplicated Lines | 0 % |
Changes | 11 | ||
Bugs | 0 | Features | 0 |
Complex classes like Tmdb often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Tmdb, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types = 1); |
||
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 | public function __construct(string $api_key, int $version = 3, LoggerInterface $logger, HttpRequestInterface $http_request) |
||
93 | { |
||
94 | $this->api_key = $api_key; |
||
95 | $this->logger = $logger; |
||
96 | $this->version = $version; |
||
97 | $this->http_request = $http_request; |
||
98 | $this->request = new \stdClass; |
||
99 | } |
||
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 | public function getRequest(string $action, array $options = array()) : ?\stdClass |
||
108 | { |
||
109 | $this->logger->debug('Start sending HTTP request with GET method', array('action' => $action, 'options' => $options)); |
||
110 | $this->url = $this->buildHTTPUrl($action, $options); |
||
111 | 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 | public function postRequest(string $action, array $options = array(), array $form_params = array()) : ?\stdClass |
||
122 | { |
||
123 | $this->logger->debug('Start sending HTTP request with POST method', array('action' => $action, 'options' => $options, 'form_params' => $form_params)); |
||
124 | $this->url = $this->buildHTTPUrl($action, $options); |
||
125 | 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 | public function deleteRequest(string $action, array $options = array()) : ?\stdClass |
||
135 | { |
||
136 | $this->logger->debug('Start sending HTTP request with DELETE method', array('action' => $action, 'options' => $options)); |
||
137 | $this->url = $this->buildHTTPUrl($action, $options); |
||
138 | 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 | protected function sendRequest(string $method, string $url, array $form_params = array()) : ?\stdClass |
||
149 | { |
||
150 | try { |
||
151 | $method_name = strtolower($method).'Response'; |
||
152 | $res = $this->http_request->$method_name($url, [], $form_params); |
||
153 | $response = $this->decodeRequest($res, $method, $url, $form_params); |
||
154 | return $response; |
||
155 | } catch (TmdbException $e) { |
||
156 | $this->logger->error('sendRequest failed : '.$e->getMessage(), array('method' => $method, 'url' => $url, 'form_params' => $form_params)); |
||
157 | 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 | private function decodeRequest($res, $method, $url, $form_params) : \stdClass |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Build URL for HTTP Call |
||
190 | * @param string $action API action to request |
||
191 | * @param array $options Array of options of the request (optional) |
||
192 | * @return string |
||
193 | */ |
||
194 | private function buildHTTPUrl(string $action, array $options) : string |
||
195 | { |
||
196 | // Url construction |
||
197 | $url = $this->base_api_url . $this->version . '/' . $action; |
||
198 | |||
199 | // Parameters |
||
200 | $params = []; |
||
201 | $params['api_key'] = $this->api_key; |
||
202 | |||
203 | $params = array_merge($params, $options); |
||
204 | |||
205 | // URL with paramters construction |
||
206 | $url = $url . '?' . http_build_query($params); |
||
207 | |||
208 | return $url; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Get API Configuration |
||
213 | * @return \stdClass |
||
214 | * @throws TmdbException |
||
215 | */ |
||
216 | public function getConfiguration() : \stdClass |
||
217 | { |
||
218 | try { |
||
219 | $this->logger->debug('Start getting configuration'); |
||
220 | if (is_null($this->configuration)) { |
||
221 | $this->logger->debug('No configuration found, sending HTTP request to get it'); |
||
222 | $this->configuration = $this->getRequest('configuration'); |
||
223 | } |
||
224 | return $this->configuration; |
||
225 | } catch (TmdbException $ex) { |
||
226 | throw $ex; |
||
227 | } |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Get logger |
||
232 | * @return LoggerInterface |
||
233 | */ |
||
234 | public function getLogger() : LoggerInterface |
||
235 | { |
||
236 | return $this->logger; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Magical property getter |
||
241 | * @param string $name Name of the property |
||
242 | * @return string Value of the property |
||
243 | */ |
||
244 | public function __get(string $name) : string |
||
245 | { |
||
246 | switch ($name) { |
||
247 | case 'url': |
||
248 | return $this->$name; |
||
249 | default: |
||
250 | throw new IncorrectParamException; |
||
251 | } |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @inheritDoc |
||
256 | */ |
||
257 | public function checkOptionYear(array $options, array &$return) : void |
||
258 | { |
||
259 | if (isset($options['year'])) { |
||
260 | $return['year'] = (int) $options['year']; |
||
261 | } |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @inheritDoc |
||
266 | */ |
||
267 | public function checkOptionLanguage(array $options, array &$return) : void |
||
268 | { |
||
269 | if (isset($options['language'])) { |
||
270 | $check = preg_match("#([a-z]{2})-([A-Z]{2})#", $options['language']); |
||
271 | if ($check === 0 || $check === false) { |
||
272 | $this->logger->error('Incorrect language param option', array('language' => $options['language'])); |
||
273 | throw new IncorrectParamException; |
||
274 | } |
||
275 | $return['language'] = $options['language']; |
||
276 | } |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * @inheritDoc |
||
281 | */ |
||
282 | public function checkOptionIncludeAdult(array $options, array &$return) : void |
||
283 | { |
||
284 | if (isset($options['include_adult'])) { |
||
285 | $return['include_adult'] = filter_var($options['include_adult'], FILTER_VALIDATE_BOOLEAN); |
||
286 | } |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * @inheritDoc |
||
291 | */ |
||
292 | public function checkOptionPage(array $options, array &$return) : void |
||
293 | { |
||
294 | if (isset($options['page'])) { |
||
295 | $return['page'] = (int) $options['page']; |
||
296 | } |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * @inheritDoc |
||
301 | */ |
||
302 | public function checkOptionSortBy(array $options, array &$return) : void |
||
303 | { |
||
304 | if (isset($options['sort_by'])) { |
||
305 | switch ($options['sort_by']) { |
||
306 | case 'asc': |
||
307 | case 'desc': |
||
308 | break; |
||
309 | default: |
||
310 | throw new IncorrectParamException; |
||
311 | } |
||
312 | $return['sort_by'] = 'created_at.'.$options['sort_by']; |
||
313 | } |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * @inheritDoc |
||
318 | */ |
||
319 | public function checkOptionQuery(array $options, array &$return) : void |
||
323 | } |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * @inheritDoc |
||
328 | */ |
||
329 | public function checkOptionSessionId(array $options, array &$return) : void |
||
333 | } |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * @inheritDoc |
||
338 | */ |
||
339 | public function checkOptionExternalSource(array $options, array &$return) : void |
||
355 | } |
||
356 | } |
||
357 | } |
||
358 | } |
||
359 |