Passed
Push — master ( 274746...99f7e7 )
by Roeland
19:54 queued 09:57
created

FullTextSearchManager   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 190
rs 10
c 0
b 0
f 0
wmc 21

15 Methods

Rating   Name   Duplication   Size   Complexity  
A registerProviderService() 0 2 1
A registerSearchService() 0 2 1
A registerIndexService() 0 2 1
A createIndex() 0 2 1
A getProviderService() 0 6 2
A updateIndexesStatus() 0 2 1
A getSearchService() 0 6 2
A getIndexService() 0 6 2
A getIndex() 0 2 1
A addJavascriptAPI() 0 2 1
A isAvailable() 0 8 4
A search() 0 4 1
A isProviderIndexed() 0 2 1
A updateIndexStatus() 0 2 1
A updateIndexes() 0 2 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, Maxence Lange <[email protected]>
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 OC\FullTextSearch;
32
33
34
use OCP\FullTextSearch\Exceptions\FullTextSearchAppNotAvailableException;
35
use OCP\FullTextSearch\IFullTextSearchManager;
36
use OCP\FullTextSearch\Model\IIndex;
37
use OCP\FullTextSearch\Model\ISearchResult;
38
use OCP\FullTextSearch\Service\IIndexService;
39
use OCP\FullTextSearch\Service\IProviderService;
40
use OCP\FullTextSearch\Service\ISearchService;
41
42
43
/**
44
 * Class FullTextSearchManager
45
 *
46
 * @package OC\FullTextSearch
47
 */
48
class FullTextSearchManager implements IFullTextSearchManager {
49
50
51
	/** @var IProviderService */
52
	private $providerService;
53
54
	/** @var IIndexService */
55
	private $indexService;
56
57
	/** @var ISearchService */
58
	private $searchService;
59
60
61
	/**
62
	 * @since 15.0.0
63
	 *
64
	 * @param IProviderService $providerService
65
	 */
66
	public function registerProviderService(IProviderService $providerService) {
67
		$this->providerService = $providerService;
68
	}
69
70
	/**
71
	 * @since 15.0.0
72
	 *
73
	 * @param IIndexService $indexService
74
	 */
75
	public function registerIndexService(IIndexService $indexService) {
76
		$this->indexService = $indexService;
77
	}
78
79
	/**
80
	 * @since 15.0.0
81
	 *
82
	 * @param ISearchService $searchService
83
	 */
84
	public function registerSearchService(ISearchService $searchService) {
85
		$this->searchService = $searchService;
86
	}
87
88
	/**
89
	 * @since 16.0.0
90
	 *
91
	 * @return bool
92
	 */
93
	public function isAvailable(): bool {
94
		if ($this->indexService === null ||
95
			$this->providerService === null ||
96
			$this->searchService === null) {
97
			return false;
98
		}
99
100
		return true;
101
	}
102
103
104
	/**
105
	 * @return IProviderService
106
	 * @throws FullTextSearchAppNotAvailableException
107
	 */
108
	private function getProviderService(): IProviderService {
109
		if ($this->providerService === null) {
110
			throw new FullTextSearchAppNotAvailableException('No IProviderService registered');
111
		}
112
113
		return $this->providerService;
114
	}
115
116
117
	/**
118
	 * @return IIndexService
119
	 * @throws FullTextSearchAppNotAvailableException
120
	 */
121
	private function getIndexService(): IIndexService {
122
		if ($this->indexService === null) {
123
			throw new FullTextSearchAppNotAvailableException('No IIndexService registered');
124
		}
125
126
		return $this->indexService;
127
	}
128
129
130
	/**
131
	 * @return ISearchService
132
	 * @throws FullTextSearchAppNotAvailableException
133
	 */
134
	private function getSearchService(): ISearchService {
135
		if ($this->searchService === null) {
136
			throw new FullTextSearchAppNotAvailableException('No ISearchService registered');
137
		}
138
139
		return $this->searchService;
140
	}
141
142
143
	/**
144
	 * @throws FullTextSearchAppNotAvailableException
145
	 */
146
	public function addJavascriptAPI() {
147
		$this->getProviderService()->addJavascriptAPI();
148
	}
149
150
151
	/**
152
	 * @param string $providerId
153
	 *
154
	 * @return bool
155
	 * @throws FullTextSearchAppNotAvailableException
156
	 */
157
	public function isProviderIndexed(string $providerId): bool {
158
		return $this->getProviderService()->isProviderIndexed($providerId);
159
	}
160
161
162
	/**
163
	 * @param string $providerId
164
	 * @param string $documentId
165
	 * @return IIndex
166
	 * @throws FullTextSearchAppNotAvailableException
167
	 */
168
	public function getIndex(string $providerId, string $documentId): IIndex {
169
		return $this->getIndexService()->getIndex($providerId, $documentId);
170
	}
171
172
	/**
173
	 * @param string $providerId
174
	 * @param string $documentId
175
	 * @param string $userId
176
	 * @param int $status
177
	 *
178
	 * @see IIndex for available value for $status.
179
	 *
180
	 * @return IIndex
181
	 * @throws FullTextSearchAppNotAvailableException
182
	 */
183
	public function createIndex(string $providerId, string $documentId, string $userId, int $status = 0): IIndex {
184
		return $this->getIndexService()->createIndex($providerId, $documentId, $userId, $status);
185
	}
186
187
188
	/**
189
	 * @param string $providerId
190
	 * @param string $documentId
191
	 * @param int $status
192
	 * @param bool $reset
193
	 *
194
	 * @see IIndex for available value for $status.
195
	 *
196
	 * @throws FullTextSearchAppNotAvailableException
197
	 */
198
	public function updateIndexStatus(string $providerId, string $documentId, int $status, bool $reset = false) {
199
		$this->getIndexService()->updateIndexStatus($providerId, $documentId, $status, $reset);
200
	}
201
202
	/**
203
	 * @param string $providerId
204
	 * @param array $documentIds
205
	 * @param int $status
206
	 * @param bool $reset
207
	 *
208
	 * @see IIndex for available value for $status.
209
	 *
210
	 * @throws FullTextSearchAppNotAvailableException
211
	 */
212
	public function updateIndexesStatus(string $providerId, array $documentIds, int $status, bool $reset = false) {
213
		$this->getIndexService()->updateIndexesStatus($providerId, $documentIds, $status, $reset);
214
	}
215
216
217
	/**
218
	 * @param IIndex[] $indexes
219
	 *
220
	 * @throws FullTextSearchAppNotAvailableException
221
	 */
222
	public function updateIndexes(array $indexes) {
223
		$this->getIndexService()->updateIndexes($indexes);
224
	}
225
226
227
	/**
228
	 * @param array $request
229
	 * @param string $userId
230
	 *
231
	 * @return ISearchResult[]
232
	 * @throws FullTextSearchAppNotAvailableException
233
	 */
234
	public function search(array $request, string $userId = ''): array {
235
		$searchRequest = $this->getSearchService()->generateSearchRequest($request);
236
237
		return $this->getSearchService()->search($userId, $searchRequest);
238
	}
239
240
241
}
242
243