Completed
Push — master ( da3b44...325c46 )
by Maxence
02:02
created

PlatformService::loadPlatform()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 3
nc 3
nop 0
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\Service;
28
29
use Exception;
30
use OC\App\AppManager;
31
use OCA\FullTextSearch\Exceptions\PlatformDoesNotExistException;
32
use OCA\FullTextSearch\Exceptions\PlatformIsNotCompatibleException;
33
use OCA\FullTextSearch\Exceptions\PlatformNotSelectedException;
34
use OCA\FullTextSearch\IFullTextSearchPlatform;
35
use OCA\FullTextSearch\Model\PlatformWrapper;
36
use OCP\AppFramework\QueryException;
37
38
class PlatformService {
39
40
	/** @var AppManager */
41
	private $appManager;
42
43
	/** @var ConfigService */
44
	private $configService;
45
46
	/** @var MiscService */
47
	private $miscService;
48
49
	/** @var PlatformWrapper[] */
50
	private $platforms = [];
51
52
	/** @var PlatformWrapper */
53
	private $platform;
54
55
	/** @var bool */
56
	private $platformsLoaded = false;
57
58
59
	/**
60
	 * ProviderService constructor.
61
	 *
62
	 * @param AppManager $appManager
63
	 * @param ConfigService $configService
64
	 * @param MiscService $miscService
65
	 *
66
	 */
67
	public function __construct(
68
		AppManager $appManager, ConfigService $configService, MiscService $miscService
69
	) {
70
		$this->appManager = $appManager;
71
72
		$this->configService = $configService;
73
		$this->miscService = $miscService;
74
	}
75
76
77
	/**
78
	 * @param bool $silent
79
	 *
80
	 * @return PlatformWrapper
81
	 * @throws Exception
82
	 */
83
	public function getPlatform($silent = false) {
84
		try {
85
			$this->loadPlatform();
86
		} catch (Exception $e) {
87
			if (!$silent) {
88
				$this->miscService->log($e->getMessage());
89
			}
90
			throw $e;
91
		}
92
93
		return $this->platform;
94
	}
95
96
97
	/**
98
	 * @return PlatformWrapper[]
99
	 * @throws Exception
100
	 */
101
	public function getPlatforms() {
102
		$this->loadPlatforms();
103
104
		$platforms = [];
105
		foreach ($this->platforms as $wrapper) {
106
			$class = $wrapper->getClass();
107
			try {
108
				$platform = \OC::$server->query((string)$class);
109
				if ($platform instanceof IFullTextSearchPlatform) {
110
					$wrapper->setPlatform($platform);
111
					$platforms[] = $wrapper;
112
				}
113
			} catch (QueryException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\AppFramework\QueryException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
114
				/** we cycle */
115
			}
116
117
		}
118
119
		return $platforms;
120
	}
121
122
123
	/**
124
	 * @throws Exception
125
	 */
126 View Code Duplication
	private function loadPlatforms() {
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...
127
		if ($this->platformsLoaded) {
128
			return;
129
		}
130
131
		try {
132
			$apps = $this->appManager->getInstalledApps();
133
			foreach ($apps as $appId) {
134
				$this->loadPlatformsFromApp($appId);
135
			}
136
137
			$this->platformsLoaded = true;
138
		} catch (Exception $e) {
139
			$this->miscService->log($e->getMessage());
140
			throw $e;
141
		}
142
143
	}
144
145
146
	/**
147
	 * @throws Exception
148
	 * @throws PlatformDoesNotExistException
149
	 * @throws PlatformIsNotCompatibleException
150
	 * @throws PlatformNotSelectedException
151
	 * @throws QueryException
152
	 */
153
	private function loadPlatform() {
154
		if ($this->platform !== null) {
155
			return;
156
		}
157
158
		$this->loadPlatforms();
159
160
		$selected = $this->getSelectedPlatform();
161
		$platform = \OC::$server->query((string)$selected->getClass());
162
		if (!($platform instanceof IFullTextSearchPlatform)) {
163
			throw new PlatformIsNotCompatibleException(
164
				$selected . ' is not a compatible FullTextSearchPlatform'
165
			);
166
		}
167
168
		$platform->loadPlatform();
169
		$selected->setPlatform($platform);
170
171
		$this->platform = $selected;
172
	}
173
174
175
	/**
176
	 * @return PlatformWrapper
177
	 * @throws PlatformDoesNotExistException
178
	 * @throws PlatformNotSelectedException
179
	 */
180
	private function getSelectedPlatform() {
181
		$selected = $this->configService->getAppValue(ConfigService::SEARCH_PLATFORM);
182
183
		if ($selected === '') {
184
			throw new PlatformNotSelectedException(
185
				'Admin have not selected any IFullTextSearchPlatform'
186
			);
187
		}
188
189
		foreach ($this->platforms as $wrapper) {
190
			if ($wrapper->getClass() === $selected) {
191
				return $wrapper;
192
			}
193
		}
194
195
		throw new PlatformDoesNotExistException(
196
			'FullTextSearchPlatform ' . $selected . ' is not available'
197
		);
198
	}
199
200
201
	/**
202
	 * @param string $appId
203
	 */
204
	private function loadPlatformsFromApp($appId) {
205
		$appInfo = $this->appManager->getAppInfo($appId);
206 View Code Duplication
		if (!is_array($appInfo) || !key_exists('fulltextsearch', $appInfo)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
207
			|| !key_exists('platform', $appInfo['fulltextsearch'])) {
208
			return;
209
		}
210
211
		$platforms = $appInfo['fulltextsearch']['platform'];
212
		if (!is_array($platforms)) {
213
			$platforms = [$platforms];
214
		}
215
216
		$wrappers = [];
217
		foreach ($platforms as $class) {
218
			$wrapper = new PlatformWrapper($appId, $class);
219
			$wrapper->setVersion($this->configService->getAppVersion($appId));
220
			$wrappers[] = $wrapper;
221
		}
222
223
		$this->platforms = array_merge($this->platforms, $wrappers);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->platforms, $wrappers) of type array is incompatible with the declared type array<integer,object<OCA...Model\PlatformWrapper>> of property $platforms.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
224
	}
225
226
227
}
228