| Conditions | 8 |
| Paths | 24 |
| Total Lines | 58 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 |
||
| 31 | private function makeCurlRequest( |
||
| 32 | string $httpVerb, |
||
| 33 | string $fullUrl, |
||
| 34 | array $headers = [], |
||
| 35 | array $payload = [], |
||
| 36 | int $maxRedirects = 0, |
||
| 37 | int $retries = 3 |
||
| 38 | ): Response { |
||
| 39 | $allHeaders = array_merge([ |
||
| 40 | 'Content-Type: ' . ($httpVerb === 'post') ? 'application/jose+json' : 'application/json', |
||
| 41 | ], $headers); |
||
| 42 | |||
| 43 | $curlHandle = $this->getCurlHandle($fullUrl, $allHeaders, $maxRedirects); |
||
| 44 | |||
| 45 | switch ($httpVerb) { |
||
| 46 | case 'head': |
||
| 47 | curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, 'HEAD'); |
||
| 48 | curl_setopt($curlHandle, CURLOPT_NOBODY, true); |
||
| 49 | break; |
||
| 50 | |||
| 51 | case 'get': |
||
| 52 | curl_setopt($curlHandle, CURLOPT_URL, $fullUrl . '?' . http_build_query($payload)); |
||
| 53 | break; |
||
| 54 | |||
| 55 | case 'post': |
||
| 56 | curl_setopt($curlHandle, CURLOPT_POST, true); |
||
| 57 | $this->attachRequestPayload($curlHandle, $payload); |
||
| 58 | break; |
||
| 59 | } |
||
| 60 | |||
| 61 | $rawResponse = curl_exec($curlHandle); |
||
| 62 | $headerSize = curl_getinfo($curlHandle, CURLINFO_HEADER_SIZE); |
||
| 63 | $allHeaders = curl_getinfo($curlHandle); |
||
| 64 | |||
| 65 | $rawHeaders = mb_substr($rawResponse, 0, $headerSize); |
||
|
|
|||
| 66 | $rawBody = mb_substr($rawResponse, $headerSize); |
||
| 67 | $body = $rawBody; |
||
| 68 | |||
| 69 | $allHeaders = array_merge($allHeaders, $this->parseRawHeaders($rawHeaders)); |
||
| 70 | |||
| 71 | if (json_validate($rawBody)) { |
||
| 72 | $body = json_decode($rawBody, true, 512, JSON_THROW_ON_ERROR); |
||
| 73 | } |
||
| 74 | |||
| 75 | $httpCode = $allHeaders['http_code'] ?? null; |
||
| 76 | |||
| 77 | // Catch HTTP status code 0 when Let's Encrypt API is having problems. |
||
| 78 | if ($httpCode === 0) { |
||
| 79 | // Retry. |
||
| 80 | if ($retries > 0) { |
||
| 81 | return $this->makeCurlRequest($httpVerb, $fullUrl, $headers, $payload, $maxRedirects, --$retries); |
||
| 82 | } |
||
| 83 | |||
| 84 | // Return 504 Gateway Timeout. |
||
| 85 | $httpCode = 504; |
||
| 86 | } |
||
| 87 | |||
| 88 | return new Response($allHeaders, $allHeaders['url'] ?? '', $httpCode, $body); |
||
| 89 | } |
||
| 142 |