Complex classes like HttpClient 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 HttpClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class HttpClient extends ClientSocket implements HttpStream |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Path to file on server (excluding endpoint address) |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | private $path; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Headers |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | private $headers; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The payload |
||
| 40 | * |
||
| 41 | * @var MemoryStream |
||
| 42 | */ |
||
| 43 | private $payload; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The HTTP protocol version |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | private $protocol; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Whether to use https instead of http |
||
| 54 | * |
||
| 55 | * @var boolean |
||
| 56 | */ |
||
| 57 | private $secure; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The response status code |
||
| 61 | * |
||
| 62 | * @var int |
||
| 63 | */ |
||
| 64 | private $responseCode; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * When the connection times out (in seconds) |
||
| 68 | * |
||
| 69 | * @var int |
||
| 70 | */ |
||
| 71 | private $timeout; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Create a new http client |
||
| 75 | * |
||
| 76 | * @param Url $url |
||
| 77 | * The url for http request |
||
| 78 | * @param string $proto |
||
| 79 | * The protocol to use (default = HTTP/1.1) |
||
| 80 | * @param integer $timeout |
||
| 81 | * Optional timeout for request (default = 10 seconds) |
||
| 82 | */ |
||
| 83 | 8 | public function __construct(Url $url, $proto = 'HTTP/1.1', $timeout = 10) |
|
| 84 | { |
||
| 85 | 8 | parent::__construct($url); |
|
| 86 | 8 | $this->path = $url->getPath(); |
|
| 87 | 8 | $this->secure = $url->getScheme() == 'https'; |
|
| 88 | 8 | $this->protocol = $proto; |
|
| 89 | 8 | $this->headers = array(); |
|
| 90 | 8 | $this->payload = new MemoryStream(); |
|
| 91 | 8 | $this->timeout = $timeout; |
|
| 92 | 8 | } |
|
| 93 | |||
| 94 | /** |
||
| 95 | * |
||
| 96 | * {@inheritdoc} |
||
| 97 | * @see \Generics\Streams\HttpStream::getHeaders() |
||
| 98 | */ |
||
| 99 | 2 | public function getHeaders() |
|
| 100 | { |
||
| 101 | 2 | return $this->headers; |
|
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * |
||
| 106 | * {@inheritdoc} |
||
| 107 | * @see \Generics\Streams\HttpStream::setHeader() |
||
| 108 | * @return HttpClient |
||
| 109 | */ |
||
| 110 | 7 | public function setHeader($headerName, $headerValue) |
|
| 111 | { |
||
| 112 | 7 | $this->headers[$headerName] = $headerValue; |
|
| 113 | 7 | return $this; |
|
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Reset the headers |
||
| 118 | */ |
||
| 119 | 1 | public function resetHeaders() |
|
| 120 | { |
||
| 121 | 1 | $this->headers = array(); |
|
| 122 | 1 | } |
|
| 123 | |||
| 124 | /** |
||
| 125 | * |
||
| 126 | * {@inheritdoc} |
||
| 127 | * @see \Generics\Streams\HttpStream::appendPayload() |
||
| 128 | */ |
||
| 129 | 1 | public function appendPayload(InputStream $payload) |
|
| 130 | { |
||
| 131 | 1 | while ($payload->ready()) { |
|
| 132 | 1 | $this->payload->write($payload->read(1024)); |
|
| 133 | } |
||
| 134 | 1 | } |
|
| 135 | |||
| 136 | /** |
||
| 137 | * |
||
| 138 | * {@inheritdoc} |
||
| 139 | * @see \Generics\Streams\HttpStream::getPayload() |
||
| 140 | */ |
||
| 141 | 1 | public function getPayload() |
|
| 142 | { |
||
| 143 | 1 | return $this->payload; |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Load headers from remote and return it |
||
| 148 | * |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | 1 | public function retrieveHeaders() |
|
| 152 | { |
||
| 153 | 1 | $this->setHeader('Connection', 'close'); |
|
| 154 | 1 | $this->setHeader('Accept', ''); |
|
| 155 | 1 | $this->setHeader('Accept-Language', ''); |
|
| 156 | 1 | $this->setHeader('User-Agent', ''); |
|
| 157 | |||
| 158 | 1 | $savedProto = $this->protocol; |
|
| 159 | 1 | $this->protocol = 'HTTP/1.0'; |
|
| 160 | 1 | $this->request('HEAD'); |
|
| 161 | 1 | $this->protocol = $savedProto; |
|
| 162 | |||
| 163 | 1 | return $this->headers; |
|
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Set connection timeout in seconds |
||
| 168 | * |
||
| 169 | * @param int $timeout |
||
| 170 | */ |
||
| 171 | 5 | public function setTimeout($timeout) |
|
| 172 | { |
||
| 173 | 5 | $timeout = intval($timeout); |
|
| 174 | 5 | if ($timeout < 1 || $timeout > 60) { |
|
| 175 | 1 | $timeout = 5; |
|
| 176 | } |
||
| 177 | 5 | $this->timeout = $timeout; |
|
| 178 | 5 | } |
|
| 179 | |||
| 180 | /** |
||
| 181 | * |
||
| 182 | * {@inheritdoc} |
||
| 183 | * @see \Generics\Streams\HttpStream::request() |
||
| 184 | */ |
||
| 185 | 8 | public function request($requestType) |
|
| 186 | { |
||
| 187 | 8 | if ($this->secure) { |
|
| 188 | 1 | throw new HttpException("Secure connection using HTTPs is not supported yet!"); |
|
| 189 | } |
||
| 190 | |||
| 191 | 7 | if ($requestType == 'HEAD') { |
|
| 192 | 2 | $this->setTimeout(1); // Don't wait too long on simple head |
|
| 193 | } |
||
| 194 | |||
| 195 | 7 | $ms = $this->prepareRequest($requestType); |
|
| 196 | |||
| 197 | 7 | $ms = $this->appendPayloadToRequest($ms); |
|
| 198 | |||
| 199 | 7 | if (! $this->isConnected()) { |
|
| 200 | 6 | $this->connect(); |
|
| 201 | } |
||
| 202 | |||
| 203 | 7 | while ($ms->ready()) { |
|
| 204 | 7 | $this->write($ms->read(1024)); |
|
| 205 | } |
||
| 206 | |||
| 207 | 7 | $this->retrieveAndParseResponse($requestType); |
|
| 208 | |||
| 209 | 6 | if ($this->headers['Connection'] == 'close') { |
|
| 210 | 3 | $this->disconnect(); |
|
| 211 | } |
||
| 212 | 6 | } |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Check the connection availability |
||
| 216 | * |
||
| 217 | * @param int $start |
||
| 218 | * Timestamp when read request attempt starts |
||
| 219 | * @throws HttpException |
||
| 220 | * @return boolean |
||
| 221 | */ |
||
| 222 | 7 | private function checkConnection($start) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Adjust number of bytes to read according content length header |
||
| 238 | * |
||
| 239 | * @param int $numBytes |
||
| 240 | * @return int |
||
| 241 | */ |
||
| 242 | 6 | private function adjustNumbytes($numBytes) |
|
| 243 | { |
||
| 244 | 6 | if (isset($this->headers['Content-Length'])) { |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Try to parse line as header and add the results to local header list |
||
| 254 | * |
||
| 255 | * @param string $line |
||
| 256 | */ |
||
| 257 | 6 | private function addParsedHeader($line) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Check whether the readen bytes amount has reached the |
||
| 270 | * content length amount |
||
| 271 | * |
||
| 272 | * @return boolean |
||
| 273 | */ |
||
| 274 | 4 | private function checkContentLengthExceeded() |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Handle a header line |
||
| 286 | * |
||
| 287 | * All parameters by reference, which means the the values can be |
||
| 288 | * modified during execution of this method. |
||
| 289 | * |
||
| 290 | * @param boolean $delimiterFound |
||
| 291 | * Whether the delimiter for end of header section was found |
||
| 292 | * @param int $numBytes |
||
| 293 | * The number of bytes to read from remote |
||
| 294 | * @param string $tmp |
||
| 295 | * The current readen line |
||
| 296 | */ |
||
| 297 | 6 | private function handleHeader(&$delimiterFound, &$numBytes, &$tmp) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Retrieve and parse the response |
||
| 314 | * |
||
| 315 | * @param string $requestType |
||
| 316 | * @throws \Generics\Client\HttpException |
||
| 317 | * @throws \Generics\Socket\SocketException |
||
| 318 | * @throws \Generics\Streams\StreamException |
||
| 319 | */ |
||
| 320 | 7 | private function retrieveAndParseResponse($requestType) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Append the payload buffer to the request buffer |
||
| 370 | * |
||
| 371 | * @param MemoryStream $ms |
||
| 372 | * @return MemoryStream |
||
| 373 | * @throws \Generics\Streams\StreamException |
||
| 374 | * @throws \Generics\ResetException |
||
| 375 | */ |
||
| 376 | 7 | private function appendPayloadToRequest(MemoryStream $ms) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Prepare the request buffer |
||
| 391 | * |
||
| 392 | * @param string $requestType |
||
| 393 | * @return \Generics\Streams\MemoryStream |
||
| 394 | * @throws \Generics\Streams\StreamException |
||
| 395 | */ |
||
| 396 | 7 | private function prepareRequest($requestType) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Depending on request type the connection header is either |
||
| 432 | * set to keep-alive or close |
||
| 433 | * |
||
| 434 | * @param string $requestType |
||
| 435 | */ |
||
| 436 | 5 | private function adjustConnectionHeader($requestType) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Adjust the headers by injecting default values for missing keys. |
||
| 447 | */ |
||
| 448 | 7 | private function adjustHeaders($requestType) |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Retrieve the response status code |
||
| 469 | * |
||
| 470 | * @return int |
||
| 471 | */ |
||
| 472 | 5 | public function getResponseCode() |
|
| 476 | |||
| 477 | /** |
||
| 478 | * |
||
| 479 | * {@inheritdoc} |
||
| 480 | * @see \Generics\Streams\Stream::isOpen() |
||
| 481 | */ |
||
| 482 | public function isOpen() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * |
||
| 489 | * {@inheritdoc} |
||
| 490 | * @see \Generics\Resettable::reset() |
||
| 491 | */ |
||
| 492 | public function reset() |
||
| 496 | } |
||
| 497 |