Completed
Push — master ( d92761...e5ee26 )
by Maxence
04:52
created

Index::generateIndexOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * FullTextSearch - Full text search framework for Nextcloud
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2018
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\FullTextSearch\Command;
28
29
use Exception;
30
use OCA\FullTextSearch\IFullTextSearchProvider;
31
use OCA\FullTextSearch\Model\ExtendedBase;
32
use OCA\FullTextSearch\Model\IndexOptions;
33
use OCA\FullTextSearch\Model\Runner;
34
use OCA\FullTextSearch\Service\IndexService;
35
use OCA\FullTextSearch\Service\MiscService;
36
use OCA\FullTextSearch\Service\PlatformService;
37
use OCA\FullTextSearch\Service\ProviderService;
38
use OCA\FullTextSearch\Service\RunningService;
39
use OCP\IUserManager;
40
use Symfony\Component\Console\Input\InputArgument;
41
use Symfony\Component\Console\Input\InputInterface;
42
use Symfony\Component\Console\Output\OutputInterface;
43
44
45
class Index extends ExtendedBase {
46
47
	/** @var IUserManager */
48
	private $userManager;
49
50
	/** @var IndexService */
51
	private $indexService;
52
53
	/** @var PlatformService */
54
	private $platformService;
55
56
	/** @var ProviderService */
57
	private $providerService;
58
59
	/** @var MiscService */
60
	private $miscService;
61
62
63
	/** @var Runner */
64
	private $runner;
65
66
67
	/**
68
	 * Index constructor.
69
	 *
70
	 * @param IUserManager $userManager
71
	 * @param RunningService $runningService
72
	 * @param IndexService $indexService
73
	 * @param PlatformService $platformService
74
	 * @param ProviderService $providerService
75
	 * @param MiscService $miscService
76
	 */
77 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
		IUserManager $userManager, RunningService $runningService, IndexService $indexService,
79
		PlatformService $platformService, ProviderService $providerService, MiscService $miscService
80
	) {
81
		parent::__construct();
82
		$this->userManager = $userManager;
83
84
		$this->runner = new Runner($runningService, 'commandIndex');
85
		$this->indexService = $indexService;
86
		$this->indexService->setRunner($this->runner);
87
88
		$this->platformService = $platformService;
89
		$this->providerService = $providerService;
90
		$this->miscService = $miscService;
91
	}
92
93
94
	/**
95
	 *
96
	 */
97
	protected function configure() {
98
		parent::configure();
99
		$this->setName('fulltextsearch:index')
100
			 ->setDescription('Index files')
101
			 ->addArgument('options', InputArgument::OPTIONAL, 'options');
102
	}
103
104
105
	/**
106
	 * @param InputInterface $input
107
	 * @param OutputInterface $output
108
	 *
109
	 * @return int|null|void
110
	 * @throws Exception
111
	 */
112
	protected function execute(InputInterface $input, OutputInterface $output) {
113
		$options = $this->generateIndexOptions($input);
114
115
		try {
116
			$this->runner->sourceIsCommandLine($this, $output);
117
			$this->runner->start();
118
119
			$providers = $this->providerService->getProviders();
120
			foreach ($providers as $provider) {
121
122
				if (!$this->isIncludedProvider($options, $provider->getId())) {
123
					continue;
124
				}
125
126
				$this->runner->output('indexing ' . $provider->getName() . '.');
127
				$provider->setRunner($this->runner);
128
				$provider->setIndexOptions($options);
0 ignored issues
show
Bug introduced by
The method setIndexOptions() does not seem to exist on object<OCA\FullTextSearc...FullTextSearchProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
				$this->indexProvider($provider, $options);
130
			}
131
132
		} catch (Exception $e) {
133
			$this->runner->exception($e->getMessage(), true);
134
			throw $e;
135
		}
136
137
		$this->runner->stop();
138
	}
139
140
141
	/**
142
	 * @param IFullTextSearchProvider $provider
143
	 * @param IndexOptions $options
144
	 *
145
	 * @throws Exception
146
	 */
147
	private function indexProvider(IFullTextSearchProvider $provider, IndexOptions $options) {
148
		$platform = $this->platformService->getPlatform();
149
		$platform->initializeIndex();
150
		$provider->onInitializingIndex($platform);
151
152
		$platform->setRunner($this->runner);
153
154
		$users = $this->generateUserList($options);
155
		foreach ($users as $user) {
156
			if ($user === null) {
157
				continue;
158
			}
159
160
			$this->runner->output(' USER: ' . $user->getUID());
161
			$this->indexService->indexProviderContentFromUser(
162
				$platform, $provider, $user->getUID(), $options
163
			);
164
		}
165
166
		$this->providerService->setProviderAsIndexed($provider, true);
167
168
	}
169
170
171
	/**
172
	 * @param InputInterface $input
173
	 *
174
	 * @return IndexOptions
175
	 */
176
	private function generateIndexOptions(InputInterface $input) {
177
		$jsonOptions = $input->getArgument('options');
178
		$options = json_decode($jsonOptions, true);
179
180
		if (!is_array($options)) {
181
			$options = [];
182
		}
183
184
		return new IndexOptions($options);
185
	}
186
187
188
	/**
189
	 * @param IndexOptions $options
190
	 * @param string $providerId
191
	 *
192
	 * @return bool
193
	 */
194
	private function isIncludedProvider(IndexOptions $options, $providerId) {
195
		if ($options->getOption('provider', '') !== ''
196
			&& $options->getOption('provider') !== $providerId) {
197
			return false;
198
		}
199
200
		if ($options->getOption('providers', null) !== null
201
			&& is_array($options->getOption('providers'))) {
202
			return (in_array($providerId, $options->getOption('providers')));
203
		}
204
205
		return true;
206
	}
207
208
209
	/**
210
	 * @param IndexOptions $options
211
	 *
212
	 * @return array
213
	 */
214
	private function generateUserList(IndexOptions $options) {
215
		if ($options->getOption('user', '') !== '') {
216
			return [$this->userManager->get($options->getOption('user'))];
217
		}
218
219
		if ($options->getOption('users', null) !== null
220
			&& is_array($options->getOption('users'))) {
221
			return array_map([$this->userManager, 'get'], $options->getOption('users'));
222
		}
223
224
		return $this->userManager->search('');
225
	}
226
}
227
228
229
230