| Conditions | 24 |
| Paths | > 20000 |
| Total Lines | 151 |
| 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 |
||
| 121 | public function call() { |
||
| 122 | |||
| 123 | // Define the necessary argurments. |
||
| 124 | $curlHeaders = $this->mergeHeaders(); |
||
| 125 | $curlPOSTData = http_build_query($this->getPostData()); |
||
| 126 | |||
| 127 | // |
||
| 128 | if (true === in_array("Content-Type: application/json", $curlHeaders)) { |
||
| 129 | $curlPOSTData = json_encode($this->getPostData()); |
||
| 130 | } |
||
| 131 | |||
| 132 | // Initialize the URL. |
||
| 133 | $curlURL = $this->mergeURL(); |
||
| 134 | if (0 < count($this->getQueryData())) { |
||
| 135 | $curlURL = implode("?", [$curlURL, http_build_query($this->getQueryData())]); |
||
| 136 | } |
||
| 137 | |||
| 138 | // Initialize cURL. |
||
| 139 | $stream = curl_init(); |
||
| 140 | |||
| 141 | // Set the connect timeout. |
||
| 142 | if (0 < $this->getConfiguration()->getConnectTimeout()) { |
||
| 143 | curl_setopt($stream, CURLOPT_CONNECTTIMEOUT, $this->getConfiguration()->getConnectTimeout()); |
||
| 144 | } |
||
| 145 | |||
| 146 | // Set the encoding. |
||
| 147 | if (true === $this->getConfiguration()->getAllowEncoding()) { |
||
| 148 | curl_setopt($stream, CURLOPT_ENCODING, ""); |
||
| 149 | } |
||
| 150 | |||
| 151 | // Set the HTTP headers. |
||
| 152 | curl_setopt($stream, CURLOPT_HTTPHEADER, $curlHeaders); |
||
| 153 | |||
| 154 | // Set the post. |
||
| 155 | switch ($this->getMethod()) { |
||
| 156 | |||
| 157 | case self::HTTP_METHOD_DELETE: |
||
| 158 | case self::HTTP_METHOD_OPTIONS: |
||
| 159 | case self::HTTP_METHOD_PATCH: |
||
| 160 | case self::HTTP_METHOD_PUT: |
||
| 161 | curl_setopt($stream, CURLOPT_CUSTOMREQUEST, $this->getMethod()); |
||
| 162 | curl_setopt($stream, CURLOPT_POSTFIELDS, $curlPOSTData); |
||
| 163 | break; |
||
| 164 | |||
| 165 | case self::HTTP_METHOD_HEAD: |
||
| 166 | curl_setopt($stream, CURLOPT_NOBODY, true); |
||
| 167 | break; |
||
| 168 | |||
| 169 | case self::HTTP_METHOD_POST: |
||
| 170 | curl_setopt($stream, CURLOPT_POST, true); |
||
| 171 | curl_setopt($stream, CURLOPT_POSTFIELDS, $curlPOSTData); |
||
| 172 | break; |
||
| 173 | } |
||
| 174 | |||
| 175 | // Set the proxy. |
||
| 176 | if (null !== $this->getConfiguration()->getProxyHost()) { |
||
| 177 | curl_setopt($stream, CURLOPT_PROXY, $this->getConfiguration()->getProxyHost()); |
||
| 178 | } |
||
| 179 | if (null !== $this->getConfiguration()->getProxyPort()) { |
||
| 180 | curl_setopt($stream, CURLOPT_PROXYPORT, $this->getConfiguration()->getProxyPort()); |
||
| 181 | } |
||
| 182 | if (null !== $this->getConfiguration()->getProxyType()) { |
||
| 183 | curl_setopt($stream, CURLOPT_PROXYTYPE, $this->getConfiguration()->getProxyType()); |
||
| 184 | } |
||
| 185 | if (null !== $this->getConfiguration()->getProxyUsername()) { |
||
| 186 | curl_setopt($stream, CURLOPT_PROXYUSERPWD, implode(":", [$this->getConfiguration()->getProxyUsername(), $this->getConfiguration()->getProxyPassword()])); |
||
| 187 | } |
||
| 188 | |||
| 189 | // Set the return. |
||
| 190 | curl_setopt($stream, CURLOPT_RETURNTRANSFER, true); |
||
| 191 | |||
| 192 | // Set the SSL verification. |
||
| 193 | if (false === $this->getConfiguration()->getSslVerification()) { |
||
| 194 | curl_setopt($stream, CURLOPT_SSL_VERIFYHOST, 0); |
||
| 195 | curl_setopt($stream, CURLOPT_SSL_VERIFYPEER, 0); |
||
| 196 | } |
||
| 197 | |||
| 198 | // Set the request timeout. |
||
| 199 | if (0 < $this->getConfiguration()->getRequestTimeout()) { |
||
| 200 | curl_setopt($stream, CURLOPT_TIMEOUT, $this->getConfiguration()->getRequestTimeout()); |
||
| 201 | } |
||
| 202 | |||
| 203 | // Set the URL. |
||
| 204 | curl_setopt($stream, CURLOPT_URL, $curlURL); |
||
| 205 | |||
| 206 | // Set the user agent. |
||
| 207 | curl_setopt($stream, CURLOPT_USERAGENT, $this->getConfiguration()->getUserAgent()); |
||
| 208 | |||
| 209 | // Get the HTTP response headers. |
||
| 210 | curl_setopt($stream, CURLOPT_HEADER, 1); |
||
| 211 | |||
| 212 | // Set the verbose. |
||
| 213 | if (true === $this->getConfiguration()->getDebug()) { |
||
| 214 | curl_setopt($stream, CURLOPT_STDERR, fopen($this->getConfiguration()->getDebugFile(), "a")); |
||
| 215 | curl_setopt($stream, CURLOPT_VERBOSE, 0); |
||
| 216 | |||
| 217 | $msg = (new DateTime())->format("c") . " [DEBUG] " . $curlURL . PHP_EOL . "HTTP request body ~BEGIN~" . PHP_EOL . print_r($curlPOSTData, true) . PHP_EOL . "~END~" . PHP_EOL; |
||
| 218 | error_log($msg, 3, $this->getConfiguration()->getDebugFile()); |
||
| 219 | } else { |
||
| 220 | if (true === $this->getConfiguration()->getVerbose()) { |
||
| 221 | curl_setopt($stream, CURLOPT_VERBOSE, 1); |
||
| 222 | } else { |
||
| 223 | curl_setopt($stream, CURLOPT_VERBOSE, 0); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | // Make the request. |
||
| 228 | $curlExec = curl_exec($stream); |
||
| 229 | $httpHeadSize = curl_getinfo($stream, CURLINFO_HEADER_SIZE); |
||
| 230 | $httpHead = $this->parseheader(substr($curlExec, 0, $httpHeadSize)); |
||
| 231 | $httpBody = substr($curlExec, $httpHeadSize); |
||
| 232 | $curlInfo = curl_getinfo($stream); |
||
| 233 | |||
| 234 | // |
||
| 235 | if (true === $this->getConfiguration()->getDebug()) { |
||
| 236 | $msg = (new DateTime())->format("c") . " [DEBUG] " . $curlURL . PHP_EOL . "HTTP response body ~BEGIN~" . PHP_EOL . print_r($httpBody, true) . PHP_EOL . "~END~" . PHP_EOL; |
||
| 237 | error_log($msg, 3, $this->getConfiguration()->getDebugFile()); |
||
| 238 | } |
||
| 239 | |||
| 240 | // Initialize the response. |
||
| 241 | $response = new CURLResponse(); |
||
| 242 | $response->setRequestBody($curlPOSTData); |
||
| 243 | $response->setRequestHeader($curlHeaders); |
||
| 244 | $response->setRequestURL($curlURL); |
||
| 245 | $response->setResponseBody($httpBody); |
||
| 246 | $response->setResponseHeader($httpHead); |
||
| 247 | $response->setResponseInfo($curlInfo); |
||
| 248 | |||
| 249 | // Check HTTP code. |
||
| 250 | if (200 <= $curlInfo["http_code"] && $curlInfo["http_code"] <= 299) { |
||
| 251 | |||
| 252 | // Return the response. |
||
| 253 | return $response; |
||
| 254 | } |
||
| 255 | |||
| 256 | // Initialize the parameters. |
||
| 257 | $cde = $curlInfo["http_code"]; |
||
| 258 | $msg = curl_errno($stream); |
||
| 259 | |||
| 260 | // Check the HTTP code. |
||
| 261 | if (0 === $curlInfo["http_code"]) { |
||
| 262 | if (false === empty(curl_error($stream))) { |
||
| 263 | $msg = "Call to " . $curlURL . " failed : " . curl_error($stream); |
||
| 264 | } else { |
||
| 265 | $msg = "Call to " . $curlURL . " failed, but for an unknown reason. This could happen if you are disconnected from the network."; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | // Throw the exception. |
||
| 270 | throw new CURLRequestCallException($msg, $cde, $response); |
||
| 271 | } |
||
| 272 | |||
| 518 |