| Conditions | 22 |
| Paths | 137 |
| Total Lines | 107 |
| Code Lines | 68 |
| 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 |
||
| 116 | protected function executeRequest(RequestInterface $request) |
||
| 117 | { |
||
| 118 | $this->logger->debug( |
||
| 119 | sprintf('HTTP Request %s %s', $request->getMethod(), $request->getUrl()), |
||
| 120 | ['headers' => $request->getHeaders(), 'body' => $request->toArray()] |
||
| 121 | ); |
||
| 122 | |||
| 123 | if (!$request->validate()) { |
||
| 124 | throw new \RuntimeException('Request entity is not valid'); |
||
| 125 | } |
||
| 126 | |||
| 127 | $ch = curl_init(); |
||
| 128 | |||
| 129 | if ($ch === false) { |
||
| 130 | throw new \RuntimeException('Could not get cURL resource'); |
||
| 131 | } |
||
| 132 | |||
| 133 | $options = [ |
||
| 134 | CURLOPT_URL => $request->getUrl(), |
||
| 135 | CURLOPT_HTTPHEADER => $request->getHeaders(), |
||
| 136 | CURLOPT_HTTP_VERSION => $request->getProtocolVersion(), |
||
| 137 | ]; |
||
| 138 | |||
| 139 | $customMethods = [ |
||
| 140 | Request::METHOD_PUT, |
||
| 141 | Request::METHOD_PATCH, |
||
| 142 | Request::METHOD_DELETE, |
||
| 143 | ]; |
||
| 144 | |||
| 145 | if ($request->getMethod() === Request::METHOD_POST) { |
||
| 146 | $options[CURLOPT_POST] = true; |
||
| 147 | $options[CURLOPT_POSTFIELDS] = $request->toArray(); |
||
| 148 | } elseif ($request->getMethod() === Request::METHOD_GET && $request->toArray()) { |
||
| 149 | $paramChar = strpos('?', $request->getUrl()) === false ? '?' : '&'; |
||
| 150 | $options[CURLOPT_URL] = $request->getUrl() . $paramChar . http_build_query($request->toArray()); |
||
| 151 | } elseif (in_array($request->getMethod(), $customMethods)) { |
||
| 152 | $options[CURLOPT_CUSTOMREQUEST] = $request->getMethod(); |
||
| 153 | $options[CURLOPT_POSTFIELDS] = $request->toArray(); |
||
| 154 | } |
||
| 155 | |||
| 156 | if ( |
||
| 157 | isset($options[CURLOPT_POSTFIELDS]) && is_array($options[CURLOPT_POSTFIELDS]) |
||
| 158 | && $request->getHeader('Content-Type') == 'application/x-www-form-urlencoded' |
||
| 159 | ) { |
||
| 160 | $options[CURLOPT_POSTFIELDS] = http_build_query($options[CURLOPT_POSTFIELDS]); |
||
|
|
|||
| 161 | } elseif ( |
||
| 162 | isset($options[CURLOPT_POSTFIELDS]) && is_array($options[CURLOPT_POSTFIELDS]) |
||
| 163 | && $request->getHeader('Content-Type') == 'application/json' |
||
| 164 | ) { |
||
| 165 | $options[CURLOPT_POSTFIELDS] = json_encode($options[CURLOPT_POSTFIELDS]); |
||
| 166 | } |
||
| 167 | |||
| 168 | curl_setopt_array($ch, $this->getRequestOptions($options)); |
||
| 169 | |||
| 170 | $result = curl_exec($ch); |
||
| 171 | $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
| 172 | $errorMessage = curl_error($ch); |
||
| 173 | $errorNumber = curl_errno($ch); |
||
| 174 | |||
| 175 | curl_close($ch); |
||
| 176 | |||
| 177 | $this->logger->debug( |
||
| 178 | sprintf('HTTP Response %s %s', $request->getMethod(), $request->getUrl()), |
||
| 179 | ['httpCode' => $httpCode, 'body' => $result, 'curlError' => $errorMessage] |
||
| 180 | ); |
||
| 181 | |||
| 182 | if ($errorNumber !== 0) { |
||
| 183 | throw new PayeverCommunicationException($errorMessage, $errorNumber); |
||
| 184 | } |
||
| 185 | |||
| 186 | if ($httpCode >= 400) { |
||
| 187 | $message = $result; |
||
| 188 | $data = json_decode($result, true); |
||
| 189 | |||
| 190 | if (isset($data['error_description'])) { |
||
| 191 | $message = $data['error_description']; |
||
| 192 | } elseif (isset($data['message'])) { |
||
| 193 | $message = $data['message']; |
||
| 194 | } |
||
| 195 | |||
| 196 | if (isset($data['error'])) { |
||
| 197 | $message = sprintf( |
||
| 198 | '%s: %s', |
||
| 199 | $data['error'], |
||
| 200 | $message |
||
| 201 | ? (is_array($message) ? json_encode($message) : $message) |
||
| 202 | : 'Unknown' |
||
| 203 | ); |
||
| 204 | } |
||
| 205 | |||
| 206 | throw new PayeverCommunicationException($message, $httpCode); |
||
| 207 | } |
||
| 208 | |||
| 209 | $response = new Response(); |
||
| 210 | |||
| 211 | $response |
||
| 212 | ->setRequestEntity($request->getRequestEntity()) |
||
| 213 | ->setResponseEntity($request->getResponseEntity()) |
||
| 214 | ->load($result); |
||
| 215 | |||
| 216 | if ($response->isFailed()) { |
||
| 217 | throw new PayeverCommunicationException( |
||
| 218 | $response->getResponseEntity()->getErrorDescription() |
||
| 219 | ); |
||
| 220 | } |
||
| 221 | |||
| 222 | return $response; |
||
| 223 | } |
||
| 327 |