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