Completed
Pull Request — master (#93)
by Janis
04:18
created

StatusService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 7
ccs 7
cts 7
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 6
nc 1
nop 5
crap 1
1
<?php
2
3
/**
4
 * Nextcloud - OCR
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
namespace OCA\Ocr\Service;
12
13
use Exception;
14
use OCP\IL10N;
15
use OCP\ILogger;
16
use OCA\Ocr\Db\OcrJobMapper;
17
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 2
    public function __construct(IL10N $l10n, ILogger $logger, $userId, OcrJobMapper $mapper, JobService $jobService) {
66 2
        $this->logger = $logger;
67 2
        $this->jobMapper = $mapper;
68 2
        $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 2
        $this->l10n = $l10n;
70 2
        $this->jobService = $jobService;
71 2
    }
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
     * @codeCoverageIgnore
77
     * 
78
     * @return string
79
     */
80
    public function getStatus() {
81
        try {
82
            // TODO: For now this will be placed in the regular check, but in the future this could be done in another
83
            // way
84
            $this->jobService->checkForFinishedJobs();
85
            // returns user specific processed files
86
            $processed = $this->jobMapper->findAllProcessed($this->userId);
87
            $this->jobService->handleProcessed();
88
            // return user specific pending state
89
            $pending = $this->jobMapper->findAllPending($this->userId);
90
            // return user specific failed state and set up error
91
            $failed = $this->jobMapper->findAllFailed($this->userId);
92
            $this->jobService->handleFailed();
93
            return [
94
                    'processed' => count($processed),
95
                    'failed' => count($failed),
96
                    'pending' => count($pending)
97
            ];
98
        } catch (Exception $e) {
99
            $this->handleException($e);
100
        }
101
    }
102
103
    /**
104
     * Handle the possible thrown Exceptions from all methods of this class.
105
     * 
106
     * @param Exception $e            
107
     * @throws Exception
108
     * @throws NotFoundException
109
     */
110
    private function handleException($e) {
111
        $this->logger->logException($e, [
112
                'message' => 'Exception during status service function processing'
113
        ]);
114
        throw $e;
115
    }
116
}