Total Complexity | 64 |
Total Lines | 417 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Complex classes like RequestService 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 RequestService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | abstract class RequestService implements RequestServiceInterface |
||
39 | { |
||
40 | |||
41 | /** |
||
42 | * Minimum supported version of editors |
||
43 | * |
||
44 | * @var float |
||
45 | */ |
||
46 | private const MIN_EDITORS_VERSION = 6.0; |
||
47 | |||
48 | protected SettingsManager $settingsManager; |
||
49 | protected JwtManager $jwtManager; |
||
50 | |||
51 | abstract public function getFileUrlForConvert(); |
||
52 | |||
53 | public function __construct( |
||
54 | SettingsManager $settingsManager, |
||
55 | HttpClientInterface $httpClient, |
||
56 | JwtManager $jwtManager |
||
57 | ) { |
||
58 | $this->settingsManager = $settingsManager; |
||
59 | $this->jwtManager = $jwtManager; |
||
60 | $this->httpClient = $httpClient; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Request to Document Server |
||
65 | * |
||
66 | * @param string $url - request address |
||
67 | * @param array $method - request method |
||
68 | * @param array $opts - request options |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | public function request($url, $method = "GET", $opts = []) |
||
73 | { |
||
74 | if ($this->settingsManager->isIgnoreSSL()) { |
||
75 | $opts["verify"] = false; |
||
76 | } |
||
77 | |||
78 | if (!array_key_exists("timeout", $opts)) { |
||
79 | $opts["timeout"] = 60; |
||
80 | } |
||
81 | |||
82 | $this->httpClient->request($url, $method, $opts); |
||
83 | if ($this->httpClient->getStatusCode() === 200) { |
||
84 | return $this->httpClient->getBody(); |
||
85 | } |
||
86 | |||
87 | return ""; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Generate an error code table of convertion |
||
92 | * |
||
93 | * @param int $errorCode - Error code |
||
94 | * |
||
95 | * @throws Exception |
||
96 | */ |
||
97 | public function processConvServResponceError($errorCode) |
||
98 | { |
||
99 | $errorMessage = ''; |
||
100 | |||
101 | switch ($errorCode) { |
||
102 | case ConvertResponseError::UNKNOWN: |
||
103 | $errorMessage = ConvertResponseError::message(ConvertResponseError::UNKNOWN); |
||
104 | break; |
||
105 | case ConvertResponseError::TIMEOUT: |
||
106 | $errorMessage = ConvertResponseError::message(ConvertResponseError::TIMEOUT); |
||
107 | break; |
||
108 | case ConvertResponseError::CONVERSION: |
||
109 | $errorMessage = ConvertResponseError::message(ConvertResponseError::CONVERSION); |
||
110 | break; |
||
111 | case ConvertResponseError::DOWNLOADING: |
||
112 | $errorMessage = ConvertResponseError::message(ConvertResponseError::DOWNLOADING); |
||
113 | break; |
||
114 | case ConvertResponseError::PASSWORD: |
||
115 | $errorMessage = ConvertResponseError::message(ConvertResponseError::PASSWORD); |
||
116 | break; |
||
117 | case ConvertResponseError::DATABASE: |
||
118 | $errorMessage = ConvertResponseError::message(ConvertResponseError::DATABASE); |
||
119 | break; |
||
120 | case ConvertResponseError::INPUT: |
||
121 | $errorMessage = ConvertResponseError::message(ConvertResponseError::INPUT); |
||
122 | break; |
||
123 | case ConvertResponseError::TOKEN: |
||
124 | $errorMessage = ConvertResponseError::message(ConvertResponseError::TOKEN); |
||
125 | break; |
||
126 | default: |
||
127 | $errorMessage = "ErrorCode = " . $errorCode; |
||
128 | break; |
||
129 | } |
||
130 | |||
131 | throw new \Exception($errorMessage); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Generate an error code table of command |
||
136 | * |
||
137 | * @param string $errorCode - Error code |
||
138 | * |
||
139 | * @throws Exception |
||
140 | */ |
||
141 | public function processCommandServResponceError($errorCode) |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Request health status |
||
176 | * |
||
177 | * @throws Exception |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | public function healthcheckRequest() : bool |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Request for conversion to a service |
||
194 | * |
||
195 | * @param string $documentUri - Uri for the document to convert |
||
196 | * @param string $fromExtension - Document extension |
||
197 | * @param string $toExtension - Extension to which to convert |
||
198 | * @param string $documentRevisionId - Key for caching on service |
||
199 | * @param bool - $isAsync - Perform conversions asynchronously |
||
|
|||
200 | * @param string $region - Region |
||
201 | * |
||
202 | * @throws Exception |
||
203 | * |
||
204 | * @return array |
||
205 | */ |
||
206 | public function sendRequestToConvertService( |
||
207 | $documentUri, |
||
208 | $fromExtension, |
||
209 | $toExtension, |
||
210 | $documentRevisionId, |
||
211 | $isAsync, |
||
212 | $region = null |
||
213 | ) { |
||
214 | $urlToConverter = $this->settingsManager->getConvertServiceUrl(true); |
||
215 | if (empty($urlToConverter)) { |
||
216 | throw new \Exception(CommonError::message(CommonError::NO_CONVERT_SERVICE_ENDPOINT)); |
||
217 | } |
||
218 | |||
219 | if (empty($documentRevisionId)) { |
||
220 | $documentRevisionId = $documentUri; |
||
221 | } |
||
222 | $documentRevisionId = DocumentManager::generateRevisionId($documentRevisionId); |
||
223 | |||
224 | if (empty($fromExtension)) { |
||
225 | $fromExtension = pathinfo($documentUri)["extension"]; |
||
226 | } else { |
||
227 | $fromExtension = trim($fromExtension, "."); |
||
228 | } |
||
229 | |||
230 | $data = new ConvertRequest; |
||
231 | $data->setAsync($isAsync); |
||
232 | $data->setUrl($documentUri); |
||
233 | $data->setOutputtype(trim($toExtension, ".")); |
||
234 | $data->setFiletype($fromExtension); |
||
235 | $data->setTitle($documentRevisionId . "." . $fromExtension); |
||
236 | $data->setKey($documentRevisionId); |
||
237 | |||
238 | if (!is_null($region)) { |
||
239 | $data->setRegion($region); |
||
240 | } |
||
241 | |||
242 | $opts = [ |
||
243 | "timeout" => "120", |
||
244 | "headers" => [ |
||
245 | 'Content-type' => 'application/json' |
||
246 | ], |
||
247 | "body" => json_encode($data) |
||
248 | ]; |
||
249 | |||
250 | if ($this->jwtManager->isJwtEnabled()) { |
||
251 | $params = [ |
||
252 | "payload" => json_decode(json_encode($data), true) |
||
253 | ]; |
||
254 | $token = $this->jwtManager->jwtEncode($params); |
||
255 | $jwtHeader = $this->settingsManager->getJwtHeader(); |
||
256 | $jwtPrefix = $this->settingsManager->getJwtPrefix(); |
||
257 | |||
258 | if (empty($jwtHeader)) { |
||
259 | throw new \Exception(CommonError::message(CommonError::NO_JWT_HEADER)); |
||
260 | } elseif (empty($jwtPrefix)) { |
||
261 | throw new \Exception(CommonError::message(CommonError::NO_JWT_PREFIX)); |
||
262 | } |
||
263 | |||
264 | $opts["headers"][$jwtHeader] = (string)$jwtPrefix . $token; |
||
265 | $token = $this->jwtManager->jwtEncode(json_decode(json_encode($data), true)); |
||
266 | $data->setToken($token); |
||
267 | $opts["body"] = json_encode($data); |
||
268 | } |
||
269 | |||
270 | |||
271 | $responseXmlData = $this->request($urlToConverter, "POST", $opts); |
||
272 | libxml_use_internal_errors(true); |
||
273 | |||
274 | if (!function_exists("simplexml_load_file")) { |
||
275 | throw new \Exception(CommonError::message(CommonError::READ_XML)); |
||
276 | } |
||
277 | |||
278 | $responseData = simplexml_load_string($responseXmlData); |
||
279 | |||
280 | if (!$responseData) { |
||
281 | $exc = CommonError::message(CommonError::BAD_RESPONSE_XML); |
||
282 | foreach (libxml_get_errors() as $error) { |
||
283 | $exc = $exc . PHP_EOL . $error->message; |
||
284 | } |
||
285 | throw new \Exception($exc); |
||
286 | } |
||
287 | |||
288 | return $responseData; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * The method is to convert the file to the required format and return the result url |
||
293 | * |
||
294 | * @param string $documentUri - Uri for the document to convert |
||
295 | * @param string $fromExtension - Document extension |
||
296 | * @param string $toExtension - Extension to which to convert |
||
297 | * @param string $documentRevisionId - Key for caching on service |
||
298 | * @param string $region - Region |
||
299 | * |
||
300 | * @return string |
||
301 | */ |
||
302 | public function getConvertedUri($documentUri, $fromExtension, $toExtension, $documentRevisionId, $region = null) |
||
303 | { |
||
304 | $responseFromConvertService = $this->sendRequestToConvertService( |
||
305 | $documentUri, |
||
306 | $fromExtension, |
||
307 | $toExtension, |
||
308 | $documentRevisionId, |
||
309 | false, |
||
310 | $region |
||
311 | ); |
||
312 | // phpcs:ignore |
||
313 | $errorElement = $responseFromConvertService->Error; |
||
314 | if ($errorElement->count() > 0) { |
||
315 | $this->processConvServResponceError($errorElement); |
||
316 | } |
||
317 | |||
318 | // phpcs:ignore |
||
319 | $isEndConvert = $responseFromConvertService->EndConvert; |
||
320 | |||
321 | if ($isEndConvert !== null && strtolower($isEndConvert) === "true") { |
||
322 | // phpcs:ignore |
||
323 | return is_string($responseFromConvertService->FileUrl) ? $responseFromConvertService->FileUrl : $responseFromConvertService->FileUrl->__toString(); |
||
324 | } |
||
325 | |||
326 | return ""; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Send command |
||
331 | * |
||
332 | * @param string $method - type of command |
||
333 | * |
||
334 | * @return array |
||
335 | */ |
||
336 | public function commandRequest($method) |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Checking document service location |
||
383 | * |
||
384 | * @return array |
||
385 | */ |
||
386 | public function checkDocServiceUrl() |
||
457 |