| Total Complexity | 65 |
| Total Lines | 533 |
| 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 | if($host && filter_var($host, FILTER_VALIDATE_URL)) |
||
| 74 | { |
||
| 75 | $this->setUrl($host); |
||
| 76 | } |
||
| 77 | elseif($host) |
||
| 78 | { |
||
| 79 | $this->setHost($host); |
||
| 80 | } |
||
| 81 | |||
| 82 | if($port) |
||
|
|
|||
| 83 | { |
||
| 84 | $this->setPort($port); |
||
| 85 | } |
||
| 86 | |||
| 87 | if(!empty($options)) |
||
| 88 | { |
||
| 89 | $this->setOptions($options); |
||
| 90 | } |
||
| 91 | |||
| 92 | $this->setDownloadRemote(true); |
||
| 93 | |||
| 94 | $this->getVersion(); // exception if not running |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Get the base URL |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function getUrl() |
||
| 103 | { |
||
| 104 | return sprintf('http://%s:%d', $this->host, $this->port ?: 9998); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Set the host and port using an URL |
||
| 109 | * |
||
| 110 | * @param string $url |
||
| 111 | * @return $this |
||
| 112 | */ |
||
| 113 | public function setUrl($url) |
||
| 114 | { |
||
| 115 | $url = parse_url($url); |
||
| 116 | |||
| 117 | $this->setHost($url['host']); |
||
| 118 | |||
| 119 | if(!empty($url['port'])) |
||
| 120 | { |
||
| 121 | $this->setPort($url['port']); |
||
| 122 | } |
||
| 123 | |||
| 124 | return $this; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get the host |
||
| 129 | * |
||
| 130 | * @return null|string |
||
| 131 | */ |
||
| 132 | public function getHost() |
||
| 133 | { |
||
| 134 | return $this->host; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Set the host |
||
| 139 | * |
||
| 140 | * @param string $host |
||
| 141 | * @return $this |
||
| 142 | */ |
||
| 143 | public function setHost($host) |
||
| 144 | { |
||
| 145 | $this->host = $host; |
||
| 146 | |||
| 147 | return $this; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Get the port |
||
| 152 | * |
||
| 153 | * @return null|int |
||
| 154 | */ |
||
| 155 | public function getPort() |
||
| 156 | { |
||
| 157 | return $this->port; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Set the port |
||
| 162 | * |
||
| 163 | * @param int $port |
||
| 164 | * @return $this |
||
| 165 | */ |
||
| 166 | public function setPort($port) |
||
| 167 | { |
||
| 168 | $this->port = $port; |
||
| 169 | |||
| 170 | return $this; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get the number of retries |
||
| 175 | * |
||
| 176 | * @return int |
||
| 177 | */ |
||
| 178 | public function getRetries() |
||
| 179 | { |
||
| 180 | return $this->retries; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Set the number of retries |
||
| 185 | * |
||
| 186 | * @param int $retries |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | public function setRetries($retries) |
||
| 190 | { |
||
| 191 | $this->retries = $retries; |
||
| 192 | |||
| 193 | return $this; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get all the options |
||
| 198 | * |
||
| 199 | * @return null|array |
||
| 200 | */ |
||
| 201 | public function getOptions() |
||
| 202 | { |
||
| 203 | return $this->options; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Get an specified option |
||
| 208 | * |
||
| 209 | * @param string $key |
||
| 210 | * @return mixed |
||
| 211 | */ |
||
| 212 | public function getOption($key) |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Set a cURL option to be set with curl_setopt() |
||
| 219 | * |
||
| 220 | * @link http://php.net/manual/en/curl.constants.php |
||
| 221 | * @link http://php.net/manual/en/function.curl-setopt.php |
||
| 222 | * @param string $key |
||
| 223 | * @param mixed $value |
||
| 224 | * @return $this |
||
| 225 | * @throws \Exception |
||
| 226 | */ |
||
| 227 | public function setOption($key, $value) |
||
| 228 | { |
||
| 229 | if(in_array($key, [CURLINFO_HEADER_OUT, CURLOPT_PUT, CURLOPT_RETURNTRANSFER])) |
||
| 230 | { |
||
| 231 | throw new Exception("Value for cURL option $key cannot be modified", 3); |
||
| 232 | } |
||
| 233 | |||
| 234 | $this->options[$key] = $value; |
||
| 235 | |||
| 236 | return $this; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Set the cURL options |
||
| 241 | * |
||
| 242 | * @param array $options |
||
| 243 | * @return $this |
||
| 244 | * @throws \Exception |
||
| 245 | */ |
||
| 246 | public function setOptions($options) |
||
| 247 | { |
||
| 248 | foreach($options as $key => $value) |
||
| 249 | { |
||
| 250 | $this->setOption($key, $value); |
||
| 251 | } |
||
| 252 | |||
| 253 | return $this; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Get the timeout value for cURL |
||
| 258 | * |
||
| 259 | * @return int |
||
| 260 | */ |
||
| 261 | public function getTimeout() |
||
| 262 | { |
||
| 263 | return $this->getOption(CURLOPT_TIMEOUT); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Set the timeout value for cURL |
||
| 268 | * |
||
| 269 | * @param int $value |
||
| 270 | * @return $this |
||
| 271 | * @throws \Exception |
||
| 272 | */ |
||
| 273 | public function setTimeout($value) |
||
| 274 | { |
||
| 275 | $this->setOption(CURLOPT_TIMEOUT, (int) $value); |
||
| 276 | |||
| 277 | return $this; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Configure, make a request and return its results |
||
| 282 | * |
||
| 283 | * @param string $type |
||
| 284 | * @param string $file |
||
| 285 | * @return string |
||
| 286 | * @throws \Exception |
||
| 287 | */ |
||
| 288 | public function request($type, $file = null) |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Make a request to Apache Tika Server |
||
| 358 | * |
||
| 359 | * @param array $options |
||
| 360 | * @return array |
||
| 361 | * @throws \Exception |
||
| 362 | */ |
||
| 363 | protected function exec(array $options = []) |
||
| 364 | { |
||
| 365 | // cURL init and options |
||
| 366 | $curl = curl_init(); |
||
| 367 | |||
| 368 | // we avoid curl_setopt_array($curl, $options) because extrange Windows behaviour (issue #8) |
||
| 369 | foreach($options as $option => $value) |
||
| 370 | { |
||
| 371 | curl_setopt($curl, $option, $value); |
||
| 372 | } |
||
| 373 | |||
| 374 | // make the request |
||
| 375 | if(is_null($this->callback)) |
||
| 376 | { |
||
| 377 | $this->response = curl_exec($curl); |
||
| 378 | } |
||
| 379 | else |
||
| 380 | { |
||
| 381 | $this->response = ''; |
||
| 382 | curl_exec($curl); |
||
| 383 | } |
||
| 384 | |||
| 385 | // exception if cURL fails |
||
| 386 | if(curl_errno($curl)) |
||
| 387 | { |
||
| 388 | throw new Exception(curl_error($curl), curl_errno($curl)); |
||
| 389 | } |
||
| 390 | |||
| 391 | // return the response and the status code |
||
| 392 | return [trim($this->response), curl_getinfo($curl, CURLINFO_HTTP_CODE)]; |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Throws an exception for an error status code |
||
| 397 | * |
||
| 398 | * @codeCoverageIgnore |
||
| 399 | * |
||
| 400 | * @param int $status |
||
| 401 | * @param string $resource |
||
| 402 | * @throws \Exception |
||
| 403 | */ |
||
| 404 | protected function error($status, $resource) |
||
| 405 | { |
||
| 406 | switch($status) |
||
| 407 | { |
||
| 408 | // method not allowed |
||
| 409 | case 405: |
||
| 410 | throw new Exception('Method not allowed', 405); |
||
| 411 | break; |
||
| 412 | |||
| 413 | // unsupported media type |
||
| 414 | case 415: |
||
| 415 | throw new Exception('Unsupported media type', 415); |
||
| 416 | break; |
||
| 417 | |||
| 418 | // unprocessable entity |
||
| 419 | case 422: |
||
| 420 | throw new Exception('Unprocessable document', 422); |
||
| 421 | break; |
||
| 422 | |||
| 423 | // server error |
||
| 424 | case 500: |
||
| 425 | throw new Exception('Error while processing document', 500); |
||
| 426 | break; |
||
| 427 | |||
| 428 | // unexpected |
||
| 429 | default: |
||
| 430 | throw new Exception("Unexpected response for /$resource ($status)", 501); |
||
| 431 | } |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Get the parameters to make the request |
||
| 436 | * |
||
| 437 | * @link https://wiki.apache.org/tika/TikaJAXRS#Specifying_a_URL_Instead_of_Putting_Bytes |
||
| 438 | * @param string $type |
||
| 439 | * @param string file |
||
| 440 | * @return array |
||
| 441 | * @throws \Exception |
||
| 442 | */ |
||
| 443 | protected function getParameters($type, $file = null) |
||
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Get the cURL options |
||
| 500 | * |
||
| 501 | * @param string $type |
||
| 502 | * @param string file |
||
| 503 | * @return array |
||
| 504 | * @throws \Exception |
||
| 505 | */ |
||
| 506 | protected function getCurlOptions($type, $file = null) |
||
| 550 | } |
||
| 551 | } |
||
| 552 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: