| Conditions | 13 |
| Paths | 432 |
| Total Lines | 101 |
| Code Lines | 75 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 24 | public function execute(Request $request, $params = []) |
||
| 25 | { |
||
| 26 | $connection = $this->getConnection(); |
||
| 27 | $conn = $this->getCurlConnection($connection->getConfig('persistent')); |
||
|
|
|||
| 28 | $url = $this->scheme . '://' . $connection->getHost() . ':' . $connection->getPort() . $connection->getPath(); |
||
| 29 | $endpoint = $request->getPath(); |
||
| 30 | $url .= $endpoint; |
||
| 31 | $url = $this->setupURI($url, $request->getQuery()); |
||
| 32 | |||
| 33 | curl_setopt($conn, CURLOPT_URL, $url); |
||
| 34 | curl_setopt($conn, CURLOPT_TIMEOUT, $connection->getTimeout()); |
||
| 35 | curl_setopt($conn, CURLOPT_ENCODING, ''); |
||
| 36 | curl_setopt($conn, CURLOPT_FORBID_REUSE, 0); |
||
| 37 | $data = $request->getBody(); |
||
| 38 | $method = $request->getMethod(); |
||
| 39 | $headers = $connection->getHeaders(); |
||
| 40 | $headers[] = sprintf('Content-Type: %s', $request->getContentType()); |
||
| 41 | if (!empty($data)) { |
||
| 42 | if (is_array($data)) { |
||
| 43 | $content = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
||
| 44 | } else { |
||
| 45 | $content = $data; |
||
| 46 | } |
||
| 47 | curl_setopt($conn, CURLOPT_POSTFIELDS, $content); |
||
| 48 | } else { |
||
| 49 | curl_setopt($conn, CURLOPT_POSTFIELDS, ''); |
||
| 50 | } |
||
| 51 | curl_setopt($conn, CURLOPT_CUSTOMREQUEST, $method); |
||
| 52 | curl_setopt($conn, CURLOPT_HTTPHEADER, $headers); |
||
| 53 | |||
| 54 | if ($connection->getConnectTimeout()>0) { |
||
| 55 | curl_setopt($conn, CURLOPT_CONNECTTIMEOUT, $connection->getConnectTimeout()); |
||
| 56 | } |
||
| 57 | |||
| 58 | if ($connection->getConfig('username') !== null && $connection->getConfig('password') !== null) { |
||
| 59 | curl_setopt($conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY); |
||
| 60 | curl_setopt( |
||
| 61 | $conn, |
||
| 62 | CURLOPT_USERPWD, |
||
| 63 | $connection->getConfig('username').":".$connection->getConfig('password') |
||
| 64 | ); |
||
| 65 | } |
||
| 66 | if ($connection->getConfig('proxy') !== null) { |
||
| 67 | curl_setopt($conn, CURLOPT_PROXY, $connection->getConfig('proxy')); |
||
| 68 | } |
||
| 69 | if (!empty($connection->getConfig('curl'))) { |
||
| 70 | foreach ($connection->getConfig('curl') as $k => $v) { |
||
| 71 | curl_setopt($conn, $k, $v); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | $start = microtime(true); |
||
| 75 | ob_start(); |
||
| 76 | curl_exec($conn); |
||
| 77 | $responseString = \ob_get_clean(); |
||
| 78 | $end = microtime(true); |
||
| 79 | $errorno = curl_errno($conn); |
||
| 80 | $status = curl_getinfo($conn, CURLINFO_HTTP_CODE); |
||
| 81 | if (isset($params['responseClass'])) { |
||
| 82 | $responseClass = $params['responseClass']; |
||
| 83 | $responseClassParams = isset($params['responseClassParams'])?$params['responseClassParams']:[]; |
||
| 84 | $response = new $responseClass($responseString, $status, $responseClassParams); |
||
| 85 | } else { |
||
| 86 | $response = new Response($responseString, $status); |
||
| 87 | } |
||
| 88 | |||
| 89 | $time = $end-$start; |
||
| 90 | $response->setTime($time); |
||
| 91 | $response->setTransportInfo([ |
||
| 92 | 'url' => $url, |
||
| 93 | 'headers' => $headers, |
||
| 94 | 'body' => $request->getBody() |
||
| 95 | ]); |
||
| 96 | //hard error |
||
| 97 | if ($errorno>0) { |
||
| 98 | $error = curl_error($conn); |
||
| 99 | |||
| 100 | static::$curl = null; |
||
| 101 | throw new ConnectionException($error, $request); |
||
| 102 | } |
||
| 103 | |||
| 104 | |||
| 105 | $this->logger->debug('Request body:', [ |
||
| 106 | 'connection' => $connection->getConfig(), |
||
| 107 | 'payload' => $request->getBody() |
||
| 108 | ]); |
||
| 109 | $this->logger->info('Request:', [ |
||
| 110 | 'url' => $url, |
||
| 111 | 'status' => $status, |
||
| 112 | 'time' => $time, |
||
| 113 | ]); |
||
| 114 | $this->logger->debug('Response body:', [json_decode($responseString, true)]); |
||
| 115 | //soft error |
||
| 116 | if ($response->hasError()) { |
||
| 117 | $this->logger->error('Response:', [ |
||
| 118 | 'url' => $url, |
||
| 119 | 'error' => $response->getError(), |
||
| 120 | 'payload' => $request->getBody(), |
||
| 121 | ]); |
||
| 122 | throw new ResponseException($request, $response); |
||
| 123 | } |
||
| 124 | return $response; |
||
| 125 | } |
||
| 135 |