Completed
Pull Request — master (#17)
by Janis
02:51
created

QueueService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 12.31 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 70%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 8
loc 65
ccs 21
cts 30
cp 0.7
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handleException() 8 8 2
B clientSend() 0 24 3

How to fix   Duplicated Code   

Duplicated Code

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
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 OCA\Ocr\Db\OcrStatus;
16
use OCA\Ocr\Db\OcrStatusMapper;
17
use OCP\IL10N;
18
use OCP\ILogger;
19
20
class QueueService {
21
22
	private $mapper;
23
24
	private $logger;
25
26
	private $l10n;
27
28 4
	public function __construct(OcrStatusMapper $mapper, IL10N $l10n, ILogger $logger) {
29 4
		$this->mapper = $mapper;
30 4
		$this->logger = $logger;
31 4
		$this->l10n = $l10n;
32 4
	}
33
34
	/**
35
	 * Inits the client and sends the task to the background worker (async)
36
	 *
37
	 * @param OcrStatus $status
38
	 * @param string $datadirectory
39
	 * @param string $path
40
	 * @param string $language
41
	 * @param string $occDir
42
	 */
43 1
	public function clientSend($status, $datadirectory, $path, $language, $occDir) {
44
		try {
45 1
				$this->mapper->insert($status);
46 1
				$queue = msg_get_queue(21671);
47 1
				$msg = json_encode(array(
48 1
					'type' => $status->getType(),
49 1
					'datadirectory' => $datadirectory,
50 1
					'path' => $path,
51 1
					'tempfile' => $status->getTempFile(),
52 1
					'language' => $language,
53 1
					'statusid' => $status->getId(),
54
					'occdir' => $occDir
55 1
				));
56 1
				if (msg_send($queue, 1, $msg)) {
57 1
					$this->logger->debug('Client message: ' . json_encode($msg), ['app' => 'ocr']);
58 1
				} else {
59
					$this->mapper->delete($status);
60
					throw new NotFoundException($this->l10n->t('Could not add files to the ocr processing queue.'));
61
				}
62 1
		} catch (Exception $e) {
63
			exec('rm ' . $status->getTempFile());
64
			$this->handleException($e);
65
		}
66 1
	}
67
68
	/**
69
	 * Handle the possible thrown Exceptions from all methods of this class.
70
	 *
71
	 * @param Exception $e
72
	 * @throws Exception
73
	 * @throws NotFoundException
74
	 */
75 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...
76
		$this->logger->logException($e, ['app' => 'ocr', 'message' => 'Exception during message queue processing']);
77
		if ($e instanceof NotFoundException) {
78
			throw new NotFoundException($e->getMessage());
79
		} else {
80
			throw $e;
81
		}
82
	}
83
84
}