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

StatusService::handleException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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