Completed
Pull Request — master (#90)
by Janis
06:21
created

QueueService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 135
Duplicated Lines 19.26 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 59.18%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 26
loc 135
ccs 29
cts 49
cp 0.5918
rs 10
c 2
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A countMessages() 9 9 2
A countActiveProcesses() 9 9 2
A handleException() 8 8 2
A __construct() 0 13 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 2017
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\IConfig;
18
use OCP\IL10N;
19
use OCP\ILogger;
20
21
/**
22
 * Class QueueService
23
 *
24
 * @package OCA\Ocr\Service
25
 */
26
class QueueService {
27
28
	/**
29
	 * @var IConfig
30
	 */
31
	private $config;
32
33
	/**
34
	 * @var OcrStatusMapper
35
	 */
36
	private $mapper;
37
38
	/**
39
	 * @var ILogger
40
	 */
41
	private $logger;
42
43
	/**
44
	 * @var IL10N
45
	 */
46
	private $l10n;
47
48
	/**
49
	 * @var resource
50
	 */
51
	private $queue;
52
53
	/**
54
	 * @var resource
55
	 */
56
	private $statusqueue;
57
58
	/**
59
	 * QueueService constructor.
60
	 *
61
	 * @param OcrStatusMapper $mapper
62
	 * @param IConfig $config
63
	 * @param IL10N $l10n
64
	 * @param ILogger $logger
65
	 */
66 7
	public function __construct(OcrStatusMapper $mapper, IConfig $config, IL10N $l10n, ILogger $logger) {
67 7
		$this->mapper = $mapper;
68 7
		$this->logger = $logger;
69 7
		$this->l10n = $l10n;
70 7
		$this->config = $config;
71
		try {
72 7
			$this->queue = msg_get_queue(21671);
73 7
			$this->statusqueue = msg_get_queue(27672);
74
		} catch (Exception $e) {
75
			$this->logger->debug('It seems that the message queueing capabilities are not available in your local php installation.', ['app' => 'ocr']);
76
			$this->handleException($e);
77
		}
78 7
	}
79
80
	/**
81
	 * Inits the client and sends the task to the background worker (async)
82
	 *
83
	 * @param OcrStatus $status
84
	 * @param string[] $languages
85
	 * @param string $occDir
86
	 */
87 1
	public function clientSend($status, $languages, $occDir) {
88
		try {
89 1
				$this->mapper->insert($status);
90 1
				$msg = json_encode(array(
91 1
					'type' => $status->getType(),
92 1
					'source' => $this->config->getSystemValue('datadirectory') . '/' . $status->getSource(),
93 1
					'tempfile' => $status->getTempFile(),
94 1
					'languages' => $languages,
95 1
					'statusid' => $status->getId(),
96 1
					'occdir' => $occDir
97
				));
98 1
				if (msg_send($this->queue, 1, $msg)) {
99 1
					$this->logger->debug('Client message: ' . $msg, ['app' => 'ocr']);
100
				} else {
101
					$this->mapper->delete($status);
102 1
					throw new NotFoundException($this->l10n->t('Could not add files to the OCR processing queue.'));
103
				}
104
		} catch (Exception $e) {
105
			exec('rm ' . $status->getTempFile());
106
			$status->setStatus('FAILED');
107
			$this->mapper->update($status);
108
			$this->handleException($e);
109
		}
110 1
	}
111
112
	/**
113
	 * TODO: in the future this function could be used to give an admin information
114
	 * Counts the messages in the message queue.
115
	 *
116
	 * @return mixed
117
	 */
118 1 View Code Duplication
	public function countMessages() {
0 ignored issues
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...
119
		try {
120 1
			$stats = msg_stat_queue($this->queue);
121 1
			$this->logger->debug('Current message count: ' . json_encode($stats['msg_qnum']), ['app' => 'ocr']);
122 1
			return $stats['msg_qnum'];
123
		} catch (Exception $e) {
124
			$this->handleException($e);
125
		}
126
	}
127
128
	/**
129
	 * TODO: in the future this function could be used to give an admin information
130
	 * Counts the at this point processed files
131
	 *
132
	 * @return mixed
133
	 */
134 1 View Code Duplication
	public function countActiveProcesses() {
0 ignored issues
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...
135
		try {
136 1
			$stats = msg_stat_queue($this->statusqueue);
137 1
			$this->logger->debug('Current active processing count: ' . json_encode($stats['msg_qnum']), ['app' => 'ocr']);
138 1
			return $stats['msg_qnum'];
139
		} catch (Exception $e) {
140
			$this->handleException($e);
141
		}
142
	}
143
144
	/**
145
	 * Handle the possible thrown Exceptions from all methods of this class.
146
	 *
147
	 * @param Exception $e
148
	 * @throws Exception
149
	 * @throws NotFoundException
150
	 */
151 View Code Duplication
	private function handleException($e) {
152
		$this->logger->logException($e, ['app' => 'ocr', 'message' => 'Exception during message queue processing']);
153
		if ($e instanceof NotFoundException) {
154
			throw new NotFoundException($e->getMessage());
155
		} else {
156
			throw $e;
157
		}
158
	}
159
160
}