Completed
Push — master ( c218d3...33ae5d )
by Maxence
02:12
created

Index::setLastIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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