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

StatusService::getStatus()   A

Complexity

Conditions 2
Paths 7

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 23
rs 9.0856
c 1
b 0
f 0
cc 2
eloc 13
nc 7
nop 0
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: release lock
84
			// returns user specific processed files
85
			$processed = $this->jobMapper->findAllProcessed ( $this->userId );
86
			$this->jobService->handleProcessed ();
87
			
88
			// return user specific pending state
89
			$pending = $this->jobMapper->findAllPending ( $this->userId );
90
			
91
			// return user specific failed state and set up error
92
			$failed = $this->jobMapper->findAllFailed ( $this->userId );
93
			$this->jobService->handleFailed ();
94
			
95
			return [ 
96
					'processed' => count ( $processed ),
97
					'failed' => count ( $failed ),
98
					'pending' => count ( $pending ) 
99
			];
100
		} catch ( Exception $e ) {
101
			$this->handleException ( $e );
102
		}
103
	}
104
	
105
	/**
106
	 * Handle the possible thrown Exceptions from all methods of this class.
107
	 *
108
	 * @param Exception $e        	
109
	 * @throws Exception
110
	 * @throws NotFoundException
111
	 */
112 View Code Duplication
	private function handleException($e) {
113
		$this->logger->logException ( $e, [ 
114
				'app' => 'ocr',
115
				'message' => 'Exception during status service function processing' 
116
		] );
117
		if ($e instanceof NotFoundException) {
118
			throw new NotFoundException ( $e->getMessage () );
119
		} else {
120
			throw $e;
121
		}
122
	}
123
}