| Conditions | 12 |
| Paths | 128 |
| Total Lines | 62 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 44 |
| CRAP Score | 12 |
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 |
||
| 139 | 1 | public function sendRequest(RequestInterface $request) { |
|
| 140 | 1 | $response = $this->getClient()->createResponse(); |
|
| 141 | 1 | $this->getCurl()->reset(); |
|
| 142 | $curlopts = array( |
||
| 143 | 1 | CURLOPT_URL => $request->getUrl(), |
|
| 144 | 1 | ); |
|
| 145 | |||
| 146 | 1 | $method = $request->getMethod(); |
|
| 147 | if ($method !== RequestInterface::METHOD_GET |
||
| 148 | 1 | && $method !== RequestInterface::METHOD_POST) { |
|
| 149 | 1 | $curlopts[CURLOPT_CUSTOMREQUEST] = $method; |
|
| 150 | 1 | } |
|
| 151 | |||
| 152 | 1 | $curlopts = $this->getOpts()->getArrayCopy() + $curlopts; |
|
| 153 | |||
| 154 | 1 | if ($request->getHeaders()->count()) { |
|
| 155 | 1 | $headers = array(); |
|
| 156 | 1 | foreach ($request->getHeaders() as $header => $value) { |
|
| 157 | 1 | $headers[] = "{$header}: {$value}"; |
|
| 158 | 1 | } |
|
| 159 | 1 | $curlopts[CURLOPT_HTTPHEADER] = $headers; |
|
| 160 | 1 | } |
|
| 161 | |||
| 162 | 1 | $postfields = array(); |
|
| 163 | if ($method === RequestInterface::METHOD_POST |
||
| 164 | 1 | && $request->getFileParams()->count() |
|
| 165 | 1 | ) { |
|
| 166 | 1 | $postfields = array_merge( |
|
| 167 | 1 | $postfields, |
|
| 168 | 1 | array_map( |
|
| 169 | 1 | array($this->getCurl(), 'preparePostFileField'), |
|
| 170 | 1 | $request->getFileParams()->getArrayCopy())); |
|
| 171 | 1 | } |
|
| 172 | if ($method !== RequestInterface::METHOD_GET |
||
| 173 | 1 | && $request->getBodyParams()->count()) { |
|
| 174 | $postfields |
||
| 175 | 1 | = array_merge($postfields, $request->getBodyParams()->export()); |
|
| 176 | 1 | } |
|
| 177 | |||
| 178 | 1 | if (!empty($postfields)) { |
|
| 179 | 1 | $curlopts[CURLOPT_POSTFIELDS] = $postfields; |
|
| 180 | 1 | } |
|
| 181 | |||
| 182 | 1 | $this->getCurl()->setoptArray($curlopts); |
|
| 183 | 1 | $raw_response = $this->getCurl()->exec(); |
|
| 184 | |||
| 185 | 1 | $status_code = $this->getCurl()->getInfo(CURLINFO_HTTP_CODE); |
|
| 186 | 1 | $curl_errno = $this->getCurl()->errno(); |
|
| 187 | 1 | $curl_error = $curl_errno ? $this->getCurl()->error() : null; |
|
| 188 | |||
| 189 | 1 | $response_parts = $this->extractResponseHeadersAndBody($raw_response); |
|
| 190 | |||
| 191 | 1 | $response->setStatusCode($status_code); |
|
| 192 | 1 | $this->parseHeaders($response->getHeaders(), $response_parts[0]); |
|
| 193 | 1 | $response->setBody($response_parts[1]); |
|
| 194 | |||
| 195 | 1 | if ($curl_errno) { |
|
| 196 | 1 | throw new Exception($curl_error, $curl_errno); |
|
| 197 | } |
||
| 198 | |||
| 199 | 1 | return $response; |
|
| 200 | } |
||
| 201 | } |
||
| 202 |