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