Completed
Push — master ( 7bde6b...85b016 )
by Maxence
02:01
created

Index::setMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * FullTextSearch - Full text search framework for Nextcloud
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
47
	/** @var string */
48
	private $providerId;
49
50
	/** @var string */
51
	private $documentId;
52
53
	/** @var string */
54
	private $source = '';
55
56
	/** @var string */
57
	private $ownerId = '';
58
59
	/** @var int */
60
	private $status = 0;
61
62
	/** @var array */
63
	private $options = [];
64
65
	/** @var int */
66
	private $err = 0;
67
68
	/** @var string */
69
	private $message;
70
71
	/** @var int */
72
	private $lastIndex = 0;
73
74
75
	public function __construct($providerId, $documentId) {
76
		$this->providerId = $providerId;
77
		$this->documentId = $documentId;
78
	}
79
80
81
	/**
82
	 * @return string
83
	 */
84
	public function getProviderId() {
85
		return $this->providerId;
86
	}
87
88
	/**
89
	 * @return string
90
	 */
91
	public function getDocumentId() {
92
		return $this->documentId;
93
	}
94
95
96
	/**
97
	 * @param string $source
98
	 *
99
	 * @return $this
100
	 */
101
	public function setSource($source) {
102
		$this->source = $source;
103
104
		return $this;
105
	}
106
107
	/**
108
	 * @return string
109
	 */
110
	public function getSource() {
111
		return $this->source;
112
	}
113
114
115
	/**
116
	 * @param string $ownerId
117
	 *
118
	 * @return $this
119
	 */
120
	public function setOwnerId($ownerId) {
121
		$this->ownerId = $ownerId;
122
123
		return $this;
124
	}
125
126
	/**
127
	 * @return string
128
	 */
129
	public function getOwnerId() {
130
		return $this->ownerId;
131
	}
132
133
134
	/**
135
	 * @param int $status
136
	 * @param bool $reset
137
	 *
138
	 * @return $this
139
	 */
140
	public function setStatus($status, $reset = false) {
141
		if ($reset === true) {
142
			$this->status = $status;
143
		} else if (!$this->isStatus($status)) {
144
			$this->status += $status;
145
		}
146
147
		return $this;
148
	}
149
150
	/**
151
	 * @return int
152
	 */
153
	public function getStatus() {
154
		return $this->status;
155
	}
156
157
	/**
158
	 * @param int $status
159
	 *
160
	 * @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...
161
	 */
162
	public function isStatus($status) {
163
		return ((int)$status & $this->getStatus());
164
	}
165
166
	/**
167
	 * @param int $status
168
	 */
169
	public function unsetStatus($status) {
170
		if (!$this->isStatus($status)) {
171
			return;
172
		}
173
174
		$this->status -= $status;
175
	}
176
177
178
	/**
179
	 * @param string $option
180
	 * @param string|int $value
181
	 *
182
	 * @return $this
183
	 */
184
	public function addOption($option, $value) {
185
		$this->options[$option] = $value;
186
187
		return $this;
188
	}
189
190
	/**
191
	 * @param array $options
192
	 *
193
	 * @return $this
194
	 */
195
	public function setOptions($options) {
196
		$this->options = $options;
197
198
		return $this;
199
	}
200
201
	/**
202
	 * @return array
203
	 */
204
	public function getOptions() {
205
		return $this->options;
206
	}
207
208
209
	/**
210
	 * @param string $option
211
	 * @param string $default
212
	 *
213
	 * @return mixed|string
214
	 */
215
	public function getOption($option, $default = '') {
216
		if (!array_key_exists($option, $this->options)) {
217
			return $default;
218
		}
219
220
		return $this->options[$option];
221
	}
222
223
224
	/**
225
	 * @param int $err
226
	 *
227
	 * @return $this
228
	 */
229
	public function setError($err) {
230
		$this->err = $err;
231
232
		return $this;
233
	}
234
235
	/**
236
	 * @return int
237
	 */
238
	public function getError() {
239
		return $this->err;
240
	}
241
242
	/**
243
	 * @return $this
244
	 */
245
	public function incrementError() {
246
		$this->err++;
247
248
		return $this;
249
	}
250
251
252
	/**
253
	 * @return string
254
	 */
255
	public function getMessage() {
256
		return $this->message;
257
	}
258
259
	/**
260
	 * @param string $message
261
	 *
262
	 * @return Index
263
	 */
264
	public function setMessage($message) {
265
		$this->message = substr($message, 0, 3800);
266
267
		return $this;
268
	}
269
270
271
	/**
272
	 * @param int $lastIndex
273
	 *
274
	 * @return $this
275
	 */
276
	public function setLastIndex($lastIndex = -1) {
277
		if ($lastIndex === -1) {
278
			$lastIndex = time();
279
		}
280
281
		$this->lastIndex = $lastIndex;
282
283
		return $this;
284
	}
285
286
	/**
287
	 * @return int
288
	 */
289
	public function getLastIndex() {
290
		return $this->lastIndex;
291
	}
292
293
294
	/**
295
	 * @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...
296
	 */
297
	public function jsonSerialize() {
298
		return [
299
			'ownerId'    => $this->getOwnerId(),
300
			'providerId' => $this->getProviderId(),
301
			'documentId' => $this->getDocumentId(),
302
			'lastIndex'  => $this->getLastIndex(),
303
			'status'     => (int)$this->getStatus(),
304
			'options'    => $this->getOptions()
305
		];
306
	}
307
308
309
	public function __destruct() {
310
		unset($this->providerId);
311
		unset($this->documentId);
312
		unset($this->ownerId);
313
		unset($this->status);
314
		unset($this->options);
315
		unset($this->err);
316
		unset($this->message);
317
		unset($this->lastIndex);
318
	}
319
320
}