Completed
Pull Request — master (#40)
by Janis
123:40 queued 121:18
created

QueueService   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 103
Duplicated Lines 25.24 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 66.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 26
loc 103
ccs 30
cts 45
cp 0.6667
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B clientSend() 0 24 3
A countMessages() 9 9 2
A countActiveProcesses() 9 9 2
A handleException() 8 8 2

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
	private $queue;
29
30
	private $statusqueue;
31
32 7
	public function __construct(OcrStatusMapper $mapper, IL10N $l10n, ILogger $logger) {
33 7
		$this->mapper = $mapper;
34 7
		$this->logger = $logger;
35 7
		$this->l10n = $l10n;
36 7
		$this->queue = msg_get_queue(21671);
37 7
		$this->statusqueue = msg_get_queue(27672);
38 7
	}
39
40
	/**
41
	 * Inits the client and sends the task to the background worker (async)
42
	 *
43
	 * @param OcrStatus $status
44
	 * @param string $datadirectory
45
	 * @param string $path
46
	 * @param string $language
47
	 * @param string $occDir
48
	 */
49 1
	public function clientSend($status, $datadirectory, $path, $language, $occDir) {
50
		try {
51 1
				$this->mapper->insert($status);
52
53 1
				$msg = json_encode(array(
54 1
					'type' => $status->getType(),
55 1
					'datadirectory' => $datadirectory,
56 1
					'path' => $path,
57 1
					'tempfile' => $status->getTempFile(),
58 1
					'language' => $language,
59 1
					'statusid' => $status->getId(),
60
					'occdir' => $occDir
61 1
				));
62 1
				if (msg_send($this->queue, 1, $msg)) {
63 1
					$this->logger->debug('Client message: ' . json_encode($msg), ['app' => 'ocr']);
64 1
				} else {
65
					$this->mapper->delete($status);
66
					throw new NotFoundException($this->l10n->t('Could not add files to the ocr processing queue.'));
67
				}
68 1
		} catch (Exception $e) {
69
			exec('rm ' . $status->getTempFile());
70
			$this->handleException($e);
71
		}
72 1
	}
73
74
	/**
75
     * TODO: in the future this function could be used to give an admin information
76
	 * Counts the messages in the message queue.
77
     *
78
	 * @return mixed
79
	 */
80 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...
81
		try {
82 1
			$stats = msg_stat_queue($this->queue);
83 1
			$this->logger->debug('Current message count: ' . json_encode($stats['msg_qnum']), ['app' => 'ocr']);
84 1
			return $stats['msg_qnum'];
85
		} catch (Exception $e) {
86
			$this->handleException($e);
87
		}
88
	}
89
90
    /**
91
     * TODO: in the future this function could be used to give an admin information
92
     * Counts the at this point processed files
93
     *
94
     * @return mixed
95
     */
96 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...
97
		try {
98 1
			$stats = msg_stat_queue($this->statusqueue);
99 1
			$this->logger->debug('Current active processing count: ' . json_encode($stats['msg_qnum']), ['app' => 'ocr']);
100 1
			return $stats['msg_qnum'];
101
		} catch (Exception $e) {
102
			$this->handleException($e);
103
		}
104
	}
105
106
	/**
107
	 * Handle the possible thrown Exceptions from all methods of this class.
108
	 *
109
	 * @param Exception $e
110
	 * @throws Exception
111
	 * @throws NotFoundException
112
	 */
113 View Code Duplication
	private function handleException($e) {
114
		$this->logger->logException($e, ['app' => 'ocr', 'message' => 'Exception during message queue processing']);
115
		if ($e instanceof NotFoundException) {
116
			throw new NotFoundException($e->getMessage());
117
		} else {
118
			throw $e;
119
		}
120
	}
121
122
}