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 QueueService |
||
| 49 | */ |
||
| 50 | private $queueService; |
||
| 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 QueueService $queueService |
||
| 93 | * @param OcrStatusMapper $mapper |
||
| 94 | * @param View $view |
||
| 95 | * @param $userId |
||
| 96 | * @param IL10N $l10n |
||
| 97 | * @param ILogger $logger |
||
| 98 | */ |
||
| 99 | 10 | public function __construct(ITempManager $tempManager, IConfig $config, QueueService $queueService, 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 | 3 | public function listLanguages() { |
|
| 116 | try { |
||
| 117 | 3 | $success = -1; |
|
| 118 | 3 | $this->logger->debug('Fetching languages. ', ['app' => 'ocr']); |
|
| 119 | 3 | exec('tesseract --list-langs 2>&1', $result, $success); |
|
| 120 | 3 | if ($success === 0 && count($result) > 0) { |
|
| 121 | if (is_array($result)) { |
||
| 122 | $traineddata = $result; |
||
| 123 | } else { |
||
| 124 | $traineddata = explode(' ', $result); |
||
| 125 | } |
||
| 126 | $languages = array(); |
||
| 127 | foreach ($traineddata as $td) { |
||
| 128 | $tdname = trim($td); |
||
| 129 | if (strlen($tdname) === 3) { |
||
| 130 | array_push($languages, $tdname); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | $this->logger->debug('Fetched languages: ' . json_encode($languages), ['app' => 'ocr']); |
||
| 134 | return $languages; |
||
| 135 | } else { |
||
| 136 | 3 | throw new NotFoundException($this->l10n->t('No languages found.')); |
|
| 137 | } |
||
| 138 | 3 | } catch (Exception $e) { |
|
| 139 | 3 | $this->handleException($e); |
|
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Processes and prepares the files for ocr. |
||
| 145 | * Sends the stuff to the client in order to ocr async. |
||
| 146 | * |
||
| 147 | * @param string $language |
||
| 148 | * @param array $files |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | 4 | public function process($language, $files) { |
|
| 152 | try { |
||
| 153 | 4 | $this->logger->debug('Will now process files: ' . json_encode($files) . ' with language: ' . json_encode($language), ['app' => 'ocr']); |
|
| 154 | // Check if files and language not empty |
||
| 155 | 4 | if (!empty($files) && !empty($language) && in_array($language, $this->listLanguages())) { |
|
| 156 | // get the array with full fileinfo |
||
| 157 | $fileInfo = $this->buildFileInfo($files); |
||
| 158 | foreach ($fileInfo as $fInfo) { |
||
| 159 | // Check if filelock existing |
||
| 160 | // TODO: FileLock maybe \OC\Files\View::lockFile() |
||
| 161 | // get new name for saving purpose |
||
| 162 | $newName = $this->buildNewName($fInfo); |
||
| 163 | |||
| 164 | // create a temp file for ocr processing purposes |
||
| 165 | $tempFile = $this->tempM->getTemporaryFile(); |
||
| 166 | |||
| 167 | // set the running type |
||
| 168 | if ($fInfo->getMimetype() === $this::MIMETYPE_PDF) { |
||
| 169 | $ftype = 'mypdf'; |
||
| 170 | } else { |
||
| 171 | $ftype = 'tess'; |
||
| 172 | } |
||
| 173 | |||
| 174 | // Create status object |
||
| 175 | $status = new OcrStatus('PENDING', $fInfo->getId(), $newName, $tempFile, $ftype, $this->userId); |
||
| 176 | |||
| 177 | // Init client and send task / job |
||
| 178 | // Feed the worker |
||
| 179 | $this->queueService->clientSend($status, $this->config->getSystemValue('datadirectory'), $fInfo->getPath(), $language, \OC::$SERVERROOT); |
||
| 180 | } |
||
| 181 | return 'PROCESSING'; |
||
| 182 | } else { |
||
| 183 | 2 | throw new NotFoundException($this->l10n->t('Empty passed parameters.')); |
|
| 184 | } |
||
| 185 | 4 | } catch (Exception $e) { |
|
| 186 | 4 | $this->handleException($e); |
|
| 187 | } |
||
| 188 | } |
||
| 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 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 | * protected as of testing issues with static methods. (Actually |
||
| 290 | * it will be mocked partially) FIXME: Change this behaviour as soon as the buidlNotExistingFileName function is not static anymore |
||
| 291 | * |
||
| 292 | * @param FileInfo $fileInfo |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | protected function buildNewName(FileInfo $fileInfo) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Returns the fileInfo for each file in files and checks |
||
| 313 | * if it has a allowed mimetype and some other conditions. |
||
| 314 | * |
||
| 315 | * @param array $files |
||
| 316 | * @return array of Files\FileInfo |
||
| 317 | * @throws NotFoundException |
||
| 318 | */ |
||
| 319 | private function buildFileInfo(array $files) { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Checks a Mimetype for a specific given FileInfo. |
||
| 342 | * @param Files\FileInfo $fileInfo |
||
| 343 | */ |
||
| 344 | private function checkMimeType(FileInfo $fileInfo) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Returns the correct path based on delivered file variable |
||
| 357 | * @param $file |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | private function getCorrectPath($file) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Handle the possible thrown Exceptions from all methods of this class. |
||
| 375 | * |
||
| 376 | * @param Exception $e |
||
| 377 | * @throws Exception |
||
| 378 | * @throws NotFoundException |
||
| 379 | */ |
||
| 380 | 6 | View Code Duplication | private function handleException($e) { |
| 388 | } |