| Total Complexity | 75 |
| Total Lines | 472 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 0 | Features | 1 |
Complex classes like CLIClient 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 CLIClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class CLIClient extends Client |
||
| 17 | { |
||
| 18 | protected const MODE = 'cli'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Apache Tika app path |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $path = null; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Java binary path |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $java = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Java arguments |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $javaArgs = null; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Environment variables |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $envVars = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Configure client |
||
| 50 | * |
||
| 51 | * @throws \Exception |
||
| 52 | */ |
||
| 53 | public function __construct(?string $path = null, ?string $java = null, bool $check = true) |
||
| 54 | { |
||
| 55 | parent::__construct(); |
||
| 56 | |||
| 57 | if($path) |
||
| 58 | { |
||
| 59 | $this->setPath($path); |
||
| 60 | } |
||
| 61 | |||
| 62 | if($java) |
||
| 63 | { |
||
| 64 | $this->setJava($java); |
||
| 65 | } |
||
| 66 | |||
| 67 | if($check === true) |
||
| 68 | { |
||
| 69 | $this->check(); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get the path |
||
| 75 | */ |
||
| 76 | public function getPath(): ?string |
||
| 77 | { |
||
| 78 | return $this->path; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Set the path |
||
| 83 | */ |
||
| 84 | public function setPath(string $path): self |
||
| 85 | { |
||
| 86 | $this->path = $path; |
||
| 87 | |||
| 88 | return $this; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get the Java path |
||
| 93 | */ |
||
| 94 | public function getJava(): ?string |
||
| 95 | { |
||
| 96 | return $this->java; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Set the Java path |
||
| 101 | */ |
||
| 102 | public function setJava(string $java): self |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get the Java arguments |
||
| 111 | */ |
||
| 112 | public function getJavaArgs(): ?string |
||
| 113 | { |
||
| 114 | return $this->javaArgs; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Set the Java arguments |
||
| 119 | * |
||
| 120 | * NOTE: to modify child process jvm args, prepend "J" to each argument (-JXmx4g) |
||
| 121 | */ |
||
| 122 | public function setJavaArgs(string $args): self |
||
| 123 | { |
||
| 124 | $this->javaArgs = $args; |
||
| 125 | |||
| 126 | return $this; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get the environment variables |
||
| 131 | */ |
||
| 132 | public function getEnvVars(): array |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Set the environment variables |
||
| 139 | */ |
||
| 140 | public function setEnvVars(array $variables): self |
||
| 141 | { |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns current Tika version |
||
| 149 | * |
||
| 150 | * @throws \Exception |
||
| 151 | */ |
||
| 152 | public function getVersion(): string |
||
| 153 | { |
||
| 154 | $manifest = []; |
||
| 155 | |||
| 156 | if(class_exists(ZipArchive::class) && file_exists($this->path)) |
||
| 157 | { |
||
| 158 | $zip = new ZipArchive(); |
||
| 159 | |||
| 160 | if($zip->open($this->path)) |
||
| 161 | { |
||
| 162 | $content = $zip->getFromName('META-INF/MANIFEST.MF') ?: 'ERROR'; |
||
| 163 | if(preg_match_all('/(.+):\s+(.+)\r?\n/U', $content, $match)) |
||
| 164 | { |
||
| 165 | foreach($match[1] as $index => $key) |
||
| 166 | { |
||
| 167 | $manifest[$key] = $match[2][$index]; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | return $manifest['Implementation-Version'] ?? $this->request('version'); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Returns the supported MIME types |
||
| 178 | * |
||
| 179 | * NOTE: the data provided by the CLI must be parsed: mime type has no spaces, aliases go next prefixed with spaces |
||
| 180 | * |
||
| 181 | * @throws \Exception |
||
| 182 | */ |
||
| 183 | public function getSupportedMIMETypes(): array |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Returns the available detectors |
||
| 218 | * |
||
| 219 | * @throws \Exception |
||
| 220 | */ |
||
| 221 | public function getAvailableDetectors(): array |
||
| 222 | { |
||
| 223 | $detectors = []; |
||
| 224 | |||
| 225 | $split = preg_split("/\n/", $this->request('detectors')) ?: []; |
||
| 226 | |||
| 227 | $parent = null; |
||
| 228 | foreach($split as $line) |
||
| 229 | { |
||
| 230 | if(preg_match('/composite/i', $line)) |
||
| 231 | { |
||
| 232 | $parent = trim(preg_replace('/\(.+\):/', '', $line) ?: ''); |
||
| 233 | $detectors[$parent] = ['children' => [], 'composite' => true, 'name' => $parent]; |
||
| 234 | } |
||
| 235 | else |
||
| 236 | { |
||
| 237 | $child = trim($line); |
||
| 238 | $detectors[$parent]['children'][$child] = ['composite' => false, 'name' => $child]; |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | return $detectors; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Returns the available parsers |
||
| 247 | * |
||
| 248 | * @throws \Exception |
||
| 249 | */ |
||
| 250 | public function getAvailableParsers(): array |
||
| 251 | { |
||
| 252 | $parsers = []; |
||
| 253 | |||
| 254 | $split = preg_split("/\n/", $this->request('parsers')) ?: []; |
||
| 255 | array_shift($split); |
||
| 256 | |||
| 257 | $parent = null; |
||
| 258 | foreach($split as $line) |
||
| 259 | { |
||
| 260 | if(preg_match('/composite/i', $line)) |
||
| 261 | { |
||
| 262 | $parent = trim(preg_replace('/\(.+\):/', '', $line) ?: ''); |
||
| 263 | |||
| 264 | $parsers[$parent] = ['children' => [], 'composite' => true, 'name' => $parent, 'decorated' => false]; |
||
| 265 | } |
||
| 266 | else |
||
| 267 | { |
||
| 268 | $child = trim($line); |
||
| 269 | |||
| 270 | $parsers[$parent]['children'][$child] = ['composite' => false, 'name' => $child, 'decorated' => false]; |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | return $parsers; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Check Java binary, JAR path or server connection |
||
| 279 | * |
||
| 280 | * @throws \Exception |
||
| 281 | */ |
||
| 282 | public function check(): void |
||
| 283 | { |
||
| 284 | if($this->isChecked() === false) |
||
| 285 | { |
||
| 286 | // Java command must not return an error |
||
| 287 | try |
||
| 288 | { |
||
| 289 | $this->exec(($this->java ?: 'java') . ' -version'); |
||
| 290 | } |
||
| 291 | catch(Exception $exception) |
||
| 292 | { |
||
| 293 | throw new Exception('Java command not found'); |
||
| 294 | } |
||
| 295 | |||
| 296 | // JAR path must exists |
||
| 297 | if(file_exists($this->path) === false) |
||
| 298 | { |
||
| 299 | throw new Exception('Apache Tika app JAR not found'); |
||
| 300 | } |
||
| 301 | |||
| 302 | $this->setChecked(true); |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Configure and make a request and return its results |
||
| 308 | * |
||
| 309 | * @throws \Exception |
||
| 310 | */ |
||
| 311 | public function request(string $type, ?string $file = null): string |
||
| 312 | { |
||
| 313 | // check if not checked |
||
| 314 | $this->check(); |
||
| 315 | |||
| 316 | // check if is cached |
||
| 317 | if($file !== null && $this->isCached($type, $file)) |
||
| 318 | { |
||
| 319 | return $this->getCachedResponse($type, $file); |
||
|
|
|||
| 320 | } |
||
| 321 | |||
| 322 | // command arguments |
||
| 323 | $arguments = $this->getArguments($type, $file); |
||
| 324 | |||
| 325 | // check the request |
||
| 326 | $file = $this->checkRequest($type, $file); |
||
| 327 | |||
| 328 | // add last argument |
||
| 329 | if($file) |
||
| 330 | { |
||
| 331 | $arguments[] = escapeshellarg($file); |
||
| 332 | } |
||
| 333 | |||
| 334 | // build command |
||
| 335 | $jar = escapeshellarg($this->path); |
||
| 336 | $command = trim(($this->java ?: 'java') . " -jar $jar " . implode(' ', $arguments) . " {$this->javaArgs}"); |
||
| 337 | |||
| 338 | // run command |
||
| 339 | $response = $this->exec($command); |
||
| 340 | |||
| 341 | // error if command fails |
||
| 342 | if($response === null) |
||
| 343 | { |
||
| 344 | throw new Exception('An error occurred running Java command'); |
||
| 345 | } |
||
| 346 | |||
| 347 | // metadata response |
||
| 348 | if($file !== null && in_array(preg_replace('/\/.+/', '', $type), ['meta', 'rmeta'])) |
||
| 349 | { |
||
| 350 | // fix for invalid? json returned only with images |
||
| 351 | $response = str_replace(basename($file) . '"}{', '", ', $response); |
||
| 352 | |||
| 353 | // on Windows, response must be encoded to UTF8 |
||
| 354 | if(version_compare($this->getVersion(), '2.1.0', '<')) |
||
| 355 | { |
||
| 356 | $response = $this->platform == 'win' ? utf8_encode($response) : $response; |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | // cache certain responses |
||
| 361 | if($file !== null && $this->isCacheable($type)) |
||
| 362 | { |
||
| 363 | $this->cacheResponse($type, $response, $file); |
||
| 364 | } |
||
| 365 | |||
| 366 | return $this->filterResponse($response); |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Run the command and return its results |
||
| 371 | * |
||
| 372 | * @throws \Exception |
||
| 373 | */ |
||
| 374 | public function exec(string $command): ?string |
||
| 375 | { |
||
| 376 | // get env variables for proc_open() |
||
| 377 | $env = empty($this->envVars) ? null : array_merge(getenv(), $this->envVars); |
||
| 378 | |||
| 379 | // run command |
||
| 380 | $exit = -1; |
||
| 381 | $logfile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'tika-error.log'; |
||
| 382 | $descriptors = [['pipe', 'r'], ['pipe', 'w'], ['file', $logfile, 'a']]; |
||
| 383 | $process = proc_open($command, $descriptors, $pipes, null, $env); |
||
| 384 | $callback = $this->callback; |
||
| 385 | |||
| 386 | // get output if command runs ok |
||
| 387 | if(is_resource($process)) |
||
| 388 | { |
||
| 389 | fclose($pipes[0]); |
||
| 390 | $this->response = ''; |
||
| 391 | while($chunk = stream_get_line($pipes[1], $this->chunkSize)) |
||
| 392 | { |
||
| 393 | if(!is_null($callback)) |
||
| 394 | { |
||
| 395 | $callback($chunk); |
||
| 396 | } |
||
| 397 | |||
| 398 | if($this->callbackAppend === true) |
||
| 399 | { |
||
| 400 | $this->response .= $chunk; |
||
| 401 | } |
||
| 402 | } |
||
| 403 | fclose($pipes[1]); |
||
| 404 | $exit = proc_close($process); |
||
| 405 | } |
||
| 406 | |||
| 407 | // exception if exit value is not zero |
||
| 408 | if($exit > 0) |
||
| 409 | { |
||
| 410 | throw new Exception("Unexpected exit value ($exit) for command $command"); |
||
| 411 | } |
||
| 412 | |||
| 413 | return $this->filterResponse($this->response); |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Get the arguments to run the command |
||
| 418 | * |
||
| 419 | * @throws Exception |
||
| 420 | */ |
||
| 421 | protected function getArguments(string $type, ?string $file = null): array |
||
| 488 | } |
||
| 489 | } |
||
| 490 |