| Total Complexity | 71 |
| Total Lines | 574 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like WebClient 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 WebClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class WebClient extends Client |
||
| 18 | { |
||
| 19 | const MODE = 'web'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Cached responses to avoid multiple request for the same file |
||
| 23 | * |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $cache = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Apache Tika server host |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $host = null; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Apache Tika server port |
||
| 37 | * |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | protected $port = null; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Number of retries on server error |
||
| 44 | * |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | protected $retries = 3; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Default cURL options |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $options = |
||
| 55 | [ |
||
| 56 | CURLINFO_HEADER_OUT => true, |
||
| 57 | CURLOPT_HTTPHEADER => [], |
||
| 58 | CURLOPT_PUT => true, |
||
| 59 | CURLOPT_RETURNTRANSFER => true, |
||
| 60 | CURLOPT_TIMEOUT => 5 |
||
| 61 | ]; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Configure class and test if server is running |
||
| 65 | * |
||
| 66 | * @param string $host |
||
| 67 | * @param int $port |
||
| 68 | * @param array $options |
||
| 69 | * @throws \Exception |
||
| 70 | */ |
||
| 71 | public function __construct($host = null, $port = null, $options = []) |
||
| 72 | { |
||
| 73 | parent::__construct(); |
||
| 74 | |||
| 75 | if(is_string($host) && filter_var($host, FILTER_VALIDATE_URL)) |
||
| 76 | { |
||
| 77 | $this->setUrl($host); |
||
| 78 | } |
||
| 79 | elseif($host) |
||
| 80 | { |
||
| 81 | $this->setHost($host); |
||
| 82 | } |
||
| 83 | |||
| 84 | if(is_numeric($port)) |
||
| 85 | { |
||
| 86 | $this->setPort($port); |
||
| 87 | } |
||
| 88 | |||
| 89 | if(!empty($options)) |
||
| 90 | { |
||
| 91 | $this->setOptions($options); |
||
| 92 | } |
||
| 93 | |||
| 94 | $this->setDownloadRemote(true); |
||
| 95 | |||
| 96 | if(self::$check === true) |
||
| 97 | { |
||
| 98 | $this->check(); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get the base URL |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | public function getUrl() |
||
| 108 | { |
||
| 109 | return sprintf('http://%s:%d', $this->host, $this->port ?: 9998); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Set the host and port using an URL |
||
| 114 | * |
||
| 115 | * @param string $url |
||
| 116 | * @return $this |
||
| 117 | */ |
||
| 118 | public function setUrl($url) |
||
| 119 | { |
||
| 120 | $url = parse_url($url); |
||
| 121 | |||
| 122 | $this->setHost($url['host']); |
||
| 123 | |||
| 124 | if(isset($url['port'])) |
||
| 125 | { |
||
| 126 | $this->setPort($url['port']); |
||
| 127 | } |
||
| 128 | |||
| 129 | return $this; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get the host |
||
| 134 | * |
||
| 135 | * @return null|string |
||
| 136 | */ |
||
| 137 | public function getHost() |
||
| 138 | { |
||
| 139 | return $this->host; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Set the host |
||
| 144 | * |
||
| 145 | * @param string $host |
||
| 146 | * @return $this |
||
| 147 | */ |
||
| 148 | public function setHost($host) |
||
| 149 | { |
||
| 150 | $this->host = $host; |
||
| 151 | |||
| 152 | return $this; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Get the port |
||
| 157 | * |
||
| 158 | * @return null|int |
||
| 159 | */ |
||
| 160 | public function getPort() |
||
| 161 | { |
||
| 162 | return $this->port; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Set the port |
||
| 167 | * |
||
| 168 | * @param int $port |
||
| 169 | * @return $this |
||
| 170 | */ |
||
| 171 | public function setPort($port) |
||
| 172 | { |
||
| 173 | $this->port = $port; |
||
| 174 | |||
| 175 | return $this; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get the number of retries |
||
| 180 | * |
||
| 181 | * @return int |
||
| 182 | */ |
||
| 183 | public function getRetries() |
||
| 184 | { |
||
| 185 | return $this->retries; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Set the number of retries |
||
| 190 | * |
||
| 191 | * @param int $retries |
||
| 192 | * @return $this |
||
| 193 | */ |
||
| 194 | public function setRetries($retries) |
||
| 195 | { |
||
| 196 | $this->retries = $retries; |
||
| 197 | |||
| 198 | return $this; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get all the options |
||
| 203 | * |
||
| 204 | * @return null|array |
||
| 205 | */ |
||
| 206 | public function getOptions() |
||
| 207 | { |
||
| 208 | return $this->options; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get an specified option |
||
| 213 | * |
||
| 214 | * @param string $key |
||
| 215 | * @return mixed |
||
| 216 | */ |
||
| 217 | public function getOption($key) |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Set a cURL option to be set with curl_setopt() |
||
| 224 | * |
||
| 225 | * @link http://php.net/manual/en/curl.constants.php |
||
| 226 | * @link http://php.net/manual/en/function.curl-setopt.php |
||
| 227 | * @param string $key |
||
| 228 | * @param mixed $value |
||
| 229 | * @return $this |
||
| 230 | * @throws \Exception |
||
| 231 | */ |
||
| 232 | public function setOption($key, $value) |
||
| 233 | { |
||
| 234 | if(in_array($key, [CURLINFO_HEADER_OUT, CURLOPT_PUT, CURLOPT_RETURNTRANSFER])) |
||
| 235 | { |
||
| 236 | throw new Exception("Value for cURL option $key cannot be modified", 3); |
||
| 237 | } |
||
| 238 | |||
| 239 | $this->options[$key] = $value; |
||
| 240 | |||
| 241 | return $this; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Set the cURL options |
||
| 246 | * |
||
| 247 | * @param array $options |
||
| 248 | * @return $this |
||
| 249 | * @throws \Exception |
||
| 250 | */ |
||
| 251 | public function setOptions($options) |
||
| 252 | { |
||
| 253 | foreach($options as $key => $value) |
||
| 254 | { |
||
| 255 | $this->setOption($key, $value); |
||
| 256 | } |
||
| 257 | |||
| 258 | return $this; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get the timeout value for cURL |
||
| 263 | * |
||
| 264 | * @return int |
||
| 265 | */ |
||
| 266 | public function getTimeout() |
||
| 267 | { |
||
| 268 | return $this->getOption(CURLOPT_TIMEOUT); |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Set the timeout value for cURL |
||
| 273 | * |
||
| 274 | * @param int $value |
||
| 275 | * @return $this |
||
| 276 | * @throws \Exception |
||
| 277 | */ |
||
| 278 | public function setTimeout($value) |
||
| 279 | { |
||
| 280 | $this->setOption(CURLOPT_TIMEOUT, (int) $value); |
||
| 281 | |||
| 282 | return $this; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Check if server is running |
||
| 287 | * |
||
| 288 | * @throws \Exception |
||
| 289 | */ |
||
| 290 | public function check() |
||
| 291 | { |
||
| 292 | if(self::isChecked() === false) |
||
| 293 | { |
||
| 294 | self::setChecked(true); |
||
| 295 | |||
| 296 | // throws an exception if server is unreachable or can't connect |
||
| 297 | $this->request('version'); |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Configure, make a request and return its results |
||
| 303 | * |
||
| 304 | * @param string $type |
||
| 305 | * @param string $file |
||
| 306 | * @return string |
||
| 307 | * @throws \Exception |
||
| 308 | */ |
||
| 309 | public function request($type, $file = null) |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Make a request to Apache Tika Server |
||
| 388 | * |
||
| 389 | * @param array $options |
||
| 390 | * @return array |
||
| 391 | * @throws \Exception |
||
| 392 | */ |
||
| 393 | protected function exec(array $options = []) |
||
| 394 | { |
||
| 395 | // cURL init and options |
||
| 396 | $curl = curl_init(); |
||
| 397 | |||
| 398 | // we avoid curl_setopt_array($curl, $options) because extrange Windows behaviour (issue #8) |
||
| 399 | foreach($options as $option => $value) |
||
| 400 | { |
||
| 401 | curl_setopt($curl, $option, $value); |
||
| 402 | } |
||
| 403 | |||
| 404 | // make the request |
||
| 405 | if(is_null($this->callback)) |
||
| 406 | { |
||
| 407 | $this->response = curl_exec($curl); |
||
| 408 | } |
||
| 409 | else |
||
| 410 | { |
||
| 411 | $this->response = ''; |
||
| 412 | curl_exec($curl); |
||
| 413 | } |
||
| 414 | |||
| 415 | // exception if cURL fails |
||
| 416 | if(curl_errno($curl)) |
||
| 417 | { |
||
| 418 | throw new Exception(curl_error($curl), curl_errno($curl)); |
||
| 419 | } |
||
| 420 | |||
| 421 | // return the response and the status code |
||
| 422 | return [trim($this->response), curl_getinfo($curl, CURLINFO_HTTP_CODE)]; |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Throws an exception for an error status code |
||
| 427 | * |
||
| 428 | * @codeCoverageIgnore |
||
| 429 | * |
||
| 430 | * @param int $status |
||
| 431 | * @param string $resource |
||
| 432 | * @throws \Exception |
||
| 433 | */ |
||
| 434 | protected function error($status, $resource) |
||
| 435 | { |
||
| 436 | switch($status) |
||
| 437 | { |
||
| 438 | // method not allowed |
||
| 439 | case 405: |
||
| 440 | throw new Exception('Method not allowed', 405); |
||
| 441 | break; |
||
| 442 | |||
| 443 | // unsupported media type |
||
| 444 | case 415: |
||
| 445 | throw new Exception('Unsupported media type', 415); |
||
| 446 | break; |
||
| 447 | |||
| 448 | // unprocessable entity |
||
| 449 | case 422: |
||
| 450 | throw new Exception('Unprocessable document', 422); |
||
| 451 | break; |
||
| 452 | |||
| 453 | // server error |
||
| 454 | case 500: |
||
| 455 | throw new Exception('Error while processing document', 500); |
||
| 456 | break; |
||
| 457 | |||
| 458 | // unexpected |
||
| 459 | default: |
||
| 460 | throw new Exception("Unexpected response for /$resource ($status)", 501); |
||
| 461 | } |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Get the parameters to make the request |
||
| 466 | * |
||
| 467 | * @link https://wiki.apache.org/tika/TikaJAXRS#Specifying_a_URL_Instead_of_Putting_Bytes |
||
| 468 | * @param string $type |
||
| 469 | * @param string $file |
||
| 470 | * @return array |
||
| 471 | * @throws \Exception |
||
| 472 | */ |
||
| 473 | protected function getParameters($type, $file = null) |
||
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Get the cURL options |
||
| 530 | * |
||
| 531 | * @param string $type |
||
| 532 | * @param string $file |
||
| 533 | * @return array |
||
| 534 | * @throws \Exception |
||
| 535 | */ |
||
| 536 | protected function getCurlOptions($type, $file = null) |
||
| 580 | } |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Check if a request type must be cached |
||
| 584 | * |
||
| 585 | * @param string $type |
||
| 586 | * @return bool |
||
| 587 | */ |
||
| 588 | protected function isCacheable($type) |
||
| 593 |