Completed
Push — master ( 58d870...e50603 )
by
unknown
02:12
created

PlatformService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 3
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 OC_App;
32
use OCA\FullTextSearch\Exceptions\PlatformDoesNotExistException;
33
use OCA\FullTextSearch\Exceptions\PlatformIsNotCompatibleException;
34
use OCA\FullTextSearch\Exceptions\PlatformNotSelectedException;
35
use OCA\FullTextSearch\IFullTextSearchPlatform;
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 array */
50
	private $platforms = [];
51
52
	/** @var IFullTextSearchPlatform */
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 IFullTextSearchPlatform
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 IFullTextSearchPlatform[]
99
	 * @throws Exception
100
	 */
101
	public function getPlatforms() {
102
		$this->loadPlatforms();
103
104
		$platforms = [];
105
		foreach ($this->platforms as $class) {
106
			try {
107
				$platform = \OC::$server->query((string)$class);
108
				if ($platform instanceof IFullTextSearchPlatform) {
109
					$platforms[$class] = $platform;
110
				}
111
			} 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...
112
				/** we cycle */
113
			}
114
115
		}
116
117
		return $platforms;
118
	}
119
120
121
	/**
122
	 * @throws Exception
123
	 */
124
	private function loadPlatforms() {
125
		if ($this->platformsLoaded) {
126
			return;
127
		}
128
129
		try {
130
			$apps = $this->appManager->getInstalledApps();
131
			foreach ($apps as $appId) {
132
				$this->loadPlatformsFromApp($appId);
133
			}
134
135
			$this->platformsLoaded = true;
136
		} catch (Exception $e) {
137
			$this->miscService->log($e->getMessage());
138
			throw $e;
139
		}
140
141
	}
142
143
144
	/**
145
	 * @throws Exception
146
	 * @throws PlatformDoesNotExistException
147
	 * @throws PlatformIsNotCompatibleException
148
	 * @throws PlatformNotSelectedException
149
	 * @throws QueryException
150
	 */
151
	private function loadPlatform() {
152
		if ($this->platform !== null) {
153
			return;
154
		}
155
156
		$this->loadPlatforms();
157
158
		$selected = $this->getSelectedPlatform();
159
		$platform = \OC::$server->query((string)$selected);
160
		if (!($platform instanceof IFullTextSearchPlatform)) {
161
			throw new PlatformIsNotCompatibleException(
162
				$selected . ' is not a compatible FullTextSearchPlatform'
163
			);
164
		}
165
166
		$platform->loadPlatform();
167
		$this->platform = $platform;
168
	}
169
170
171
	/**
172
	 * @return string
173
	 * @throws PlatformDoesNotExistException
174
	 * @throws PlatformNotSelectedException
175
	 */
176
	private function getSelectedPlatform() {
177
		$selected = $this->configService->getAppValue(ConfigService::SEARCH_PLATFORM);
178
179
		if ($selected === '') {
180
			throw new PlatformNotSelectedException('Admin have not selected any IFullTextSearchPlatform');
181
		}
182
183
		if (!in_array($selected, $this->platforms)) {
184
			throw new PlatformDoesNotExistException(
185
				'FullTextSearchPlatform ' . $selected . ' is not available'
186
			);
187
		}
188
189
		return $selected;
190
	}
191
192
	/**
193
	 * @param string $appId
194
	 */
195
	private function loadPlatformsFromApp($appId) {
196
		$appInfo = OC_App::getAppInfo($appId);
197 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...
198
			|| !key_exists('platform', $appInfo['fulltextsearch'])) {
199
			return;
200
		}
201
202
		$platforms = $appInfo['fulltextsearch']['platform'];
203
		if (!is_array($platforms)) {
204
			$platforms = [$platforms];
205
		}
206
207
		$this->platforms = array_merge($this->platforms, $platforms);
208
	}
209
210
211
}