| Conditions | 9 |
| Paths | 27 |
| Total Lines | 57 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 33 | protected function executeApiRequest($urlPath, $method, $requestParams, $templateParamNames, $templateParamValues) |
||
| 34 | { |
||
| 35 | $client = new Client(['base_uri' => getenv('API_URL')]); |
||
| 36 | |||
| 37 | $url = str_replace($templateParamNames, $templateParamValues, $urlPath); |
||
| 38 | $defaultRequestParams = [ |
||
| 39 | 'headers' => [ |
||
| 40 | 'Authorization' => 'apikey ' . getenv('API_KEY'), |
||
| 41 | ] |
||
| 42 | ]; |
||
| 43 | |||
| 44 | try { |
||
| 45 | $response = $client->request($method, $url, array_merge_recursive($requestParams, $defaultRequestParams)); |
||
| 46 | } catch (\Exception $e) { |
||
| 47 | $emergency = true; |
||
| 48 | $emergencyClientStatusCodes = ['404', '403', '401']; |
||
| 49 | if ($e->hasResponse()) { |
||
|
|
|||
| 50 | $response = $e->getResponse(); |
||
| 51 | $statusCode = $response->getStatusCode(); |
||
| 52 | // 500 errors are always considered and emergency |
||
| 53 | if (!in_array($statusCode, $emergencyClientStatusCodes) && !preg_match('/5[0-9][0-9]/', $statusCode)) { |
||
| 54 | $emergency = false; |
||
| 55 | } |
||
| 56 | |||
| 57 | $body = $response->getBody(); |
||
| 58 | try { |
||
| 59 | $sxml = new SimpleXMLElement($body); |
||
| 60 | |||
| 61 | foreach ($sxml->errorList as $error) { |
||
| 62 | $code = $error->error->errorCode; |
||
| 63 | $msg = $error->error->errorMessage; |
||
| 64 | $this->logger->error("Alma API error (HTTP status code: $statusCode Alma Error Code: $code): $msg"); |
||
| 65 | } |
||
| 66 | } catch (\Exception $e1) { |
||
| 67 | /** |
||
| 68 | * The Alma API will return a 400 status code in the event that the API key is invalid. |
||
| 69 | * Unfortunately, this same status code is returned under many other circumstances, for |
||
| 70 | * example if a user provides incorrect credentials to log in. The only way I can figure |
||
| 71 | * out how to distinguish the two is by checking the actual text of the body, which annoyingly |
||
| 72 | * isn't a valid XML response like all the other responses. |
||
| 73 | */ |
||
| 74 | if ($body == 'Invalid API Key') { |
||
| 75 | $this->logger->emergency("@web-irt-dev Critical Error: $body :fire:"); |
||
| 76 | } else { |
||
| 77 | $this->logger->error("Unable to parse response from Alma API as XML. Status code: $statusCode Body: $body"); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | if ($emergency) { |
||
| 83 | $this->logger->emergency("@web-irt-dev Critical Error: Unable to reach the Alma API! :fire:"); |
||
| 84 | } |
||
| 85 | |||
| 86 | throw $e; |
||
| 87 | } |
||
| 88 | |||
| 89 | return $response; |
||
| 90 | } |
||
| 230 |