| Total Complexity | 47 |
| Total Lines | 346 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like JHttpTransportCurl often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use JHttpTransportCurl, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class JHttpTransportCurl implements JHttpTransport |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var Registry The client options. |
||
| 23 | * @since 11.3 |
||
| 24 | */ |
||
| 25 | protected $options; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Constructor. CURLOPT_FOLLOWLOCATION must be disabled when open_basedir or safe_mode are enabled. |
||
| 29 | * |
||
| 30 | * @param Registry $options Client options object. |
||
| 31 | * |
||
| 32 | * @see https://secure.php.net/manual/en/function.curl-setopt.php |
||
| 33 | * @since 11.3 |
||
| 34 | * @throws RuntimeException |
||
| 35 | */ |
||
| 36 | public function __construct(Registry $options) |
||
| 37 | { |
||
| 38 | if (!function_exists('curl_init') || !is_callable('curl_init')) |
||
| 39 | { |
||
| 40 | throw new RuntimeException('Cannot use a cURL transport when curl_init() is not available.'); |
||
| 41 | } |
||
| 42 | |||
| 43 | $this->options = $options; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Send a request to the server and return a JHttpResponse object with the response. |
||
| 48 | * |
||
| 49 | * @param string $method The HTTP method for sending the request. |
||
| 50 | * @param JUri $uri The URI to the resource to request. |
||
| 51 | * @param mixed $data Either an associative array or a string to be sent with the request. |
||
| 52 | * @param array $headers An array of request headers to send with the request. |
||
| 53 | * @param integer $timeout Read timeout in seconds. |
||
| 54 | * @param string $userAgent The optional user agent string to send with the request. |
||
| 55 | * |
||
| 56 | * @return JHttpResponse |
||
| 57 | * |
||
| 58 | * @since 11.3 |
||
| 59 | * @throws RuntimeException |
||
| 60 | */ |
||
| 61 | public function request($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null) |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Method to get a response object from a server response. |
||
| 240 | * |
||
| 241 | * @param string $content The complete server response, including headers |
||
| 242 | * as a string if the response has no errors. |
||
| 243 | * @param array $info The cURL request information. |
||
| 244 | * |
||
| 245 | * @return JHttpResponse |
||
| 246 | * |
||
| 247 | * @since 11.3 |
||
| 248 | * @throws UnexpectedValueException |
||
| 249 | */ |
||
| 250 | protected function getResponse($content, $info) |
||
| 251 | { |
||
| 252 | // Create the response object. |
||
| 253 | $return = new JHttpResponse; |
||
| 254 | |||
| 255 | // Try to get header size |
||
| 256 | if (isset($info['header_size'])) |
||
| 257 | { |
||
| 258 | $headerString = trim(substr($content, 0, $info['header_size'])); |
||
| 259 | $headerArray = explode("\r\n\r\n", $headerString); |
||
| 260 | |||
| 261 | // Get the last set of response headers as an array. |
||
| 262 | $headers = explode("\r\n", array_pop($headerArray)); |
||
| 263 | |||
| 264 | // Set the body for the response. |
||
| 265 | $return->body = substr($content, $info['header_size']); |
||
| 266 | } |
||
| 267 | // Fallback and try to guess header count by redirect count |
||
| 268 | else |
||
| 269 | { |
||
| 270 | // Get the number of redirects that occurred. |
||
| 271 | $redirects = isset($info['redirect_count']) ? $info['redirect_count'] : 0; |
||
| 272 | |||
| 273 | /* |
||
| 274 | * Split the response into headers and body. If cURL encountered redirects, the headers for the redirected requests will |
||
| 275 | * also be included. So we split the response into header + body + the number of redirects and only use the last two |
||
| 276 | * sections which should be the last set of headers and the actual body. |
||
| 277 | */ |
||
| 278 | $response = explode("\r\n\r\n", $content, 2 + $redirects); |
||
| 279 | |||
| 280 | // Set the body for the response. |
||
| 281 | $return->body = array_pop($response); |
||
| 282 | |||
| 283 | // Get the last set of response headers as an array. |
||
| 284 | $headers = explode("\r\n", array_pop($response)); |
||
| 285 | } |
||
| 286 | |||
| 287 | // Get the response code from the first offset of the response headers. |
||
| 288 | preg_match('/[0-9]{3}/', array_shift($headers), $matches); |
||
| 289 | |||
| 290 | $code = count($matches) ? $matches[0] : null; |
||
| 291 | |||
| 292 | if (is_numeric($code)) |
||
| 293 | { |
||
| 294 | $return->code = (int) $code; |
||
| 295 | } |
||
| 296 | |||
| 297 | // No valid response code was detected. |
||
| 298 | else |
||
| 299 | { |
||
| 300 | throw new UnexpectedValueException('No HTTP response code found.'); |
||
| 301 | } |
||
| 302 | |||
| 303 | // Add the response headers to the response object. |
||
| 304 | foreach ($headers as $header) |
||
| 305 | { |
||
| 306 | $pos = strpos($header, ':'); |
||
| 307 | $return->headers[trim(substr($header, 0, $pos))] = trim(substr($header, ($pos + 1))); |
||
| 308 | } |
||
| 309 | |||
| 310 | return $return; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Method to check if HTTP transport cURL is available for use |
||
| 315 | * |
||
| 316 | * @return boolean true if available, else false |
||
| 317 | * |
||
| 318 | * @since 12.1 |
||
| 319 | */ |
||
| 320 | public static function isSupported() |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Check if redirects are allowed |
||
| 327 | * |
||
| 328 | * @return boolean |
||
| 329 | * |
||
| 330 | * @since 12.1 |
||
| 331 | */ |
||
| 332 | private function redirectsAllowed() |
||
| 367 |