| Conditions | 10 |
| Paths | 2 |
| Total Lines | 63 |
| Code Lines | 45 |
| 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 |
||
| 38 | protected function handleApiCall($url, $method, array $headers = [], $postData = null) |
||
| 39 | { |
||
| 40 | $this->outputCli('', true); |
||
| 41 | $this->outputCli(sprintf('REQUEST: %s %s', $method, $url), true); |
||
| 42 | |||
| 43 | foreach ($headers as $key => $value) { |
||
| 44 | $this->curlBrowser->setRequestHeader($key, $value); |
||
| 45 | } |
||
| 46 | |||
| 47 | switch ($method) { |
||
| 48 | case Http::METHOD_POST: |
||
| 49 | $this->curlBrowser->setPostData($postData); |
||
| 50 | break; |
||
| 51 | case Http::METHOD_GET: |
||
| 52 | case Http::METHOD_HEAD: |
||
| 53 | break; |
||
| 54 | default: |
||
| 55 | throw new \WebServCo\Framework\Exceptions\NotImplementedException('Functionality not implemented'); |
||
| 56 | break; |
||
| 57 | } |
||
| 58 | $this->curlBrowser->setMethod($method); |
||
| 59 | $this->httpResponse = $this->curlBrowser->retrieve($url); |
||
| 60 | |||
| 61 | $this->requestHeaders = $this->curlBrowser->getRequestHeaders(); |
||
| 62 | foreach ($this->requestHeaders as $key => $value) { |
||
| 63 | $this->outputCli(sprintf('%s: %s', Ansi::sgr($key, [Sgr::BOLD]), $value), true); |
||
| 64 | } |
||
| 65 | if (Http::METHOD_POST == $method) { |
||
| 66 | $this->outputCli('', true); |
||
| 67 | $this->outputCli($postData, true); |
||
| 68 | } |
||
| 69 | |||
| 70 | $this->responseStatus = $this->httpResponse->getStatus(); |
||
| 71 | $this->responseHeaders = $this->httpResponse->getHeaders(); |
||
| 72 | $this->responseContent = $this->httpResponse->getContent(); |
||
| 73 | |||
| 74 | $this->outputCli('', true); |
||
| 75 | $this->outputCli( |
||
| 76 | sprintf( |
||
| 77 | 'RESPONSE: %s', |
||
| 78 | Ansi::sgr( |
||
| 79 | sprintf( |
||
| 80 | '%s %s', |
||
| 81 | $this->responseStatus, |
||
| 82 | Http::$statusCodes[$this->responseStatus] ?: null |
||
| 83 | ), |
||
| 84 | [400 > $this->responseStatus ? Sgr::GREEN : sgr::RED] |
||
| 85 | ) |
||
| 86 | ), |
||
| 87 | true |
||
| 88 | ); |
||
| 89 | foreach ($this->responseHeaders as $key => $value) { |
||
| 90 | $this->outputCli(sprintf('%s: %s', Ansi::sgr($key, [Sgr::BOLD]), $value), true); |
||
| 91 | } |
||
| 92 | $this->outputCli('', true); |
||
| 93 | $this->outputCli($this->responseContent, true); |
||
| 94 | |||
| 95 | $this->outputCli('', true); |
||
| 96 | $this->outputCli( |
||
| 97 | sprintf('Processed result: %s', json_encode(json_decode($this->responseContent, true), JSON_PRETTY_PRINT)), |
||
| 98 | true |
||
| 99 | ); |
||
| 100 | return true; |
||
| 101 | } |
||
| 119 |