Total Complexity | 48 |
Total Lines | 280 |
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 = null) |
||
78 | } |
||
79 | |||
80 | protected function setMethod($method) |
||
81 | { |
||
87 | } |
||
88 | |||
89 | protected function setPostData($postData) |
||
90 | { |
||
91 | if (is_array($postData)) { |
||
92 | $this->postData = []; |
||
93 | foreach ($postData as $key => $value) { |
||
94 | if (is_array($value)) { |
||
95 | throw new \InvalidArgumentException('POST value can not be an array'); |
||
96 | } |
||
97 | $this->postData[$key] = $value; |
||
98 | } |
||
99 | return true; |
||
100 | } |
||
101 | $this->postData = $postData; |
||
102 | return true; |
||
103 | } |
||
104 | |||
105 | protected function debugInit() |
||
106 | { |
||
107 | if ($this->debug) { |
||
108 | ob_start(); |
||
109 | $this->debugStderr = fopen('php://output', 'w'); |
||
110 | return true; |
||
111 | } |
||
112 | return false; |
||
113 | } |
||
114 | |||
115 | protected function debugDo() |
||
116 | { |
||
117 | if ($this->debug) { |
||
118 | //curl_setopt($this->curl, CURLINFO_HEADER_OUT, 1); /* verbose not working if this is enabled */ |
||
119 | curl_setopt($this->curl, CURLOPT_VERBOSE, 1); |
||
120 | curl_setopt($this->curl, CURLOPT_STDERR, $this->debugStderr); |
||
121 | return false; |
||
122 | } |
||
123 | return false; |
||
124 | } |
||
125 | |||
126 | protected function debugFinish() |
||
139 | } |
||
140 | |||
141 | protected function getHttpCode() |
||
142 | { |
||
143 | return isset($this->debugInfo['http_code']) ? $this->debugInfo['http_code']: false; |
||
144 | } |
||
145 | |||
146 | protected function parseRequestHeaders($headers) |
||
147 | { |
||
148 | $data = []; |
||
149 | foreach ($headers as $k => $v) { |
||
150 | if (is_array($v)) { |
||
151 | foreach ($v as $item) { |
||
152 | $data[] = sprintf('%s: %s', $k, $item); |
||
153 | } |
||
154 | } else { |
||
155 | $data[] = sprintf('%s: %s', $k, $v); |
||
156 | } |
||
157 | } |
||
158 | return $data; |
||
159 | } |
||
160 | |||
161 | protected function parseResponseHeaders($headerString) |
||
162 | { |
||
163 | $headers = []; |
||
164 | $lines = explode("\r\n", $headerString); |
||
165 | foreach ($lines as $index => $line) { |
||
166 | if (0 === $index) { |
||
167 | continue; /* we'll get the status code elsewhere */ |
||
168 | } |
||
169 | $parts = explode(': ', $line, 2); |
||
170 | if (!isset($parts[1])) { |
||
171 | continue; // invalid header (missing colon) |
||
172 | } |
||
173 | list($key, $value) = $parts; |
||
174 | if (isset($headers[$key])) { |
||
175 | if (!is_array($headers[$key])) { |
||
176 | $headers[$key] = [$headers[$key]]; |
||
177 | } |
||
178 | // check cookies |
||
179 | if ('Set-Cookie' == $key) { |
||
180 | $parts = explode('=', $value, 2); |
||
181 | $cookieName = $parts[0]; |
||
182 | if (is_array($headers[$key])) { |
||
183 | foreach ($headers[$key] as $cookieIndex => $existingCookie) { |
||
184 | //check if we already have a cookie with the same name |
||
185 | if (0 === mb_stripos($existingCookie, $cookieName)) { |
||
186 | // remove previous cookie with the same name |
||
187 | unset($headers[$key][$cookieIndex]); |
||
188 | } |
||
189 | } |
||
190 | } |
||
191 | } |
||
192 | $headers[$key][] = $value; |
||
193 | $headers[$key] = array_values((array) $headers[$key]); // re-index array |
||
194 | } else { |
||
195 | $headers[$key] = $value; |
||
196 | } |
||
197 | } |
||
198 | return $headers; |
||
199 | } |
||
200 | |||
201 | protected function retrieve($url) |
||
287 | ); |
||
288 | } |
||
289 | } |
||
290 |