| Conditions | 14 |
| Paths | 14 |
| Total Lines | 60 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 43 | public function send(RequestInterface $request) |
||
| 44 | { |
||
| 45 | $headers = []; |
||
| 46 | foreach ($request->getHeaders() as $name => $values) { |
||
| 47 | $headers[] = $name . ': ' . implode(', ', $values); |
||
| 48 | } |
||
| 49 | |||
| 50 | $before = microtime(true); |
||
| 51 | $curl = curl_init(); |
||
| 52 | curl_setopt($curl, CURLOPT_URL, strval($request->getUri())); |
||
| 53 | curl_setopt($curl, CURLOPT_CONNECTTIMEOUT_MS, $this->timeoutMillis); |
||
| 54 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
| 55 | curl_setopt($curl, CURLOPT_POST, true); |
||
| 56 | curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getBody()->getContents()); |
||
| 57 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); |
||
| 58 | curl_setopt($curl, CURLOPT_HEADER, 1); |
||
| 59 | |||
| 60 | $response = curl_exec($curl); |
||
| 61 | |||
| 62 | $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
| 63 | $errno = curl_errno($curl); |
||
| 64 | $error = curl_error($curl); |
||
| 65 | curl_close($curl); |
||
| 66 | |||
| 67 | if ($errno === CURLE_OPERATION_TIMEOUTED) { |
||
| 68 | // Timeout |
||
| 69 | throw new TimeoutException(sprintf('Transport timeout after %.2f seconds wait', microtime(true) - $before)); |
||
| 70 | } elseif ($errno === CURLE_SSL_CACERT |
||
| 71 | || $errno === CURLE_SSL_CERTPROBLEM |
||
| 72 | || $errno === CURLE_SSL_CIPHER |
||
| 73 | || $errno === CURLE_SSL_CONNECT_ERROR |
||
| 74 | || $errno === CURLE_SSL_PEER_CERTIFICATE |
||
| 75 | || $errno === CURLE_SSL_ENGINE_NOTFOUND |
||
| 76 | || $errno === CURLE_SSL_ENGINE_SETFAILED |
||
| 77 | ) { |
||
| 78 | // SSL error |
||
| 79 | throw new IoException('Transport SSL error ' . $error, intval($errno)); |
||
| 80 | } elseif ($errno !== CURLE_OK) { |
||
| 81 | // Other error |
||
| 82 | throw new IoException('Curl error ' . $error, intval($errno)); |
||
| 83 | } |
||
| 84 | |||
| 85 | if ($response === false) { |
||
| 86 | throw new IoException(sprintf('Curl error. Received status %s, curl error %s', $status, $error)); |
||
| 87 | } |
||
| 88 | |||
| 89 | list($rawHeaders, $body) = explode("\r\n\r\n", $response, 2); |
||
| 90 | $rawHeaders = explode("\n", $rawHeaders); |
||
| 91 | $headers = array(); |
||
| 92 | foreach ($rawHeaders as $row) { |
||
| 93 | $row = trim($row); |
||
| 94 | $index = strpos($row, ':'); |
||
| 95 | if ($index > 0) { |
||
| 96 | $headers[substr($row, 0, $index)] = trim(substr($row, $index + 1)); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | // Building response |
||
| 101 | return new Response($status, $headers, $body); |
||
| 102 | } |
||
| 103 | } |
||
| 104 |