Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like OcrService 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 OcrService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class OcrService { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var ILogger |
||
| 34 | */ |
||
| 35 | private $logger; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var ITempManager |
||
| 39 | */ |
||
| 40 | private $tempM; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var IConfig |
||
| 44 | */ |
||
| 45 | private $config; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var GearmanWorkerService |
||
| 49 | */ |
||
| 50 | private $workerService; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var OcrStatusMapper |
||
| 54 | */ |
||
| 55 | private $statusMapper; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var View |
||
| 59 | */ |
||
| 60 | private $view; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var |
||
| 64 | */ |
||
| 65 | private $userId; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var IL10N |
||
| 69 | */ |
||
| 70 | private $l10n; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Array of allowed mimetypes for ocr processing |
||
| 74 | */ |
||
| 75 | const ALLOWED_MIMETYPES = ['application/pdf', 'image/png', 'image/jpeg', 'image/tiff']; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * the correct mimetype for a pdf file |
||
| 79 | */ |
||
| 80 | const MIMETYPE_PDF = 'application/pdf'; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * the only allowed image mimetypes by tesseract |
||
| 84 | */ |
||
| 85 | const MIMETYPES_IMAGE = ['image/png', 'image/jpeg', 'image/tiff']; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * OcrService constructor. |
||
| 89 | * |
||
| 90 | * @param ITempManager $tempManager |
||
| 91 | * @param IConfig $config |
||
| 92 | * @param GearmanWorkerService $workerService |
||
| 93 | * @param OcrStatusMapper $mapper |
||
| 94 | * @param View $view |
||
| 95 | * @param $userId |
||
| 96 | * @param IL10N $l10n |
||
| 97 | * @param ILogger $logger |
||
| 98 | */ |
||
| 99 | 2 | public function __construct(ITempManager $tempManager, IConfig $config, GearmanWorkerService $workerService, OcrStatusMapper $mapper, View $view, $userId, IL10N $l10n, ILogger $logger) { |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Gets the list of all available tesseract-ocr languages. |
||
| 112 | * |
||
| 113 | * @return array Languages |
||
| 114 | */ |
||
| 115 | public function listLanguages() { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Processes and prepares the files for ocr. |
||
| 145 | * Sends the stuff to the gearman client in order to ocr async. |
||
| 146 | * |
||
| 147 | * @param string $language |
||
| 148 | * @param array $files |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function process($language, $files) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * A function which returns the JSONResponse for all required status checks and tasks. |
||
| 192 | * It will check for already processed, pending and failed ocr tasks and return them as needed. |
||
| 193 | * |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public function status() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * The command ocr:complete for occ will call this function in order to set the status. |
||
| 213 | * the gearman worker should call it automatically after each processing step. |
||
| 214 | * |
||
| 215 | * @param $statusId |
||
| 216 | * @param boolean $failed |
||
| 217 | */ |
||
| 218 | public function complete($statusId, $failed) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Finishes all Processed files by copying them to the right path and deleteing the temp files. |
||
| 240 | * Returns the number of processed files. |
||
| 241 | * |
||
| 242 | * @return int |
||
| 243 | */ |
||
| 244 | private function handleProcessed() { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Handles all failed orders of ocr processing queue and returns the status objects. |
||
| 272 | * |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | private function handleFailed() { |
||
| 290 | |||
| 291 | |||
| 292 | /** |
||
| 293 | * Returns a not existing file name for pdf or image processing |
||
| 294 | * |
||
| 295 | * @param FileInfo $fileInfo |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | private function buildNewName(FileInfo $fileInfo) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Returns the fileInfo for each file in files and checks |
||
| 316 | * if it has a allowed mimetype and some other conditions. |
||
| 317 | * |
||
| 318 | * @param array $files |
||
| 319 | * @return array of Files\FileInfo |
||
| 320 | * @throws NotFoundException |
||
| 321 | */ |
||
| 322 | private function buildFileInfo(array $files) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Checks a Mimetype for a specific given FileInfo. |
||
| 345 | * @param Files\FileInfo $fileInfo |
||
| 346 | */ |
||
| 347 | private function checkMimeType(FileInfo $fileInfo){ |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Returns the correct path based on delivered file variable |
||
| 360 | * @param $file |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | private function getCorrectPath($file) { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Inits the Gearman client and sends the task to the background worker (async) |
||
| 378 | * @param string $type |
||
| 379 | * @param $datadirectory |
||
| 380 | * @param $path |
||
| 381 | * @param $tempFile |
||
| 382 | * @param string $language |
||
| 383 | * @param OcrStatus $status |
||
| 384 | * @param string $occDir |
||
| 385 | */ |
||
| 386 | private function sendGearmanJob($type, $datadirectory, $path, $tempFile, $language, $status, $occDir) { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Handle the possible thrown Exceptions from all methods of this class. |
||
| 412 | * |
||
| 413 | * @param Exception $e |
||
| 414 | * @throws Exception |
||
| 415 | * @throws NotFoundException |
||
| 416 | */ |
||
| 417 | View Code Duplication | private function handleException($e) { |
|
| 425 | } |