Completed
Push — master ( 045efd...c665e3 )
by Maxence
01:59 queued 12s
created

Check::displayPlatform()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8817
c 0
b 0
f 0
cc 6
nc 4
nop 1
1
<?php
2
declare(strict_types=1);
3
4
5
/**
6
 * FullTextSearch - Full text search framework for Nextcloud
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later. See the COPYING file.
10
 *
11
 * @author Maxence Lange <[email protected]>
12
 * @copyright 2018
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
30
31
namespace OCA\FullTextSearch\Command;
32
33
34
use Exception;
35
use OC\Core\Command\Base;
36
use OCA\FullTextSearch\Service\ConfigService;
37
use OCA\FullTextSearch\Service\MiscService;
38
use OCA\FullTextSearch\Service\PlatformService;
39
use OCA\FullTextSearch\Service\ProviderService;
40
use Symfony\Component\Console\Input\InputInterface;
41
use Symfony\Component\Console\Input\InputOption;
42
use Symfony\Component\Console\Output\OutputInterface;
43
44
45
class Check extends Base {
46
47
	/** @var ConfigService */
48
	private $configService;
49
50
	/** @var PlatformService */
51
	private $platformService;
52
53
	/** @var ProviderService */
54
	private $providerService;
55
56
	/** @var MiscService */
57
	private $miscService;
58
59
60
	/**
61
	 * Index constructor.
62
	 *
63
	 * @param ConfigService $configService
64
	 * @param PlatformService $platformService
65
	 * @param ProviderService $providerService
66
	 * @param MiscService $miscService
67
	 */
68
	public function __construct(
69
		ConfigService $configService, PlatformService $platformService,
70
		ProviderService $providerService, MiscService $miscService
71
	) {
72
		parent::__construct();
73
74
		$this->configService = $configService;
75
		$this->platformService = $platformService;
76
		$this->providerService = $providerService;
77
		$this->miscService = $miscService;
78
	}
79
80
81
	/**
82
	 *
83
	 */
84
	protected function configure() {
85
		parent::configure();
86
		$this->setName('fulltextsearch:check')
87
			 ->addOption('json', 'j', InputOption::VALUE_NONE, 'return result as JSON')
88
			 ->setDescription('Check the installation');
89
	}
90
91
92
	/**
93
	 * @param InputInterface $input
94
	 * @param OutputInterface $output
95
	 *
96
	 * @return int
97
	 * @throws Exception
98
	 */
99
	protected function execute(InputInterface $input, OutputInterface $output): int {
100
101
		if ($input->getOption('json') === true) {
102
			$output->writeln(json_encode($this->displayAsJson(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
103
104
			return 0;
105
		}
106
107
		$output->writeln(
108
			'Full text search ' . $this->configService->getAppValue('installed_version')
109
		);
110
		$output->writeln(' ');
111
112
		$this->displayPlatform($output);
113
		$this->displayProviders($output);
114
115
		return 0;
116
	}
117
118
119
	/**
120
	 * @return array
121
	 */
122
	private function displayAsJson(): array {
123
124
		$resultPlatform = [];
125
126
		try {
127
			$platforms = $this->platformService->getPlatforms();
128
			foreach ($platforms as $platformWrapper) {
129
				$platform = $platformWrapper->getPlatform();
130
				$platform->loadPlatform();
131
				$resultPlatform[] = [
132
					'class'   => $platformWrapper->getClass(),
133
					'version' => $platformWrapper->getVersion(),
134
					'config'  => $platform->getConfiguration()
135
				];
136
			}
137
138
		} catch (Exception $e) {
139
			$resultPlatform = ['error' => $e->getMessage()];
140
		}
141
142
		$resultProviders = [];
143
		try {
144
			$providers = $this->providerService->getProviders();
145
			foreach ($providers as $providerWrapper) {
146
				$provider = $providerWrapper->getProvider();
147
				$resultProviders[$provider->getId()] = [
148
					'version' => $providerWrapper->getVersion(),
149
					'config'  => $provider->getConfiguration()
150
				];
151
			}
152
		} catch (Exception $e) {
153
			$resultProviders[] = ['error' => $e->getMessage()];
154
		}
155
156
		return [
157
			'fulltextsearch' => [
158
				'version' => $this->configService->getAppValue('installed_version'),
159
				'config'  => $this->configService->getConfig()
160
			],
161
162
			'platform'  => $resultPlatform,
163
			'providers' => $resultProviders
164
		];
165
166
	}
167
168
169
	/**
170
	 * @param OutputInterface $output
171
	 *
172
	 * @throws Exception
173
	 */
174
	private function displayPlatform(OutputInterface $output) {
175
		$platforms = $this->platformService->getPlatforms();
176
177
		if (empty($platforms)) {
178
			$output->writeln('No Search Platform available');
179
180
			return;
181
		}
182
183
		$select = $this->configService->getAppValue(ConfigService::SEARCH_PLATFORM);
184
		$output->writeln('- Search Platform:' . (($select === '') ? ' (none selected)' : ''));
185
186
		foreach ($platforms as $platformWrapper) {
187
			$platform = $platformWrapper->getPlatform();
188
			$selected = ($platformWrapper->getClass() === $select) ? '(Selected)' : '';
189
			$output->writeln($platform->getName() . ' ' . $platformWrapper->getVersion() . ' ' . $selected);
190
			try {
191
				echo json_encode($platform->getConfiguration(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
192
			} catch (Exception $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $e) { ...o '(not configured)'; } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
193
				echo '(not configured)';
194
			}
195
			$output->writeln(' ');
196
		}
197
198
		$output->writeln('');
199
	}
200
201
202
	/**
203
	 * @param OutputInterface $output
204
	 *
205
	 * @throws Exception
206
	 */
207
	private function displayProviders(OutputInterface $output) {
208
		$providers = $this->providerService->getProviders();
209
210
		if (sizeof($providers) === 0) {
211
			$output->writeln('No Content Provider available');
212
213
			return;
214
		}
215
216
		$output->writeln('- Content Providers:');
217
218
		foreach ($providers as $providerWrapper) {
219
			$provider = $providerWrapper->getProvider();
220
			$output->writeln($provider->getName() . ' ' . $providerWrapper->getVersion());
221
			echo json_encode($provider->getConfiguration(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
222
			$output->writeln('');
223
		}
224
	}
225
226
}
227
228