Completed
Push — master ( 347a51...ad246f )
by Maxence
02:23
created

IndexesRequest::updateStatuses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
c 0
b 0
f 0
rs 9.9666
nc 1
cc 1
nop 3
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
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 OCA\FullTextSearch\Db;
32
33
34
use OCA\FullTextSearch\Exceptions\IndexDoesNotExistException;
35
use OCA\FullTextSearch\Model\Index;
36
use OCP\FullTextSearch\Model\IIndex;
37
38
39
/**
40
 * Class IndexesRequest
41
 *
42
 * @package OCA\FullTextSearch\Db
43
 */
44
class IndexesRequest extends IndexesRequestBuilder {
45
46
47
	/**
48
	 * @param Index $index
49
	 *
50
	 * @return bool
51
	 * @throws \Exception
52
	 */
53
	public function create(Index $index): bool {
54
55
		try {
56
			$qb = $this->getIndexesInsertSql();
57
			$qb->setValue('owner_id', $qb->createNamedParameter($index->getOwnerId()))
58
			   ->setValue('provider_id', $qb->createNamedParameter($index->getProviderId()))
59
			   ->setValue('document_id', $qb->createNamedParameter($index->getDocumentId()))
60
			   ->setValue('source', $qb->createNamedParameter($index->getSource()))
61
			   ->setValue('err', $qb->createNamedParameter($index->getErrorCount()))
62
			   ->setValue('message', $qb->createNamedParameter(json_encode($index->getErrors())))
63
			   ->setValue('status', $qb->createNamedParameter($index->getStatus()))
64
			   ->setValue('options', $qb->createNamedParameter(json_encode($index->getOptions())))
65
			   ->setValue('indexed', $qb->createNamedParameter($index->getLastIndex()));
66
67
			$qb->execute();
68
69
			return true;
70
		} catch (\Exception $e) {
71
			throw $e;
72
		}
73
	}
74
75
76
	/**
77
	 * @param Index $index
78
	 *
79
	 * @return bool
80
	 */
81
	public function resetError(Index $index): bool {
82
83
		try {
84
			$this->getIndex($index->getProviderId(), $index->getDocumentId());
85
		} catch (IndexDoesNotExistException $e) {
86
			return false;
87
		}
88
89
		$qb = $this->getIndexesUpdateSql();
90
		$qb->set('message', $qb->createNamedParameter(json_encode([])));
91
		$qb->set('err', $qb->createNamedParameter(0));
92
93
		$this->limitToProviderId($qb, $index->getProviderId());
94
		$this->limitToDocumentId($qb, $index->getDocumentId());
95
96
		$qb->execute();
97
98
		return true;
99
	}
100
101
102
	/**
103
	 *
104
	 */
105
	public function resetAllErrors() {
106
		$qb = $this->getIndexesUpdateSql();
107
		$qb->set('message', $qb->createNamedParameter(json_encode([])));
108
		$qb->set('err', $qb->createNamedParameter(0));
109
110
		$qb->execute();
111
	}
112
113
114
	/**
115
	 * @return Index[]
116
	 */
117 View Code Duplication
	public function getErrorIndexes(): array {
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...
118
119
		$qb = $this->getIndexesSelectSql();
120
		$this->limitToErr($qb);
121
122
		$indexes = [];
123
		$cursor = $qb->execute();
124
		while ($data = $cursor->fetch()) {
125
			$indexes[] = $this->parseIndexesSelectSql($data);
126
		}
127
		$cursor->closeCursor();
128
129
		return $indexes;
130
	}
131
132
133
	/**
134
	 * @param Index $index
135
	 *
136
	 * @return bool
137
	 */
138
	public function update(Index $index): bool {
139
140
		try {
141
			$this->getIndex($index->getProviderId(), $index->getDocumentId());
142
		} catch (IndexDoesNotExistException $e) {
143
			return false;
144
		}
145
146
		$qb = $this->getIndexesUpdateSql();
147
		$qb->set('status', $qb->createNamedParameter($index->getStatus()));
148
		$qb->set('source', $qb->createNamedParameter($index->getSource()));
149
		$qb->set('options', $qb->createNamedParameter(json_encode($index->getOptions())));
150
151
		if ($index->getOwnerId() !== '') {
152
			$qb->set('owner_id', $qb->createNamedParameter($index->getOwnerId()));
153
		}
154
155
		if ($index->getLastIndex() > 0) {
156
			$qb->set('indexed', $qb->createNamedParameter($index->getLastIndex()));
157
		}
158
159
		$qb->set('message', $qb->createNamedParameter(json_encode($index->getErrors())));
160
		$qb->set('err', $qb->createNamedParameter($index->getErrorCount()));
161
162
		$this->limitToProviderId($qb, $index->getProviderId());
163
		$this->limitToDocumentId($qb, $index->getDocumentId());
164
165
		$qb->execute();
166
167
		return true;
168
	}
169
170
171
	/**
172
	 * @param string $providerId
173
	 * @param string $documentId
174
	 * @param int $status
175
	 */
176 View Code Duplication
	public function updateStatus(string $providerId, string $documentId, int $status) {
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...
177
		$qb = $this->getIndexesUpdateSql();
178
		$qb->set('status', $qb->createNamedParameter($status));
179
180
		$this->limitToProviderId($qb, $providerId);
181
		$this->limitToDocumentId($qb, $documentId);
182
183
		$qb->execute();
184
	}
185
186
	/**
187
	 * @param string $providerId
188
	 * @param array $indexes
189
	 * @param int $status
190
	 */
191 View Code Duplication
	public function updateStatuses(string $providerId, array $indexes, int $status) {
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...
192
		$qb = $this->getIndexesUpdateSql();
193
		$qb->set('status', $qb->createNamedParameter($status));
194
195
		$this->limitToProviderId($qb, $providerId);
196
		$this->limitToDocumentIds($qb, $indexes);
197
198
		$qb->execute();
199
	}
200
201
202
	/**
203
	 * @param IIndex $index
204
	 */
205
	public function deleteIndex(IIndex $index) {
206
		$qb = $this->getIndexesDeleteSql();
207
		$this->limitToProviderId($qb, $index->getProviderId());
208
		$this->limitToDocumentId($qb, $index->getDocumentId());
209
210
		$qb->execute();
211
	}
212
213
214
	/**
215
	 * @param string $providerId
216
	 */
217
	public function deleteFromProviderId(string $providerId) {
218
		$qb = $this->getIndexesDeleteSql();
219
		$this->limitToProviderId($qb, $providerId);
220
221
		$qb->execute();
222
	}
223
224
225
	/**
226
	 *
227
	 */
228
	public function reset() {
229
		$qb = $this->getIndexesDeleteSql();
230
231
		$qb->execute();
232
	}
233
234
235
	/**
236
	 * return index.
237
	 *
238
	 * @param string $providerId
239
	 * @param string $documentId
240
	 *
241
	 * @return Index
242
	 * @throws IndexDoesNotExistException
243
	 */
244 View Code Duplication
	public function getIndex(string $providerId, string $documentId): Index {
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...
245
		$qb = $this->getIndexesSelectSql();
246
		$this->limitToProviderId($qb, $providerId);
247
		$this->limitToDocumentId($qb, $documentId);
248
249
		$cursor = $qb->execute();
250
		$data = $cursor->fetch();
251
		$cursor->closeCursor();
252
253
		if ($data === false) {
254
			throw new IndexDoesNotExistException($this->l10n->t('Index not found'));
255
		}
256
257
		return $this->parseIndexesSelectSql($data);
258
	}
259
260
261
	/**
262
	 * return index.
263
	 *
264
	 * @param string $providerId
265
	 * @param array $documentIds
266
	 *
267
	 * @return Index[]
268
	 * @throws IndexDoesNotExistException
269
	 */
270
	public function getIndexes(string $providerId, array $documentIds): array {
271
		$qb = $this->getIndexesSelectSql();
272
		$this->limitToProviderId($qb, $providerId);
273
		$this->limitToDocumentIds($qb, $documentIds);
274
275
		$indexes = [];
276
		$cursor = $qb->execute();
277
		while ($data = $cursor->fetch()) {
278
			$indexes[] = $this->parseIndexesSelectSql($data);
279
		}
280
		$cursor->closeCursor();
281
282
		if (sizeof($indexes) === 0) {
283
			throw new IndexDoesNotExistException($this->l10n->t('Index not found'));
284
		}
285
286
		return $indexes;
287
	}
288
289
290
	/**
291
	 * @param bool $all
292
	 *
293
	 * @return Index[]
294
	 */
295
	public function getQueuedIndexes(bool $all = false): array {
296
		$qb = $this->getIndexesSelectSql();
297
		$this->limitToQueuedIndexes($qb);
298
		if ($all === false) {
299
			$this->limitToNoErr($qb);
300
		}
301
302
		$indexes = [];
303
		$cursor = $qb->execute();
304
		while ($data = $cursor->fetch()) {
305
			$indexes[] = $this->parseIndexesSelectSql($data);
306
		}
307
		$cursor->closeCursor();
308
309
		return $indexes;
310
	}
311
312
313
	/**
314
	 * return list of last indexes from a providerId.
315
	 *
316
	 * @param string $providerId
317
	 *
318
	 * @return Index[]
319
	 */
320 View Code Duplication
	public function getIndexesFromProvider(string $providerId): array {
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...
321
		$qb = $this->getIndexesSelectSql();
322
		$this->limitToProviderId($qb, $providerId);
323
324
		$indexes = [];
325
		$cursor = $qb->execute();
326
		while ($data = $cursor->fetch()) {
327
			$indexes[] = $this->parseIndexesSelectSql($data);
328
		}
329
		$cursor->closeCursor();
330
331
		return $indexes;
332
	}
333
334
335
}
336