Passed
Push — master ( 64ec5e...ddd39f )
by Roeland
28:49 queued 15:55
created

FullTextSearchManager   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 175
rs 10
c 0
b 0
f 0
wmc 17

14 Methods

Rating   Name   Duplication   Size   Complexity  
A createIndex() 0 2 1
A getProviderService() 0 6 2
A registerProviderService() 0 2 1
A updateIndexesStatus() 0 2 1
A getSearchService() 0 6 2
A getIndexService() 0 6 2
A addJavascriptAPI() 0 2 1
A registerSearchService() 0 2 1
A getIndex() 0 2 1
A registerIndexService() 0 2 1
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
	/**
90
	 * @return IProviderService
91
	 * @throws FullTextSearchAppNotAvailableException
92
	 */
93
	private function getProviderService(): IProviderService {
94
		if ($this->providerService === null) {
95
			throw new FullTextSearchAppNotAvailableException('No IProviderService registered');
96
		}
97
98
		return $this->providerService;
99
	}
100
101
102
	/**
103
	 * @return IIndexService
104
	 * @throws FullTextSearchAppNotAvailableException
105
	 */
106
	private function getIndexService(): IIndexService {
107
		if ($this->indexService === null) {
108
			throw new FullTextSearchAppNotAvailableException('No IIndexService registered');
109
		}
110
111
		return $this->indexService;
112
	}
113
114
115
	/**
116
	 * @return ISearchService
117
	 * @throws FullTextSearchAppNotAvailableException
118
	 */
119
	private function getSearchService(): ISearchService {
120
		if ($this->searchService === null) {
121
			throw new FullTextSearchAppNotAvailableException('No ISearchService registered');
122
		}
123
124
		return $this->searchService;
125
	}
126
127
128
	/**
129
	 * @throws FullTextSearchAppNotAvailableException
130
	 */
131
	public function addJavascriptAPI() {
132
		$this->getProviderService()->addJavascriptAPI();
133
	}
134
135
136
	/**
137
	 * @param string $providerId
138
	 *
139
	 * @return bool
140
	 * @throws FullTextSearchAppNotAvailableException
141
	 */
142
	public function isProviderIndexed(string $providerId): bool {
143
		return $this->getProviderService()->isProviderIndexed($providerId);
144
	}
145
146
147
	/**
148
	 * @param string $providerId
149
	 * @param string $documentId
150
	 * @return IIndex
151
	 * @throws FullTextSearchAppNotAvailableException
152
	 */
153
	public function getIndex(string $providerId, string $documentId): IIndex {
154
		return $this->getIndexService()->getIndex($providerId, $documentId);
155
	}
156
157
	/**
158
	 * @param string $providerId
159
	 * @param string $documentId
160
	 * @param string $userId
161
	 * @param int $status
162
	 *
163
	 * @see IIndex for available value for $status.
164
	 *
165
	 * @return IIndex
166
	 * @throws FullTextSearchAppNotAvailableException
167
	 */
168
	public function createIndex(string $providerId, string $documentId, string $userId, int $status = 0): IIndex {
169
		return $this->getIndexService()->getIndex($providerId, $documentId);
170
	}
171
172
173
	/**
174
	 * @param string $providerId
175
	 * @param string $documentId
176
	 * @param int $status
177
	 * @param bool $reset
178
	 *
179
	 * @see IIndex for available value for $status.
180
	 *
181
	 * @throws FullTextSearchAppNotAvailableException
182
	 */
183
	public function updateIndexStatus(string $providerId, string $documentId, int $status, bool $reset = false) {
184
		$this->getIndexService()->updateIndexStatus($providerId, $documentId, $status, $reset);
185
	}
186
187
	/**
188
	 * @param string $providerId
189
	 * @param array $documentIds
190
	 * @param int $status
191
	 * @param bool $reset
192
	 *
193
	 * @see IIndex for available value for $status.
194
	 *
195
	 * @throws FullTextSearchAppNotAvailableException
196
	 */
197
	public function updateIndexesStatus(string $providerId, array $documentIds, int $status, bool $reset = false) {
198
		$this->getIndexService()->updateIndexStatus($providerId, $documentIds, $status, $reset);
199
	}
200
201
202
	/**
203
	 * @param IIndex[] $indexes
204
	 *
205
	 * @throws FullTextSearchAppNotAvailableException
206
	 */
207
	public function updateIndexes(array $indexes) {
208
		$this->getIndexService()->updateIndexes($indexes);
209
	}
210
211
212
	/**
213
	 * @param array $request
214
	 * @param string $userId
215
	 *
216
	 * @return ISearchResult[]
217
	 * @throws FullTextSearchAppNotAvailableException
218
	 */
219
	public function search(array $request, string $userId = ''): array {
220
		$searchRequest = $this->getSearchService()->generateSearchRequest($request);
221
222
		return $this->getSearchService()->search($userId, $searchRequest);
223
	}
224
225
226
}
227
228