Completed
Pull Request — master (#93)
by Janis
13:30
created

RedisService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 116
Duplicated Lines 9.48 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 11
loc 116
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B sendJob() 0 34 4
A handleException() 11 11 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
/**
4
 * Nextcloud - OCR
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Janis Koehr <[email protected]>
10
 * @copyright Janis Koehr 2017
11
 */
12
13
// TODO: tempfile creation can be removed - output directory will not be in temp dir.
14
namespace OCA\Ocr\Service;
15
16
use Exception;
17
use OCA\Ocr\Db\OcrJob;
18
use OCA\Ocr\Db\OcrJobMapper;
19
use OCP\IConfig;
20
use OCP\IL10N;
21
use OCP\ILogger;
22
23
/**
24
 * Class RedisService
25
 *
26
 * @package OCA\Ocr\Service
27
 */
28
class RedisService {
29
	
30
	/**
31
	 *
32
	 * @var IConfig
33
	 */
34
	private $config;
35
	
36
	/**
37
	 *
38
	 * @var OcrJobMapper
39
	 */
40
	private $mapper;
41
	
42
	/**
43
	 *
44
	 * @var ILogger
45
	 */
46
	private $logger;
47
	
48
	/**
49
	 *
50
	 * @var IL10N
51
	 */
52
	private $l10n;
53
	
54
	/**
55
	 *
56
	 * @var resource
57
	 */
58
	private $queue;
59
	
60
	/**
61
	 *
62
	 * @var resource
63
	 */
64
	private $statusqueue;
0 ignored issues
show
Unused Code introduced by
The property $statusqueue is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
65
	
66
	/**
67
	 * QueueService constructor.
68
	 *
69
	 * @param OcrJobMapper $mapper        	
70
	 * @param IConfig $config        	
71
	 * @param IL10N $l10n        	
72
	 * @param ILogger $logger        	
73
	 */
74
	public function __construct(OcrJobMapper $mapper, IConfig $config, IL10N $l10n, ILogger $logger) {
75
		$this->mapper = $mapper;
76
		$this->logger = $logger;
77
		$this->l10n = $l10n;
78
		$this->config = $config;		
79
	}
80
	
81
	/**
82
	 * Inits the client and sends the task to the background worker (async)
83
	 *
84
	 * @param OcrJob $job        	
85
	 * @param string[] $languages        	
86
	 * @param string $occDir        	
87
	 */
88
	public function sendJob($job, $languages, $occDir) {
89
		try {
90
			// check for messaging and put everything together
91
			if (!extension_loaded ( 'redis' )) {
92
				$this->logger->debug ( 'It seems that the message queueing capabilities are not available in your local php installation. Please install php-redis.', [
93
						'app' => 'ocr'
94
				] );
95
				throw new NotFoundException($this->l10n->t('Message queueing capabilities are missing on the server.'));
96
			}
97
			// TODO: adjust mapping according to zettel
98
			$job = $this->mapper->insert ( $job );
99
			$msg = json_encode ( array (
100
					'type' => $job->getType (),
101
					'source' => $this->config->getSystemValue ( 'datadirectory' ) . '/' . $job->getSource (),
102
					'tempfile' => $job->getTempFile (),
103
					'languages' => $languages,
104
					'jobid' => $job->getId (),
105
					'occdir' => $occDir 
106
			) );
107
			if (msg_send ( $this->queue, 1, $msg )) {
108
				$this->logger->debug ( 'Client message: ' . $msg, [ 
109
						'app' => 'ocr' 
110
				] );
111
			} else {
112
				$this->mapper->delete ( $job );
113
				throw new NotFoundException ( $this->l10n->t ( 'Could not add files to the OCR processing queue.' ) );
114
			}
115
		} catch ( Exception $e ) {
116
			exec ( 'rm ' . $job->getTempFile () );
117
			$job->setStatus ( 'FAILED' );
118
			$this->mapper->update ( $job );
119
			$this->handleException ( $e );
120
		}
121
	}
122
	
123
	// TODO: implement reading finished jobs.
124
	
125
	/**
126
	 * Handle the possible thrown Exceptions from all methods of this class.
127
	 *
128
	 * @param Exception $e        	
129
	 * @throws Exception
130
	 * @throws NotFoundException
131
	 */
132 View Code Duplication
	private function handleException($e) {
133
		$this->logger->logException ( $e, [ 
134
				'app' => 'ocr',
135
				'message' => 'Exception during message queue processing' 
136
		] );
137
		if ($e instanceof NotFoundException) {
138
			throw new NotFoundException ( $e->getMessage () );
139
		} else {
140
			throw $e;
141
		}
142
	}
143
}