DeckProvider::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
5
/**
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Maxence Lange <[email protected]>
10
 * @copyright 2019
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
29
namespace OCA\Deck\Provider;
30
31
32
use OC\FullTextSearch\Model\IndexDocument;
33
use OC\FullTextSearch\Model\SearchTemplate;
34
use OCA\Deck\Service\FullTextSearchService;
35
use OCP\AppFramework\Db\DoesNotExistException;
36
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
37
use OCP\FullTextSearch\IFullTextSearchPlatform;
38
use OCP\FullTextSearch\IFullTextSearchProvider;
39
use OCP\FullTextSearch\Model\IIndex;
40
use OCP\FullTextSearch\Model\IIndexDocument;
41
use OCP\FullTextSearch\Model\IIndexOptions;
42
use OCP\FullTextSearch\Model\IRunner;
43
use OCP\FullTextSearch\Model\ISearchRequest;
44
use OCP\FullTextSearch\Model\ISearchResult;
45
use OCP\FullTextSearch\Model\ISearchTemplate;
46
use OCP\IL10N;
47
use OCP\IURLGenerator;
48
49
50
/**
51
 * Class DeckProvider
52
 *
53
 * @package OCA\Deck\Provider
54
 */
55
class DeckProvider implements IFullTextSearchProvider {
56
57
58
	const DECK_PROVIDER_ID = 'deck';
59
60
61
	/** @var IL10N */
62
	private $l10n;
63
64
	/** @var IUrlGenerator */
65
	private $urlGenerator;
66
67
	/** @var FullTextSearchService */
68
	private $fullTextSearchService;
69
70
71
	/** @var IRunner */
72
	private $runner;
73
74
	/** @var IIndexOptions */
75
	private $indexOptions = [];
76
77
78
	/**
79
	 * DeckProvider constructor.
80
	 *
81
	 * @param IL10N $l10n
82
	 * @param IUrlGenerator $urlGenerator
83
	 * @param FullTextSearchService $fullTextSearchService
84
	 */
85
	public function __construct(
86
		IL10N $l10n, IUrlGenerator $urlGenerator, FullTextSearchService $fullTextSearchService
87
	) {
88
		$this->l10n = $l10n;
89
		$this->urlGenerator = $urlGenerator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $urlGenerator of type object<OCP\IURLGenerator> is incompatible with the declared type object<OCA\Deck\Provider\IUrlGenerator> of property $urlGenerator.

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...
90
		$this->fullTextSearchService = $fullTextSearchService;
91
	}
92
93
94
	/**
95
	 * return unique id of the provider
96
	 */
97
	public function getId(): string {
98
		return self::DECK_PROVIDER_ID;
99
	}
100
101
102
	/**
103
	 * return name of the provider
104
	 */
105
	public function getName(): string {
106
		return 'Deck';
107
	}
108
109
110
	/**
111
	 * @return array
112
	 */
113
	public function getConfiguration(): array {
114
		return [];
115
	}
116
117
118
	/**
119
	 * @param IRunner $runner
120
	 */
121
	public function setRunner(IRunner $runner) {
122
		$this->runner = $runner;
123
	}
124
125
126
	/**
127
	 * @param IIndexOptions $options
128
	 */
129
	public function setIndexOptions(IIndexOptions $options) {
130
		$this->indexOptions = $options;
131
	}
132
133
134
	/**
135
	 * @return ISearchTemplate
136
	 */
137
	public function getSearchTemplate(): ISearchTemplate {
138
		$template = new SearchTemplate('icon-deck', 'icons');
139
140
		return $template;
141
	}
142
143
144
	/**
145
	 *
146
	 */
147
	public function loadProvider() {
148
	}
149
150
151
152
	/**
153
	 * @param string $userId
154
	 *
155
	 * @return string[]
156
	 */
157
	public function generateChunks(string $userId): array {
158
		return [];
159
	}
160
161
162
	/**
163
	 * @param string $userId
164
	 * @param string $chunk
165
	 *
166
	 * @return IIndexDocument[]
167
	 */
168
	public function generateIndexableDocuments(string $userId, string $chunk): array {
169
		$cards = $this->fullTextSearchService->getCardsFromUser($userId);
170
171
		$documents = [];
172
		foreach ($cards as $card) {
173
			$documents[] = $this->fullTextSearchService->generateIndexDocumentFromCard($card);
174
		}
175
176
		return $documents;
177
	}
178
179
180
	/**
181
	 * @param IIndexDocument $document
182
	 *
183
	 * @throws DoesNotExistException
184
	 * @throws MultipleObjectsReturnedException
185
	 */
186
	public function fillIndexDocument(IIndexDocument $document) {
187
		$this->fullTextSearchService->fillIndexDocument($document);
188
		$this->updateRunnerInfo('info', $document->getTitle());
189
	}
190
191
192
	/**
193
	 * @param IIndexDocument $document
194
	 *
195
	 * @return bool
196
	 */
197
	public function isDocumentUpToDate(IIndexDocument $document): bool {
198
		return false;
199
	}
200
201
202
	/**
203
	 * @param IIndex $index
204
	 *
205
	 * @return IIndexDocument
206
	 * @throws DoesNotExistException
207
	 * @throws MultipleObjectsReturnedException
208
	 */
209
	public function updateDocument(IIndex $index): IIndexDocument {
210
		$document = new IndexDocument(DeckProvider::DECK_PROVIDER_ID, $index->getDocumentId());
211
		$document->setIndex($index);
212
213
		$this->fullTextSearchService->fillIndexDocument($document);
214
215
		return $document;
216
	}
217
218
219
	/**
220
	 * @param IFullTextSearchPlatform $platform
221
	 */
222
	public function onInitializingIndex(IFullTextSearchPlatform $platform) {
223
	}
224
225
226
	/**
227
	 * @param IFullTextSearchPlatform $platform
228
	 */
229
	public function onResettingIndex(IFullTextSearchPlatform $platform) {
230
	}
231
232
233
	/**
234
	 * not used yet
235
	 */
236
	public function unloadProvider() {
237
	}
238
239
240
	/**
241
	 * before a search, improve the request
242
	 *
243
	 * @param ISearchRequest $request
244
	 */
245
	public function improveSearchRequest(ISearchRequest $request) {
246
	}
247
248
249
	/**
250
	 * after a search, improve results
251
	 *
252
	 * @param ISearchResult $searchResult
253
	 */
254
	public function improveSearchResult(ISearchResult $searchResult) {
255
		foreach ($searchResult->getDocuments() as $document) {
256
			try {
257
				$board =
258
					$this->fullTextSearchService->getBoardFromCardId((int)$document->getId());
259
				$path = '#!/board/' . $board->getId() . '//card/' . $document->getId();
260
				$document->setLink($this->urlGenerator->linkToRoute('deck.page.index') . $path);
261
			} catch (DoesNotExistException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
262
			} catch (MultipleObjectsReturnedException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
263
			}
264
		}
265
	}
266
267
268
	/**
269
	 * @param string $info
270
	 * @param string $value
271
	 */
272
	private function updateRunnerInfo(string $info, string $value) {
273
		if ($this->runner === null) {
274
			return;
275
		}
276
277
		$this->runner->setInfo($info, $value);
278
	}
279
280
}
281
282