StatusService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 10
cc 1
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
use OCA\Ocr\Constants\OcrConstants;
18
19
20
/**
21
 * Class StatusService
22
 * 
23
 * @package OCA\Ocr\Service
24
 */
25
class StatusService {
26
27
    /**
28
     *
29
     * @var ILogger
30
     */
31
    private $logger;
32
33
    /**
34
     *
35
     * @var string
36
     */
37
    private $userId;
38
39
    /**
40
     *
41
     * @var IL10N
42
     */
43
    private $l10n;
44
45
    /**
46
     *
47
     * @var OcrJobMapper
48
     */
49
    private $jobMapper;
50
51
    /**
52
     *
53
     * @var JobService
54
     */
55
    private $jobService;
56
57
    /**
58
     * StatusService constructor.
59
     * 
60
     * @param IL10N $l10n            
61
     * @param ILogger $logger            
62
     * @param string $userId            
63
     * @param OcrJobMapper $jobMapper            
64
     * @param JobService $jobService            
65
     */
66 3
    public function __construct(IL10N $l10n, ILogger $logger, $userId, OcrJobMapper $jobMapper, JobService $jobService) {
67 3
        $this->logger = $logger;
68 3
        $this->jobMapper = $jobMapper;
69 3
        $this->userId = $userId;
70 3
        $this->l10n = $l10n;
71 3
        $this->jobService = $jobService;
72 3
    }
73
74
    /**
75
     * A function which returns the JSONResponse for all required status checks and tasks.
76
     * It will check for already processed, pending and failed ocr tasks and return them as needed.
77
     * 
78
     * @return string
79
     */
80 1
    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 1
            $this->jobService->checkForFinishedJobs();
85
            // returns user specific processed files
86 1
            $processed = $this->jobMapper->findAllProcessed($this->userId);
87 1
            $this->jobService->handleProcessed();
88
            // return user specific pending state
89 1
            $pending = $this->jobMapper->findAllPending($this->userId);
90
            // return user specific failed state and set up error
91 1
            $failed = $this->jobMapper->findAllFailed($this->userId);
92 1
            $this->jobService->handleFailed();
93
            return [
94 1
                    'processed' => count($processed),
95 1
                    'failed' => count($failed),
96 1
                    '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
                'app' => OcrConstants::APP_NAME
114
        ]);
115
        throw $e;
116
    }
117
}