| Conditions | 15 |
| Paths | 121 |
| Total Lines | 74 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 58 | public function retrieveResponse( |
||
| 59 | UriInterface $endpoint, |
||
| 60 | $requestBody, |
||
| 61 | array $extraHeaders = [], |
||
| 62 | $method = 'POST' |
||
| 63 | ) { |
||
| 64 | // Normalize method name |
||
| 65 | $method = strtoupper($method); |
||
| 66 | |||
| 67 | $extraHeaders = $this->normalizeHeaders($extraHeaders); |
||
| 68 | |||
| 69 | if ($method === 'GET' && !empty($requestBody)) { |
||
| 70 | throw new InvalidArgumentException('No body expected for "GET" request.'); |
||
| 71 | } |
||
| 72 | |||
| 73 | if (!isset($extraHeaders['Content-Type']) && $method === 'POST' && is_array($requestBody)) { |
||
| 74 | $extraHeaders['Content-Type'] = 'Content-Type: application/x-www-form-urlencoded'; |
||
| 75 | } |
||
| 76 | |||
| 77 | $extraHeaders['Host'] = 'Host: ' . $endpoint->getHost(); |
||
| 78 | $extraHeaders['Connection'] = 'Connection: close'; |
||
| 79 | |||
| 80 | $ch = curl_init(); |
||
| 81 | |||
| 82 | curl_setopt($ch, CURLOPT_URL, $endpoint->getAbsoluteUri()); |
||
| 83 | |||
| 84 | if ($method === 'POST' || $method === 'PUT') { |
||
| 85 | if ($requestBody && is_array($requestBody)) { |
||
| 86 | $requestBody = http_build_query($requestBody, '', '&'); |
||
| 87 | } |
||
| 88 | |||
| 89 | if ($method === 'PUT') { |
||
| 90 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); |
||
| 91 | } else { |
||
| 92 | curl_setopt($ch, CURLOPT_POST, true); |
||
| 93 | } |
||
| 94 | |||
| 95 | curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody); |
||
| 96 | } else { |
||
| 97 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); |
||
| 98 | } |
||
| 99 | |||
| 100 | if ($this->maxRedirects > 0) { |
||
| 101 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
||
| 102 | curl_setopt($ch, CURLOPT_MAXREDIRS, $this->maxRedirects); |
||
| 103 | } |
||
| 104 | |||
| 105 | curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); |
||
| 106 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
| 107 | curl_setopt($ch, CURLOPT_HEADER, false); |
||
| 108 | curl_setopt($ch, CURLOPT_HTTPHEADER, $extraHeaders); |
||
| 109 | curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); |
||
| 110 | |||
| 111 | foreach ($this->parameters as $key => $value) { |
||
| 112 | curl_setopt($ch, $key, $value); |
||
| 113 | } |
||
| 114 | |||
| 115 | $response = curl_exec($ch); |
||
| 116 | $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
| 117 | |||
| 118 | if (false === $response) { |
||
| 119 | $errNo = curl_errno($ch); |
||
| 120 | $errStr = curl_error($ch); |
||
| 121 | curl_close($ch); |
||
| 122 | if (empty($errStr)) { |
||
| 123 | throw new TokenResponseException('Failed to request resource.', $responseCode); |
||
| 124 | } |
||
| 125 | |||
| 126 | throw new TokenResponseException('cURL Error # ' . $errNo . ': ' . $errStr, $responseCode); |
||
| 127 | } |
||
| 128 | |||
| 129 | curl_close($ch); |
||
| 130 | |||
| 131 | return $response; |
||
|
|
|||
| 132 | } |
||
| 134 |