| Total Complexity | 53 |
| Total Lines | 375 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| 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 |
||
| 15 | class CLIClient extends Client |
||
| 16 | { |
||
| 17 | protected const MODE = 'cli'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Apache Tika app path |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $path = null; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Java binary path |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $java = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Configure client |
||
| 35 | * |
||
| 36 | * @throws \Exception |
||
| 37 | */ |
||
| 38 | public function __construct(string $path = null, string $java = null, bool $check = true) |
||
| 39 | { |
||
| 40 | parent::__construct(); |
||
| 41 | |||
| 42 | if($path) |
||
| 43 | { |
||
| 44 | $this->setPath($path); |
||
| 45 | } |
||
| 46 | |||
| 47 | if($java) |
||
| 48 | { |
||
| 49 | $this->setJava($java); |
||
| 50 | } |
||
| 51 | |||
| 52 | if($check === true) |
||
| 53 | { |
||
| 54 | $this->check(); |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Get the path |
||
| 60 | */ |
||
| 61 | public function getPath(): ?string |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Set the path |
||
| 68 | */ |
||
| 69 | public function setPath($path): self |
||
| 70 | { |
||
| 71 | $this->path = $path; |
||
| 72 | |||
| 73 | return $this; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Get the Java path |
||
| 78 | */ |
||
| 79 | public function getJava(): ?string |
||
| 80 | { |
||
| 81 | return $this->java; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Set the Java path |
||
| 86 | */ |
||
| 87 | public function setJava($java): self |
||
| 88 | { |
||
| 89 | $this->java = $java; |
||
| 90 | |||
| 91 | return $this; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Returns the supported MIME types |
||
| 96 | * |
||
| 97 | * NOTE: the data provided by the CLI must be parsed: mime type has no spaces, aliases go next prefixed with spaces |
||
| 98 | * |
||
| 99 | * @throws \Exception |
||
| 100 | */ |
||
| 101 | public function getSupportedMIMETypes(): array |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Returns the available detectors |
||
| 136 | * |
||
| 137 | * @throws \Exception |
||
| 138 | */ |
||
| 139 | public function getAvailableDetectors(): array |
||
| 140 | { |
||
| 141 | $detectors = []; |
||
| 142 | |||
| 143 | $split = preg_split("/\n/", $this->request('detectors')); |
||
| 144 | |||
| 145 | $parent = null; |
||
| 146 | foreach($split as $line) |
||
| 147 | { |
||
| 148 | if(preg_match('/composite/i', $line)) |
||
| 149 | { |
||
| 150 | $parent = trim(preg_replace('/\(.+\):/', '', $line)); |
||
| 151 | $detectors[$parent] = ['children' => [], 'composite' => true, 'name' => $parent]; |
||
| 152 | } |
||
| 153 | else |
||
| 154 | { |
||
| 155 | $child = trim($line); |
||
| 156 | $detectors[$parent]['children'][$child] = ['composite' => false, 'name' => $child]; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | return $detectors; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Returns the available parsers |
||
| 165 | * |
||
| 166 | * @throws \Exception |
||
| 167 | */ |
||
| 168 | public function getAvailableParsers(): array |
||
| 169 | { |
||
| 170 | $parsers = []; |
||
| 171 | |||
| 172 | $split = preg_split("/\n/", $this->request('parsers')); |
||
| 173 | array_shift($split); |
||
|
|
|||
| 174 | |||
| 175 | $parent = null; |
||
| 176 | foreach($split as $line) |
||
| 177 | { |
||
| 178 | if(preg_match('/composite/i', $line)) |
||
| 179 | { |
||
| 180 | $parent = trim(preg_replace('/\(.+\):/', '', $line)); |
||
| 181 | |||
| 182 | $parsers[$parent] = ['children' => [], 'composite' => true, 'name' => $parent, 'decorated' => false]; |
||
| 183 | } |
||
| 184 | else |
||
| 185 | { |
||
| 186 | $child = trim($line); |
||
| 187 | |||
| 188 | $parsers[$parent]['children'][$child] = ['composite' => false, 'name' => $child, 'decorated' => false]; |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | return $parsers; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Check Java binary, JAR path or server connection |
||
| 197 | * |
||
| 198 | * @throws \Exception |
||
| 199 | */ |
||
| 200 | public function check(): void |
||
| 201 | { |
||
| 202 | if($this->isChecked() === false) |
||
| 203 | { |
||
| 204 | // Java command must not return an error |
||
| 205 | try |
||
| 206 | { |
||
| 207 | $this->exec(($this->java ?: 'java') . ' -version'); |
||
| 208 | } |
||
| 209 | catch(Exception $exception) |
||
| 210 | { |
||
| 211 | throw new Exception('Java command not found'); |
||
| 212 | } |
||
| 213 | |||
| 214 | // JAR path must exists |
||
| 215 | if(file_exists($this->path) === false) |
||
| 216 | { |
||
| 217 | throw new Exception('Apache Tika app JAR not found'); |
||
| 218 | } |
||
| 219 | |||
| 220 | $this->setChecked(true); |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Configure and make a request and return its results |
||
| 226 | * |
||
| 227 | * @throws \Exception |
||
| 228 | */ |
||
| 229 | public function request(string $type, string $file = null): string |
||
| 230 | { |
||
| 231 | // check if not checked |
||
| 232 | $this->check(); |
||
| 233 | |||
| 234 | // check if is cached |
||
| 235 | if($file !== null && $this->isCached($type, $file)) |
||
| 236 | { |
||
| 237 | return $this->getCachedResponse($type, $file); |
||
| 238 | } |
||
| 239 | |||
| 240 | // command arguments |
||
| 241 | $arguments = $this->getArguments($type, $file); |
||
| 242 | |||
| 243 | // check the request |
||
| 244 | $file = $this->checkRequest($type, $file); |
||
| 245 | |||
| 246 | // add last argument |
||
| 247 | if($file) |
||
| 248 | { |
||
| 249 | $arguments[] = escapeshellarg($file); |
||
| 250 | } |
||
| 251 | |||
| 252 | // build command |
||
| 253 | $jar = escapeshellarg($this->path); |
||
| 254 | $command = ($this->java ?: 'java') . " -jar $jar " . implode(' ', $arguments); |
||
| 255 | |||
| 256 | // run command |
||
| 257 | $response = $this->exec($command); |
||
| 258 | |||
| 259 | // metadata response |
||
| 260 | if(in_array(preg_replace('/\/.+/', '', $type), ['meta', 'rmeta'])) |
||
| 261 | { |
||
| 262 | // fix for invalid? json returned only with images |
||
| 263 | $response = str_replace(basename($file) . '"}{', '", ', $response); |
||
| 264 | |||
| 265 | // on Windows, response must be encoded to UTF8 |
||
| 266 | $response = $this->platform == 'win' ? utf8_encode($response) : $response; |
||
| 267 | } |
||
| 268 | |||
| 269 | // cache certain responses |
||
| 270 | if($this->isCacheable($type)) |
||
| 271 | { |
||
| 272 | $this->cacheResponse($type, $response, $file); |
||
| 273 | } |
||
| 274 | |||
| 275 | return $response; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Run the command and return its results |
||
| 280 | * |
||
| 281 | * @throws \Exception |
||
| 282 | */ |
||
| 283 | public function exec(string $command): ?string |
||
| 284 | { |
||
| 285 | // run command |
||
| 286 | $exit = -1; |
||
| 287 | $logfile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'tika-error.log'; |
||
| 288 | $descriptors = [['pipe', 'r'], ['pipe', 'w'], ['file', $logfile, 'a']]; |
||
| 289 | $process = proc_open($command, $descriptors, $pipes); |
||
| 290 | $callback = $this->callback; |
||
| 291 | |||
| 292 | // get output if command runs ok |
||
| 293 | if(is_resource($process)) |
||
| 294 | { |
||
| 295 | fclose($pipes[0]); |
||
| 296 | $this->response = ''; |
||
| 297 | while($chunk = stream_get_line($pipes[1], $this->chunkSize)) |
||
| 298 | { |
||
| 299 | if(!is_null($callback)) |
||
| 300 | { |
||
| 301 | $callback($chunk); |
||
| 302 | } |
||
| 303 | |||
| 304 | if($this->callbackAppend === true) |
||
| 305 | { |
||
| 306 | $this->response .= $chunk; |
||
| 307 | } |
||
| 308 | } |
||
| 309 | fclose($pipes[1]); |
||
| 310 | $exit = proc_close($process); |
||
| 311 | } |
||
| 312 | |||
| 313 | // exception if exit value is not zero |
||
| 314 | if($exit > 0) |
||
| 315 | { |
||
| 316 | throw new Exception("Unexpected exit value ($exit) for command $command"); |
||
| 317 | } |
||
| 318 | |||
| 319 | return trim($this->response); |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Get the arguments to run the command |
||
| 324 | * |
||
| 325 | * @throws Exception |
||
| 326 | */ |
||
| 327 | protected function getArguments(string $type, string $file = null): array |
||
| 390 | } |
||
| 391 | } |
||
| 392 |