Completed
Pull Request — master (#6)
by Janis
340:04 queued 336:33
created

GearmanWorkerService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * nextCloud - ocr
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Janis Koehr <[email protected]>
9
 * @copyright Janis Koehr 2016
10
 */
11
12
namespace OCA\Ocr\Service;
13
14
use Exception;
15
use OCP\ILogger;
16
17
/**
18
 * Class GearmanWorkerService
19
 *
20
 * @package OCA\Ocr\Service
21
 */
22
class GearmanWorkerService {
23
24
	/**
25
	 * @var ILogger
26
	 */
27
	private $logger;
28
29
	/**
30
	 * GearmanWorkerService constructor.
31
	 *
32
	 * @param ILogger $logger
33
	 */
34 2
	public function __construct(ILogger $logger) {
35 2
		$this->logger = $logger;
36 2
	}
37
38
	/**
39
	 * Checks if a worker is active and registered at the Gearman Job Server.
40
	 * returns false if not.
41
	 * @return bool
42
	 */
43
	public function workerExists(){
44
		try {
45
			$checkCommand = 'gearadmin -h 127.0.0.1 -p 4730 --workers 2>&1';
46
			exec($checkCommand, $result, $success);
47
			if ($success !== 0 && count($result) > 0) {
48
				throw new NotFoundException('Gearman worker detection failed.');
49
			}
50
			// 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.
51
			foreach ($result as $res){
1 ignored issue
show
Bug introduced by
The expression $result of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
52
				if(strpos($res, 'ocr') !== false){
53
					$this->logger->debug('Worker found.', ['app' => 'ocr']);
54
					return true;
55
				}
56
			}
57
			$this->logger->debug('No worker found.', ['app' => 'ocr']);
58
			return false;
59
		} catch (Exception $e){
60
			$this->handleException($e);
61
		}
62
	}
63
64
	/**
65
	 * Handle the possible thrown Exceptions from all methods of this class.
66
	 *
67
	 * @param Exception $e
68
	 * @throws Exception
69
	 * @throws NotFoundException
70
	 */
71 View Code Duplication
	private function handleException($e) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
		$this->logger->logException($e, ['app' => 'ocr', 'message' => 'Exception during gearman worker service function processing']);
73
		if ($e instanceof NotFoundException) {
74
			throw new NotFoundException($e->getMessage());
75
		} else {
76
			throw $e;
77
		}
78
	}
79
80
}