Completed
Pull Request — master (#93)
by Janis
14:36
created

StatusService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 102
Duplicated Lines 10.78 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 3
dl 11
loc 102
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B getStatus() 0 25 2
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
namespace OCA\Ocr\Service;
13
14
use Exception;
15
use OCP\IL10N;
16
use OCP\ILogger;
17
use OCA\Ocr\Db\OcrJobMapper;
18
19
/**
20
 * Class StatusService
21
 * 
22
 * @package OCA\Ocr\Service
23
 */
24
class StatusService {
25
	
26
	/**
27
	 *
28
	 * @var ILogger
29
	 */
30
	private $logger;
31
	
32
	/**
33
	 *
34
	 * @var String
35
	 */
36
	private $userId;
37
	
38
	/**
39
	 *
40
	 * @var IL10N
41
	 */
42
	private $l10n;
43
	
44
	/**
45
	 *
46
	 * @var OcrJobMapper
47
	 */
48
	private $jobMapper;
49
	
50
	/**
51
	 *
52
	 * @var JobService
53
	 */
54
	private $jobService;
55
	
56
	/**
57
	 * StatusService constructor.
58
	 *
59
	 * @param IL10N $l10n        	
60
	 * @param ILogger $logger        	
61
	 * @param unknown $userId        	
62
	 * @param OcrJobMapper $mapper        	
63
	 * @param JobService $jobService        	
64
	 */
65
	public function __construct(IL10N $l10n, ILogger $logger, $userId, OcrJobMapper $mapper, JobService $jobService) {
66
		$this->logger = $logger;
67
		$this->jobMapper = $mapper;
68
		$this->userId = $userId;
0 ignored issues
show
Documentation Bug introduced by
It seems like $userId of type object<OCA\Ocr\Service\unknown> is incompatible with the declared type string of property $userId.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
69
		$this->l10n = $l10n;
70
		$this->jobService = $jobService;
71
	}
72
	
73
	/**
74
	 * A function which returns the JSONResponse for all required status checks and tasks.
75
	 * It will check for already processed, pending and failed ocr tasks and return them as needed.
76
	 *
77
	 * @codeCoverageIgnore
78
	 * 
79
	 * @return string
80
	 */
81
	public function getStatus() {
82
		try {
83
			// TODO: For now this will be placed in the regular check, but in the future this could be done in another way
84
			$this->jobService->checkForFinishedJobs();
85
			
86
			// returns user specific processed files
87
			$processed = $this->jobMapper->findAllProcessed ( $this->userId );
88
			$this->jobService->handleProcessed ();
89
			
90
			// return user specific pending state
91
			$pending = $this->jobMapper->findAllPending ( $this->userId );
92
			
93
			// return user specific failed state and set up error
94
			$failed = $this->jobMapper->findAllFailed ( $this->userId );
95
			$this->jobService->handleFailed ();
96
			
97
			return [ 
98
					'processed' => count ( $processed ),
99
					'failed' => count ( $failed ),
100
					'pending' => count ( $pending ) 
101
			];
102
		} catch ( Exception $e ) {
103
			$this->handleException ( $e );
104
		}
105
	}
106
	
107
	/**
108
	 * Handle the possible thrown Exceptions from all methods of this class.
109
	 *
110
	 * @param Exception $e        	
111
	 * @throws Exception
112
	 * @throws NotFoundException
113
	 */
114 View Code Duplication
	private function handleException($e) {
115
		$this->logger->logException ( $e, [ 
116
				'app' => 'ocr',
117
				'message' => 'Exception during status service function processing' 
118
		] );
119
		if ($e instanceof NotFoundException) {
120
			throw new NotFoundException ( $e->getMessage () );
121
		} else {
122
			throw $e;
123
		}
124
	}
125
}