| Total Complexity | 46 | 
| Total Lines | 270 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like CurlBrowser 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 CurlBrowser, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 7 | final class CurlBrowser implements | ||
| 8 | \WebServCo\Framework\Interfaces\HttpBrowserInterface | ||
| 9 | { | ||
| 10 | protected $debug; | ||
| 11 | protected $skipSslVerification; | ||
| 12 | protected $requestHeaders; | ||
| 13 | |||
| 14 | protected $method; | ||
| 15 | protected $postData; | ||
| 16 | |||
| 17 | protected $curl; | ||
| 18 | protected $debugStderr; | ||
| 19 | protected $debugOutput; | ||
| 20 | protected $debugInfo; | ||
| 21 | protected $response; | ||
| 22 | protected $responseHeaders; | ||
| 23 | |||
| 24 | protected $logger; | ||
| 25 | |||
| 26 | protected $curlError; | ||
| 27 | |||
| 28 | public function __construct(\WebServCo\Framework\Interfaces\LoggerInterface $logger) | ||
| 29 |     { | ||
| 30 | $this->logger = $logger; | ||
| 31 | $this->debug = false; | ||
| 32 | $this->skipSslVerification = false; | ||
| 33 | $this->requestHeaders = []; | ||
| 34 | } | ||
| 35 | |||
| 36 | public function setDebug(bool $debug) | ||
| 37 |     { | ||
| 38 | $this->debug = $debug; | ||
| 39 | } | ||
| 40 | |||
| 41 | public function setSkipSSlVerification(bool $skipSslVerification) | ||
| 44 | } | ||
| 45 | |||
| 46 | public function setRequestHeader($name, $value) | ||
| 49 | } | ||
| 50 | |||
| 51 | public function getRequestHeaders() | ||
| 52 |     { | ||
| 53 | return $this->requestHeaders; | ||
| 54 | } | ||
| 55 | |||
| 56 | public function getResponseHeaders() | ||
| 57 |     { | ||
| 58 | return $this->responseHeaders; | ||
| 59 | } | ||
| 60 | |||
| 61 | public function get($url) | ||
| 62 |     { | ||
| 63 | $this->setMethod(Http::METHOD_GET); | ||
| 64 | return $this->retrieve($url); | ||
| 65 | } | ||
| 66 | |||
| 67 | public function head($url) | ||
| 68 |     { | ||
| 69 | $this->setMethod(Http::METHOD_HEAD); | ||
| 70 | return $this->retrieve($url); | ||
| 71 | } | ||
| 72 | |||
| 73 | public function post($url, $postData = []) | ||
| 74 |     { | ||
| 75 | $this->setMethod(Http::METHOD_POST); | ||
| 76 | $this->setPostData($postData); | ||
| 77 | return $this->retrieve($url); | ||
| 78 | } | ||
| 79 | |||
| 80 | protected function setMethod($method) | ||
| 81 |     { | ||
| 82 |         if (!in_array($method, Http::getMethods())) { | ||
| 83 |             throw new ApplicationException('Unsupported method'); | ||
| 84 | } | ||
| 85 | $this->method = $method; | ||
| 86 | return true; | ||
| 87 | } | ||
| 88 | |||
| 89 | protected function setPostData(array $postData) | ||
| 90 |     { | ||
| 91 |         foreach ($postData as $key => $value) { | ||
| 92 |             if (is_array($value)) { | ||
| 93 |                 throw new \InvalidArgumentException('POST value can not be an array'); | ||
| 94 | } | ||
| 95 | $this->postData[$key] = $value; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | protected function debugInit() | ||
| 100 |     { | ||
| 101 |         if ($this->debug) { | ||
| 102 | ob_start(); | ||
| 103 |             $this->debugStderr = fopen('php://output', 'w'); | ||
| 104 | return true; | ||
| 105 | } | ||
| 106 | return false; | ||
| 107 | } | ||
| 108 | |||
| 109 | protected function debugDo() | ||
| 110 |     { | ||
| 111 |         if ($this->debug) { | ||
| 112 | //curl_setopt($this->curl, CURLINFO_HEADER_OUT, 1); /* verbose not working if this is enabled */ | ||
| 113 | curl_setopt($this->curl, CURLOPT_VERBOSE, 1); | ||
| 114 | curl_setopt($this->curl, CURLOPT_STDERR, $this->debugStderr); | ||
| 115 | return false; | ||
| 116 | } | ||
| 117 | return false; | ||
| 118 | } | ||
| 119 | |||
| 120 | protected function debugFinish() | ||
| 133 | } | ||
| 134 | |||
| 135 | protected function getHttpCode() | ||
| 136 |     { | ||
| 137 | return isset($this->debugInfo['http_code']) ? $this->debugInfo['http_code']: false; | ||
| 138 | } | ||
| 139 | |||
| 140 | protected function parseRequestHeaders($headers) | ||
| 141 |     { | ||
| 142 | $data = []; | ||
| 143 |         foreach ($headers as $k => $v) { | ||
| 144 |             if (is_array($v)) { | ||
| 145 |                 foreach ($v as $item) { | ||
| 146 |                     $data[] = sprintf('%s: %s', $k, $item); | ||
| 147 | } | ||
| 148 |             } else { | ||
| 149 |                 $data[] = sprintf('%s: %s', $k, $v); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | return $data; | ||
| 153 | } | ||
| 154 | |||
| 155 | protected function parseResponseHeaders($headerString) | ||
| 156 |     { | ||
| 157 | $headers = []; | ||
| 158 |         $lines = explode("\r\n", $headerString); | ||
| 159 |         foreach ($lines as $index => $line) { | ||
| 160 |             if (0 === $index) { | ||
| 161 | continue; /* we'll get the status code elsewhere */ | ||
| 162 | } | ||
| 163 |             $parts = explode(': ', $line, 2); | ||
| 164 |             if (!isset($parts[1])) { | ||
| 165 | continue; // invalid header (missing colon) | ||
| 166 | } | ||
| 167 | list($key, $value) = $parts; | ||
| 168 |             if (isset($headers[$key])) { | ||
| 169 |                 if (!is_array($headers[$key])) { | ||
| 170 | $headers[$key] = [$headers[$key]]; | ||
| 171 | } | ||
| 172 | // check cookies | ||
| 173 |                 if ('Set-Cookie' == $key) { | ||
| 174 |                     $parts = explode('=', $value, 2); | ||
| 175 | $cookieName = $parts[0]; | ||
| 176 |                     if (is_array($headers[$key])) { | ||
| 177 |                         foreach ($headers[$key] as $cookieIndex => $existingCookie) { | ||
| 178 | //check if we already have a cookie with the same name | ||
| 179 |                             if (0 === mb_stripos($existingCookie, $cookieName)) { | ||
| 180 | // remove previous cookie with the same name | ||
| 181 | unset($headers[$key][$cookieIndex]); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | } | ||
| 185 | } | ||
| 186 | $headers[$key][] = $value; | ||
| 187 | $headers[$key] = array_values((array) $headers[$key]); // re-index array | ||
| 188 |             } else { | ||
| 189 | $headers[$key] = $value; | ||
| 190 | } | ||
| 191 | } | ||
| 192 | return $headers; | ||
| 193 | } | ||
| 194 | |||
| 195 | protected function retrieve($url) | ||
| 277 | ); | ||
| 278 | } | ||
| 279 | } | ||
| 280 |