Completed
Push — master ( 4541a4...2fe9dd )
by Maxence
01:49
created

Index::addOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * FullTextSearch - Full text search framework for extcloud
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\Model;
28
29
class Index implements \JsonSerializable {
30
31
	const INDEX_OK = 1;
32
	const INDEX_IGNORE = 2;
33
34
	const INDEX_META = 4;
35
	const INDEX_CONTENT = 8;
36
	const INDEX_FULL = 12;
37
	const INDEX_REMOVE = 16;
38
39
	const INDEX_DONE = 32;
40
	const INDEX_FAILED = 64;
41
42
	const ERROR_FAILED = 1;
43
	const ERROR_FAILED2 = 2;
44
	const ERROR_FAILED3 = 4;
45
46
	const ERROR_SEV_1 = 1;
47
	const ERROR_SEV_2 = 2;
48
	const ERROR_SEV_3 = 3;
49
	const ERROR_SEV_4 = 4;
50
51
52
	/** @var string */
53
	private $providerId;
54
55
	/** @var string */
56
	private $documentId;
57
58
	/** @var string */
59
	private $source = '';
60
61
	/** @var string */
62
	private $ownerId = '';
63
64
	/** @var int */
65
	private $status = 0;
66
67
	/** @var array */
68
	private $options = [];
69
70
	/** @var int */
71
	private $err = 0;
72
73
	/** @var array */
74
	private $errors = [];
75
76
	/** @var int */
77
	private $lastIndex = 0;
78
79
80
	/**
81
	 * Index constructor.
82
	 *
83
	 * @param string $providerId
84
	 * @param string $documentId
85
	 */
86
	public function __construct($providerId, $documentId) {
87
		$this->providerId = $providerId;
88
		$this->documentId = $documentId;
89
	}
90
91
92
	/**
93
	 * @return string
94
	 */
95
	public function getProviderId() {
96
		return $this->providerId;
97
	}
98
99
	/**
100
	 * @return string
101
	 */
102
	public function getDocumentId() {
103
		return $this->documentId;
104
	}
105
106
107
	/**
108
	 * @param string $source
109
	 *
110
	 * @return $this
111
	 */
112
	public function setSource($source) {
113
		$this->source = $source;
114
115
		return $this;
116
	}
117
118
	/**
119
	 * @return string
120
	 */
121
	public function getSource() {
122
		return $this->source;
123
	}
124
125
126
	/**
127
	 * @param string $ownerId
128
	 *
129
	 * @return $this
130
	 */
131
	public function setOwnerId($ownerId) {
132
		$this->ownerId = $ownerId;
133
134
		return $this;
135
	}
136
137
	/**
138
	 * @return string
139
	 */
140
	public function getOwnerId() {
141
		return $this->ownerId;
142
	}
143
144
145
	/**
146
	 * @param int $status
147
	 * @param bool $reset
148
	 *
149
	 * @return $this
150
	 */
151
	public function setStatus($status, $reset = false) {
152
		if ($reset === true) {
153
			$this->status = $status;
154
		} else if (!$this->isStatus($status)) {
155
			$this->status += $status;
156
		}
157
158
		return $this;
159
	}
160
161
	/**
162
	 * @return int
163
	 */
164
	public function getStatus() {
165
		return $this->status;
166
	}
167
168
	/**
169
	 * @param int $status
170
	 *
171
	 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
172
	 */
173
	public function isStatus($status) {
174
		return ((int)$status & $this->getStatus());
175
	}
176
177
	/**
178
	 * @param int $status
179
	 */
180
	public function unsetStatus($status) {
181
		if (!$this->isStatus($status)) {
182
			return;
183
		}
184
185
		$this->status -= $status;
186
	}
187
188
189
	/**
190
	 * @param string $option
191
	 * @param string|int $value
192
	 *
193
	 * @return $this
194
	 */
195
	public function addOption($option, $value) {
196
		$this->options[$option] = $value;
197
198
		return $this;
199
	}
200
201
	/**
202
	 * @param string $option
203
	 * @param int $value
204
	 *
205
	 * @return $this
206
	 */
207
	public function addOptionInt($option, $value) {
208
		$this->options[$option] = $value;
209
210
		return $this;
211
	}
212
213
214
	/**
215
	 * @param array $options
216
	 *
217
	 * @return $this
218
	 */
219
	public function setOptions($options) {
220
		$this->options = $options;
221
222
		return $this;
223
	}
224
225
	/**
226
	 * @return array
227
	 */
228
	public function getOptions() {
229
		return $this->options;
230
	}
231
232
233
	/**
234
	 * @param string $option
235
	 * @param string $default
236
	 *
237
	 * @return string
238
	 */
239
	public function getOption($option, $default = '') {
240
		if (!array_key_exists($option, $this->options)) {
241
			return $default;
242
		}
243
244
		return $this->options[$option];
245
	}
246
247
248
249
	/**
250
	 * @param string $option
251
	 * @param int $default
252
	 *
253
	 * @return int
254
	 */
255
	public function getOptionInt($option, $default = 0) {
256
		if (!array_key_exists($option, $this->options)) {
257
			return $default;
258
		}
259
260
		return $this->options[$option];
261
	}
262
263
	/**
264
	 * @param int $err
265
	 *
266
	 * @return $this
267
	 */
268
	public function setErrorCount($err) {
269
		$this->err = $err;
270
271
		return $this;
272
	}
273
274
	/**
275
	 * @return int
276
	 */
277
	public function getErrorCount() {
278
		return $this->err;
279
	}
280
281
	/**
282
	 * @return array
283
	 */
284
	public function getLastError() {
285
		return array_values(array_slice($this->errors, -1))[0];
286
	}
287
288
289
	/**
290
	 *
291
	 */
292
	public function resetErrors() {
293
		$this->setErrors([]);
294
		$this->setErrorCount(0);
295
	}
296
297
	/**
298
	 * @return array
299
	 */
300
	public function getErrors() {
301
		return $this->errors;
302
	}
303
304
	/**
305
	 * @param array $messages
306
	 *
307
	 * @return Index
308
	 */
309
	public function setErrors($messages) {
310
		$this->errors = $messages;
311
312
		return $this;
313
	}
314
315
316
	/**
317
	 * @param string $message
318
	 * @param string $exception
319
	 * @param int $sev
320
	 */
321
	public function addError($message, $exception = '', $sev = self::ERROR_SEV_3) {
322
		$this->errors[] = [
323
			'message'  => substr($message, 0, 1800),
324
			'exception' => $exception,
325
			'severity' => $sev
326
		];
327
328
		$this->err++;
329
	}
330
331
332
	/**
333
	 * @param int $lastIndex
334
	 *
335
	 * @return $this
336
	 */
337
	public function setLastIndex($lastIndex = -1) {
338
		if ($lastIndex === -1) {
339
			$lastIndex = time();
340
		}
341
342
		$this->lastIndex = $lastIndex;
343
344
		return $this;
345
	}
346
347
	/**
348
	 * @return int
349
	 */
350
	public function getLastIndex() {
351
		return $this->lastIndex;
352
	}
353
354
355
	/**
356
	 * @return array<string,string|integer>
0 ignored issues
show
Documentation introduced by
Should the return type not be array<string,string|integer|array>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
357
	 */
358
	public function jsonSerialize() {
359
		return [
360
			'ownerId'    => $this->getOwnerId(),
361
			'providerId' => $this->getProviderId(),
362
			'source'     => $this->getSource(),
363
			'documentId' => $this->getDocumentId(),
364
			'lastIndex'  => $this->getLastIndex(),
365
			'errors'     => $this->getErrors(),
366
			'errorCount' => $this->getErrorCount(),
367
			'status'     => (int)$this->getStatus(),
368
			'options'    => $this->getOptions()
369
		];
370
	}
371
372
373
	public function __destruct() {
374
		unset($this->providerId);
375
		unset($this->documentId);
376
		unset($this->ownerId);
377
		unset($this->status);
378
		unset($this->options);
379
		unset($this->err);
380
		unset($this->message);
381
		unset($this->lastIndex);
382
	}
383
384
}