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