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:
| 1 | <?php |
||
| 23 | class GearmanWorkerService { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var ILogger |
||
| 27 | */ |
||
| 28 | private $logger; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var IL10N |
||
| 32 | */ |
||
| 33 | private $l10n; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * GearmanWorkerService constructor. |
||
| 37 | * |
||
| 38 | * @param IL10N $l10n |
||
| 39 | * @param ILogger $logger |
||
| 40 | */ |
||
| 41 | 4 | public function __construct(IL10N $l10n, ILogger $logger) { |
|
| 45 | |||
| 46 | /** |
||
| 47 | * Checks if a worker is active and registered at the Gearman Job Server. |
||
| 48 | * returns false if not. |
||
| 49 | * @return boolean|null |
||
| 50 | */ |
||
| 51 | 1 | public function workerExists() { |
|
| 52 | try { |
||
| 53 | 1 | $checkCommand = 'gearadmin -h 127.0.0.1 -p 4730 --workers 2>&1'; |
|
| 54 | 1 | exec($checkCommand, $result, $success); |
|
| 55 | 1 | if ($success !== 0) { |
|
| 56 | throw new NotFoundException($this->l10n->t('Gearman worker detection failed.')); |
||
| 57 | } |
||
| 58 | // look into the resulting array. 3 because first row is the ps checking command, second row is the grep command separated from the ps and 3rd or more has to be the GearmanOCRWorker.php. |
||
| 59 | 1 | foreach ($result as $res) { |
|
| 60 | 1 | if (strpos($res, 'ocr') !== false) { |
|
| 61 | $this->logger->debug('Worker found.', ['app' => 'ocr']); |
||
| 62 | return true; |
||
| 63 | } |
||
| 64 | 1 | } |
|
| 65 | 1 | $this->logger->debug('No worker found.', ['app' => 'ocr']); |
|
| 66 | 1 | return false; |
|
| 67 | } catch (Exception $e) { |
||
| 68 | $this->handleException($e); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Handle the possible thrown Exceptions from all methods of this class. |
||
| 74 | * |
||
| 75 | * @param Exception $e |
||
| 76 | * @throws Exception |
||
| 77 | * @throws NotFoundException |
||
| 78 | */ |
||
| 79 | View Code Duplication | private function handleException($e) { |
|
| 87 | |||
| 88 | } |