Completed
Push — master ( de76a7...7811d3 )
by Maxence
02:17 queued 14s
created

DocumentStatus::statusConvertToString()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 7.6666
c 0
b 0
f 0
cc 10
nc 10
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
4
/**
5
 * FullTextSearch - Full text search framework for Nextcloud
6
 *
7
 * This file is licensed under the Affero General Public License version 3 or
8
 * later. See the COPYING file.
9
 *
10
 * @author Maxence Lange <[email protected]>
11
 * @copyright 2018
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
30
namespace OCA\FullTextSearch\Command;
31
32
33
use Exception;
34
use OC\Core\Command\Base;
35
use OCA\FullTextSearch\Exceptions\IndexDoesNotExistException;
36
use OCA\FullTextSearch\Service\IndexService;
37
use OCA\FullTextSearch\Service\MiscService;
38
use OCP\FullTextSearch\Model\IIndex;
39
use Symfony\Component\Console\Input\InputArgument;
40
use Symfony\Component\Console\Input\InputInterface;
41
use Symfony\Component\Console\Input\InputOption;
42
use Symfony\Component\Console\Output\OutputInterface;
43
44
45
/**
46
 * Class DocumentStatus
47
 *
48
 * @package OCA\FullTextSearch\Command
49
 */
50
class DocumentStatus extends Base {
51
52
53
	/** @var IndexService */
54
	private $indexService;
55
56
	/** @var MiscService */
57
	private $miscService;
58
59
	/** @var array */
60
	private $statusAvailable = [
61
		'IGNORE' => 'document will never be indexed',
62
		'INDEX'  => 'document will be indexed',
63
		'DONE'   => 'document is well indexed',
64
		'REMOVE' => 'document will be removed',
65
		'FAILED' => 'index had fail'
66
	];
67
68
69
	/**
70
	 * DocumentStatus constructor.
71
	 *
72
	 * @param IndexService $indexService
73
	 * @param MiscService $miscService
74
	 */
75
	public function __construct(IndexService $indexService, MiscService $miscService) {
76
		parent::__construct();
77
78
		$this->indexService = $indexService;
79
		$this->miscService = $miscService;
80
	}
81
82
83
	/**
84
	 *
85
	 */
86
	protected function configure() {
87
		parent::configure();
88
		$this->setName('fulltextsearch:document:status')
89
			 ->setDescription('change the status on one specific document')
90
			 ->addArgument('provider', InputArgument::REQUIRED, 'Id of the provider')
91
			 ->addArgument('document', InputArgument::REQUIRED, 'If of the document')
92
			 ->addOption('value', '', InputOption::VALUE_REQUIRED, 'new status', '')
93
			 ->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'specify the owner of the document', '')
94
			 ->addOption('json', 'j', InputOption::VALUE_NONE, 'return status in JSON');
95
	}
96
97
98
	/**
99
	 * @param InputInterface $input
100
	 * @param OutputInterface $output
101
	 *
102
	 * @throws Exception
103
	 */
104
	protected function execute(InputInterface $input, OutputInterface $output) {
105
		$providerId = $input->getArgument('provider');
106
		$documentId = $input->getArgument('document');
107
		$value = $input->getOption('value');
108
		$userId = $input->getOption('user');
109
		$json = $input->getOption('json');
110
111
		try {
112
			$index = $this->indexService->getIndex($providerId, $documentId);
113
			if ($value !== '') {
114
				$status = $this->statusConvertFromString($value);
115
				$index->setStatus($status, true);
116
				$this->indexService->updateIndex($index);
117
			}
118
		} catch (IndexDoesNotExistException $e) {
119
			if ($userId === '') {
120
				throw new Exception(
121
					"Index is not known.\nIf you want to generate the entry, please specify the owner of the document using --user <userId>"
122
				);
123
			}
124
125
			$status = $this->statusConvertFromString($value);
126
			$index = $this->indexService->createIndex($providerId, $documentId, $userId, $status);
127
		}
128
129
130
		if ($json) {
131
			echo json_encode($index, JSON_PRETTY_PRINT) . "\n";
132
133
			return;
134
		}
135
136
		$status = $this->statusConvertToString($index->getStatus());
137
		$desc = $this->statusAvailable[$status];
138
		$output->writeln('current status: <info>' . $status . '</info> (' . $desc . ')');
139
	}
140
141
142
	/**
143
	 * @param int $status
144
	 *
145
	 * @return string
146
	 */
147
	private function statusConvertToString(int $status): string {
148
		switch ($status) {
149
			case IIndex::INDEX_OK:
150
			case IIndex::INDEX_DONE:
151
				return 'DONE';
152
153
			case IIndex::INDEX_IGNORE:
154
				return 'IGNORE';
155
156
			case IIndex::INDEX_META:
157
			case IIndex::INDEX_CONTENT:
158
			case IIndex:: INDEX_PARTS:
159
			case IIndex:: INDEX_FULL:
160
				return 'INDEX';
161
162
			case IIndex:: INDEX_REMOVE:
163
				return 'REMOVE';
164
165
			case IIndex::INDEX_FAILED:
166
				return 'FAILED';
167
		}
168
169
		return 'unknown';
170
	}
171
172
173
	/**
174
	 * @param string $status
175
	 *
176
	 * @return int
177
	 * @throws Exception
178
	 */
179
	private function statusConvertFromString(string $status): int {
180
		switch ($status) {
181
			case 'DONE':
182
				return IIndex::INDEX_OK;
183
184
			case 'IGNORE':
185
				return IIndex::INDEX_IGNORE;
186
187
			case 'INDEX':
188
				return IIndex:: INDEX_FULL;
189
190
			case 'REMOVE':
191
				return IIndex:: INDEX_REMOVE;
192
193
			case 'FAILED':
194
				return IIndex::INDEX_FAILED;
195
		}
196
197
		throw new Exception("Specify a valid status: " . implode(', ', array_keys($this->statusAvailable)));
198
	}
199
200
}
201
202