Completed
Pull Request — master (#90)
by Janis
06:21
created

CompleteOCR   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 5
dl 0
loc 72
ccs 20
cts 40
cp 0.5
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configure() 0 19 1
B execute() 0 26 6
1
<?php
2
/**
3
 * nextCloud - ocr
4
 *
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
12
namespace OCA\Ocr\Command;
13
14
use Exception;
15
use OCA\Ocr\Service\OcrService;
16
use OCA\Ocr\Service\ServiceException;
17
use OCP\AppFramework\Db\DoesNotExistException;
18
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Output\OutputInterface;
23
24
/**
25
 * Class CompleteOCR
26
 *
27
 * @package OCA\Ocr\Command
28
 */
29
class CompleteOCR extends Command {
30
31
	/**
32
	 * @var OcrService
33
	 */
34
	private $ocrService;
35
36
	/**
37
	 * UpdateStatus constructor.
38
	 */
39 3
	public function __construct(OcrService $ocrService) {
40 3
		parent::__construct();
41 3
		$this->ocrService = $ocrService;
42 3
	}
43
44
	/**
45
	 * Required Arguments configuration
46
	 */
47 3
	protected function configure() {
48 3
		$this->setName('ocr:complete')
49 3
			->addArgument(
50 3
				'status-id',
51 3
				InputArgument::REQUIRED,
52 3
				'status id, integer'
53
			)
54 3
			->addArgument(
55 3
				'failed',
56 3
				InputArgument::REQUIRED,
57 3
				'failed, boolean'
58
			)
59 3
			->addArgument(
60 3
				'error-message',
61 3
				InputArgument::OPTIONAL,
62 3
				'error message, string'
63
			)
64 3
			->setDescription('Console API for completion of the ocr processing of a file');
65 3
	}
66
67
	/**
68
	 * Executes the complete function of the OCRService
69
	 *
70
	 * @param InputInterface $input
71
	 * @param OutputInterface $output
72
	 * @return int|null|void
73
	 */
74
	protected function execute(InputInterface $input, OutputInterface $output) {
75
		$statusId = $input->getArgument('status-id');
76
		$failed = $input->getArgument('failed');
77
		$errorMessage = $input->getArgument('error-message');
78
		try {
79
			if ($failed === 'false') {
80
				$failed = false;
81
			} elseif ($failed === 'true') {
82
				$failed = true;
83
				$errorMessage = null;
84
			} else {
85
				throw new ServiceException('Wrong Arguments.');
86
			}
87
			$this->ocrService->complete($statusId, $failed, $errorMessage);
88
		} catch (Exception $e) {
89
			if ($e instanceof MultipleObjectsReturnedException || $e instanceof DoesNotExistException) {
90
				$output->writeln('<error>Could not complete ocr for status id ' . $statusId .
91
					': ' . $e->getMessage() .
92
					'</error> ');
93
			} else {
94
				$output->writeln('<error>Unexpected error for status id ' . $statusId .
95
					': ' . $e->getMessage() .
96
					'</error> ');
97
			}
98
		}
99
	}
100
}