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 | 5 | 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 | 3 | public function complete($statusId, $failed) { |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Finishes all Processed files by copying them to the right path and deleteing the temp files. |
||
| 235 | * Returns the number of processed files. |
||
| 236 | * |
||
| 237 | * @return int |
||
| 238 | */ |
||
| 239 | private function handleProcessed() { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Handles all failed orders of ocr processing queue and returns the status objects. |
||
| 267 | * |
||
| 268 | * @return array |
||
| 269 | */ |
||
| 270 | private function handleFailed() { |
||
| 285 | |||
| 286 | |||
| 287 | /** |
||
| 288 | * Returns a not existing file name for pdf or image processing |
||
| 289 | * |
||
| 290 | * @param FileInfo $fileInfo |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | private function buildNewName(FileInfo $fileInfo) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Returns the fileInfo for each file in files and checks |
||
| 311 | * if it has a allowed mimetype and some other conditions. |
||
| 312 | * |
||
| 313 | * @param array $files |
||
| 314 | * @return array of Files\FileInfo |
||
| 315 | * @throws NotFoundException |
||
| 316 | */ |
||
| 317 | private function buildFileInfo(array $files) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Checks a Mimetype for a specific given FileInfo. |
||
| 340 | * @param Files\FileInfo $fileInfo |
||
| 341 | */ |
||
| 342 | private function checkMimeType(FileInfo $fileInfo) { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Returns the correct path based on delivered file variable |
||
| 355 | * @param $file |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | private function getCorrectPath($file) { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Inits the Gearman client and sends the task to the background worker (async) |
||
| 373 | * @param string $type |
||
| 374 | * @param $datadirectory |
||
| 375 | * @param $path |
||
| 376 | * @param $tempFile |
||
| 377 | * @param string $language |
||
| 378 | * @param OcrStatus $status |
||
| 379 | * @param string $occDir |
||
| 380 | */ |
||
| 381 | private function sendGearmanJob($type, $datadirectory, $path, $tempFile, $language, $status, $occDir) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Handle the possible thrown Exceptions from all methods of this class. |
||
| 407 | * |
||
| 408 | * @param Exception $e |
||
| 409 | * @throws Exception |
||
| 410 | * @throws NotFoundException |
||
| 411 | */ |
||
| 412 | 1 | View Code Duplication | private function handleException($e) { |
| 420 | } |