| Total Complexity | 50 |
| Total Lines | 529 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | abstract class Client |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * List of supported Apache Tika versions |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected static $supportedVersions = |
||
| 26 | [ |
||
| 27 | '1.7', '1.8', '1.9', '1.10', '1.11', '1.12', '1.13', '1.14', |
||
| 28 | '1.15', '1.16', '1.17', '1.18', '1.19', '1.19.1', '1.20', '1.21' |
||
| 29 | ]; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Verify JAR or server connection on constructor |
||
| 33 | * |
||
| 34 | * @var bool |
||
| 35 | */ |
||
| 36 | protected static $check = true; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Checked flag |
||
| 40 | * |
||
| 41 | * @var bool |
||
| 42 | */ |
||
| 43 | protected static $checked = false; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Response using callbacks |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $response = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Platform (unix or win) |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $platform = 'unix'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Cached responses to avoid multiple request for the same file. |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $cache = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Callback called on secuential read |
||
| 68 | * |
||
| 69 | * @var \Closure |
||
| 70 | */ |
||
| 71 | protected $callback = null; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Size of chunks for callback |
||
| 75 | * |
||
| 76 | * @var int |
||
| 77 | */ |
||
| 78 | protected $chunkSize = 1048576; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Remote download flag |
||
| 82 | * |
||
| 83 | * @var bool |
||
| 84 | */ |
||
| 85 | protected $downloadRemote = false; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Configure client |
||
| 89 | */ |
||
| 90 | public function __construct() |
||
| 91 | { |
||
| 92 | if(defined('PHP_WINDOWS_VERSION_MAJOR')) |
||
| 93 | { |
||
| 94 | $this->platform = 'win'; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get a class instance throwing an exception if check fails |
||
| 100 | * |
||
| 101 | * @param string $param1 path or host |
||
| 102 | * @param int $param2 Java binary path or port for web client |
||
| 103 | * @param array $options options for cURL request |
||
| 104 | * @return \Vaites\ApacheTika\Clients\CLIClient|\Vaites\ApacheTika\Clients\WebClient |
||
| 105 | * @throws \Exception |
||
| 106 | */ |
||
| 107 | public static function make($param1 = null, $param2 = null, $options = []) |
||
| 108 | { |
||
| 109 | if (preg_match('/\.jar$/', func_get_arg(0))) |
||
| 110 | { |
||
| 111 | return new CLIClient($param1, $param2); |
||
| 112 | } |
||
| 113 | else |
||
| 114 | { |
||
| 115 | return new WebClient($param1, $param2, $options); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Get a class instance delaying the check |
||
| 121 | * |
||
| 122 | * @param string $param1 path or host |
||
| 123 | * @param int $param2 Java binary path or port for web client |
||
| 124 | * @param array $options options for cURL request |
||
| 125 | * @return \Vaites\ApacheTika\Clients\CLIClient|\Vaites\ApacheTika\Clients\WebClient |
||
| 126 | * @throws \Exception |
||
| 127 | */ |
||
| 128 | public static function prepare($param1 = null, $param2 = null, $options = []) |
||
| 129 | { |
||
| 130 | self::$check = false; |
||
| 131 | |||
| 132 | return self::make($param1, $param2, $options); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get the callback |
||
| 137 | * |
||
| 138 | * @return \Closure|null |
||
| 139 | */ |
||
| 140 | public function getCallback() |
||
| 141 | { |
||
| 142 | return $this->callback; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Set the callback (callable or closure) for call on secuential read |
||
| 147 | * |
||
| 148 | * @param mixed $callback |
||
| 149 | * @return $this |
||
| 150 | * @throws \Exception |
||
| 151 | */ |
||
| 152 | public function setCallback($callback) |
||
| 153 | { |
||
| 154 | if($callback instanceof Closure) |
||
| 155 | { |
||
| 156 | $this->callback = $callback; |
||
| 157 | } |
||
| 158 | elseif(is_callable($callback)) |
||
| 159 | { |
||
| 160 | $this->callback = function($chunk) use($callback) |
||
| 161 | { |
||
| 162 | return call_user_func_array($callback, [$chunk]); |
||
| 163 | }; |
||
| 164 | } |
||
| 165 | else |
||
| 166 | { |
||
| 167 | throw new Exception('Invalid callback'); |
||
| 168 | } |
||
| 169 | |||
| 170 | return $this; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get the chunk size |
||
| 175 | * |
||
| 176 | * @return int |
||
| 177 | */ |
||
| 178 | public function getChunkSize() |
||
| 179 | { |
||
| 180 | return $this->chunkSize; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Set the chunk size for secuential read |
||
| 185 | * |
||
| 186 | * @param int $size |
||
| 187 | * @return $this |
||
| 188 | * @throws \Exception |
||
| 189 | */ |
||
| 190 | public function setChunkSize($size) |
||
| 191 | { |
||
| 192 | if(static::MODE == 'cli' && is_numeric($size)) |
||
|
|
|||
| 193 | { |
||
| 194 | $this->chunkSize = (int)$size; |
||
| 195 | } |
||
| 196 | elseif(static::MODE == 'web') |
||
| 197 | { |
||
| 198 | throw new Exception('Chunk size is not supported on web mode'); |
||
| 199 | } |
||
| 200 | else |
||
| 201 | { |
||
| 202 | throw new Exception("$size is not a valid chunk size"); |
||
| 203 | } |
||
| 204 | |||
| 205 | return $this; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get the remote download flag |
||
| 210 | * |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | public function getDownloadRemote() |
||
| 214 | { |
||
| 215 | return $this->downloadRemote; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Set the remote download flag |
||
| 220 | * |
||
| 221 | * @param bool $download |
||
| 222 | * @return $this |
||
| 223 | */ |
||
| 224 | public function setDownloadRemote($download) |
||
| 225 | { |
||
| 226 | $this->downloadRemote = (bool) $download; |
||
| 227 | |||
| 228 | return $this; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Gets file metadata |
||
| 233 | * |
||
| 234 | * @param string $file |
||
| 235 | * @return \Vaites\ApacheTika\Metadata\Metadata |
||
| 236 | * @throws \Exception |
||
| 237 | */ |
||
| 238 | public function getMetadata($file) |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Detect language |
||
| 245 | * |
||
| 246 | * @param string $file |
||
| 247 | * @return string |
||
| 248 | * @throws \Exception |
||
| 249 | */ |
||
| 250 | public function getLanguage($file) |
||
| 251 | { |
||
| 252 | return $this->request('lang', $file); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Detect MIME type |
||
| 257 | * |
||
| 258 | * @param string $file |
||
| 259 | * @return string |
||
| 260 | * @throws \Exception |
||
| 261 | */ |
||
| 262 | public function getMIME($file) |
||
| 263 | { |
||
| 264 | return $this->request('mime', $file); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Extracts HTML |
||
| 269 | * |
||
| 270 | * @param string $file |
||
| 271 | * @param mixed $callback |
||
| 272 | * @return string |
||
| 273 | * @throws \Exception |
||
| 274 | */ |
||
| 275 | public function getHTML($file, $callback = null) |
||
| 276 | { |
||
| 277 | if(!is_null($callback)) |
||
| 278 | { |
||
| 279 | $this->setCallback($callback); |
||
| 280 | } |
||
| 281 | |||
| 282 | return $this->request('html', $file); |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Extracts text |
||
| 287 | * |
||
| 288 | * @param string $file |
||
| 289 | * @param mixed $callback |
||
| 290 | * @return string |
||
| 291 | * @throws \Exception |
||
| 292 | */ |
||
| 293 | public function getText($file, $callback = null) |
||
| 294 | { |
||
| 295 | if(!is_null($callback)) |
||
| 296 | { |
||
| 297 | $this->setCallback($callback); |
||
| 298 | } |
||
| 299 | |||
| 300 | return $this->request('text', $file); |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Extracts main text |
||
| 305 | * |
||
| 306 | * @param string $file |
||
| 307 | * @param mixed $callback |
||
| 308 | * @return string |
||
| 309 | * @throws \Exception |
||
| 310 | */ |
||
| 311 | public function getMainText($file, $callback = null) |
||
| 312 | { |
||
| 313 | if(!is_null($callback)) |
||
| 314 | { |
||
| 315 | $this->setCallback($callback); |
||
| 316 | } |
||
| 317 | |||
| 318 | return $this->request('text-main', $file); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Returns the supported MIME types |
||
| 323 | * |
||
| 324 | * @return string |
||
| 325 | * @throws \Exception |
||
| 326 | */ |
||
| 327 | public function getSupportedMIMETypes() |
||
| 328 | { |
||
| 329 | return $this->request('mime-types'); |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Returns the available detectors |
||
| 334 | * |
||
| 335 | * @return string |
||
| 336 | * @throws \Exception |
||
| 337 | */ |
||
| 338 | public function getAvailableDetectors() |
||
| 339 | { |
||
| 340 | return $this->request('detectors'); |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Returns the available parsers |
||
| 345 | * |
||
| 346 | * @return string |
||
| 347 | * @throws \Exception |
||
| 348 | */ |
||
| 349 | public function getAvailableParsers() |
||
| 350 | { |
||
| 351 | return $this->request('parsers'); |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Returns current Tika version |
||
| 356 | * |
||
| 357 | * @return string |
||
| 358 | * @throws \Exception |
||
| 359 | */ |
||
| 360 | public function getVersion() |
||
| 361 | { |
||
| 362 | return $this->request('version'); |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Return the list of Apache Tika supported versions |
||
| 367 | * |
||
| 368 | * @return array |
||
| 369 | */ |
||
| 370 | public static function getSupportedVersions() |
||
| 371 | { |
||
| 372 | return self::$supportedVersions; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Sets the checked flag |
||
| 377 | * |
||
| 378 | * @param $checked |
||
| 379 | */ |
||
| 380 | public static function setChecked($checked) |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Checks if instance is checked |
||
| 387 | * |
||
| 388 | * @return bool |
||
| 389 | */ |
||
| 390 | public static function isChecked() |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Check if a response is cached |
||
| 397 | * |
||
| 398 | * @param string $type |
||
| 399 | * @param string $file |
||
| 400 | * @return mixed |
||
| 401 | */ |
||
| 402 | protected function isCached($type, $file) |
||
| 403 | { |
||
| 404 | return isset($this->cache[sha1($file)][$type]); |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Get a cached response |
||
| 409 | * |
||
| 410 | * @param string $type |
||
| 411 | * @param string $file |
||
| 412 | * @return mixed |
||
| 413 | */ |
||
| 414 | protected function getCachedResponse($type, $file) |
||
| 415 | { |
||
| 416 | return isset($this->cache[sha1($file)][$type]) ? $this->cache[sha1($file)][$type] : null; |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Check if a request type must be cached |
||
| 421 | * |
||
| 422 | * @param string $type |
||
| 423 | * @return bool |
||
| 424 | */ |
||
| 425 | protected function isCacheable($type) |
||
| 426 | { |
||
| 427 | return in_array($type, ['lang', 'meta']); |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Caches a response |
||
| 432 | * |
||
| 433 | * @param string $type |
||
| 434 | * @param mixed $response |
||
| 435 | * @param string $file |
||
| 436 | * @return bool |
||
| 437 | */ |
||
| 438 | protected function cacheResponse($type, $response, $file) |
||
| 439 | { |
||
| 440 | $this->cache[sha1($file)][$type] = $response; |
||
| 441 | |||
| 442 | return true; |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Checks if a specific version is supported |
||
| 447 | * |
||
| 448 | * @param string $version |
||
| 449 | * @return bool |
||
| 450 | */ |
||
| 451 | public static function isVersionSupported($version) |
||
| 452 | { |
||
| 453 | return in_array($version, self::getSupportedVersions()); |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Check the request before executing |
||
| 458 | * |
||
| 459 | * @param string $type |
||
| 460 | * @param string $file |
||
| 461 | * @return string |
||
| 462 | * @throws \Exception |
||
| 463 | */ |
||
| 464 | public function checkRequest($type, $file) |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Download file to a temporary folder |
||
| 492 | * |
||
| 493 | * @link https://wiki.apache.org/tika/TikaJAXRS#Specifying_a_URL_Instead_of_Putting_Bytes |
||
| 494 | * @param string $file |
||
| 495 | * @return string |
||
| 496 | * @throws \Exception |
||
| 497 | */ |
||
| 498 | protected function downloadFile($file) |
||
| 529 | } |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Check Java binary, JAR path or server connection |
||
| 533 | * |
||
| 534 | * @return void |
||
| 535 | */ |
||
| 536 | abstract public function check(); |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Configure and make a request and return its results. |
||
| 540 | * |
||
| 541 | * @param string $type |
||
| 542 | * @param string $file |
||
| 543 | * @return string |
||
| 544 | * @throws \Exception |
||
| 545 | */ |
||
| 546 | abstract public function request($type, $file); |
||
| 547 | } |
||
| 548 |