| Conditions | 11 |
| Paths | 81 |
| Total Lines | 86 |
| Code Lines | 51 |
| 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 |
||
| 201 | protected function retrieve($url) |
||
| 202 | { |
||
| 203 | $this->debugInit(); |
||
| 204 | |||
| 205 | |||
| 206 | $this->curl = curl_init(); |
||
| 207 | if (!is_resource($this->curl)) { |
||
| 208 | throw new ApplicationException('Not a valid resource'); |
||
| 209 | } |
||
| 210 | curl_setopt_array( |
||
| 211 | $this->curl, |
||
| 212 | [ |
||
| 213 | CURLOPT_RETURNTRANSFER => true, /* return instead of outputting */ |
||
| 214 | CURLOPT_URL => $url, |
||
| 215 | CURLOPT_HEADER => true, /* include the header in the output */ |
||
| 216 | CURLOPT_FOLLOWLOCATION => true, /* follow redirects */ |
||
| 217 | CURLOPT_CONNECTTIMEOUT => 60, // The number of seconds to wait while trying to connect. |
||
| 218 | CURLOPT_TIMEOUT => 60, // The maximum number of seconds to allow cURL functions to execute. |
||
| 219 | ] |
||
| 220 | ); |
||
| 221 | if ($this->skipSslVerification) { |
||
| 222 | // stop cURL from verifying the peer's certificate |
||
| 223 | curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false); |
||
| 224 | // don't check the existence of a common name in the SSL peer certificate |
||
| 225 | curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 0); |
||
| 226 | } |
||
| 227 | |||
| 228 | $this->debugDo(); |
||
| 229 | |||
| 230 | switch ($this->method) { |
||
| 231 | case Http::METHOD_POST: |
||
| 232 | curl_setopt($this->curl, CURLOPT_POST, true); |
||
| 233 | if (!empty($this->postData)) { |
||
| 234 | curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->postData); |
||
| 235 | if (!is_array($this->postData)) { |
||
| 236 | $this->setRequestHeader('Content-Length', mb_strlen($this->postData)); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | break; |
||
| 240 | case Http::METHOD_HEAD: |
||
| 241 | curl_setopt($this->curl, CURLOPT_NOBODY, true); |
||
| 242 | break; |
||
| 243 | } |
||
| 244 | |||
| 245 | if (!empty($this->requestHeaders)) { |
||
| 246 | curl_setopt( |
||
| 247 | $this->curl, |
||
| 248 | CURLOPT_HTTPHEADER, |
||
| 249 | $this->parseRequestHeaders($this->requestHeaders) |
||
| 250 | ); |
||
| 251 | } |
||
| 252 | |||
| 253 | $this->response = curl_exec($this->curl); |
||
| 254 | $this->curlError = curl_error($this->curl); |
||
| 255 | if (false === $this->response) { |
||
| 256 | throw new ApplicationException(sprintf("cURL error: %s", $this->curlError)); |
||
| 257 | } |
||
| 258 | |||
| 259 | $this->debugInfo = curl_getinfo($this->curl); |
||
| 260 | |||
| 261 | curl_close($this->curl); |
||
| 262 | |||
| 263 | $httpCode = $this->getHttpCode(); |
||
| 264 | |||
| 265 | if (empty($httpCode)) { |
||
| 266 | throw new ApplicationException(sprintf("Empty HTTP status code. cURL error: %s", $this->curlError)); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * For redirects, the response will contain evey header/body pair. |
||
| 271 | * The last header/body will be at the end of the response. |
||
| 272 | */ |
||
| 273 | $responseParts = explode("\r\n\r\n", (string) $this->response); |
||
| 274 | $body = trim((string) array_pop($responseParts)); |
||
| 275 | |||
| 276 | $this->responseHeaders = []; |
||
| 277 | foreach ($responseParts as $item) { |
||
| 278 | $this->responseHeaders[] = $this->parseResponseHeaders($item); |
||
| 279 | } |
||
| 280 | |||
| 281 | $this->debugFinish(); |
||
| 282 | |||
| 283 | return new \WebServCo\Framework\HttpResponse( |
||
| 284 | $body, |
||
| 285 | $httpCode, |
||
| 286 | end($this->responseHeaders) |
||
| 287 | ); |
||
| 290 |