Completed
Pull Request — master (#1803)
by thomas
02:24
created

AbstractUpdateAction::getSequenceNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Elastica;
4
5
/**
6
 * Base class for things that can be sent to the update api (Document and
7
 * Script).
8
 *
9
 * @author   Nik Everett <[email protected]>
10
 */
11
class AbstractUpdateAction extends Param
12
{
13
    /**
14
     * @var Document
15
     */
16
    protected $_upsert;
17
18
    /**
19
     * Sets the id of the document.
20
     */
21
    public function setId(?string $id = null): self
22
    {
23
        return $this->setParam('_id', $id);
24
    }
25
26
    /**
27
     * Returns document id.
28
     *
29
     * @return string|null Document id
30
     */
31
    public function getId(): ?string
32
    {
33
        return $this->hasParam('_id') ? $this->getParam('_id') : null;
34
    }
35
36
    public function hasId(): bool
37
    {
38
        return null !== $this->getId();
39
    }
40
41
    /**
42
     * Sets the document index name.
43
     *
44
     * @param Index|string $index Index name
45
     */
46
    public function setIndex($index): self
47
    {
48
        if ($index instanceof Index) {
49
            $index = $index->getName();
50
        }
51
52
        return $this->setParam('_index', $index);
53
    }
54
55
    /**
56
     * Get the document index name.
57
     *
58
     * @throws \Elastica\Exception\InvalidException
59
     *
60
     * @return string Index name
61
     */
62
    public function getIndex(): string
63
    {
64
        return $this->getParam('_index');
65
    }
66
67
    /**
68
     * Sets the sequence number of a document for use with optimistic concurrency control.
69
     *
70
     * @param int $number Sequence Number
71
     *
72
     * @return $this
73
     *
74
     * @see https://www.elastic.co/guide/en/elasticsearch/reference/6.8/optimistic-concurrency-control.html
75
     */
76
    public function setSequenceNumber(int $number): self
77
    {
78
        return $this->setParam('if_seq_no', $number);
79
    }
80
81
    /**
82
     * Returns document version.
83
     *
84
     * @return int Document version
85
     */
86
    public function getSequenceNumber(): int
87
    {
88
        return $this->getParam('if_seq_no');
89
    }
90
91
    /**
92
     * @return bool
93
     */
94
    public function hasSequenceNumber(): bool
95
    {
96
        return $this->hasParam('if_seq_no');
97
    }
98
99
    /**
100
     * Sets the primary term of a document for use with optimistic concurrency control.
101
     *
102
     * @param int $term Primary Term
103
     *
104
     * @return $this
105
     *
106
     * @see https://www.elastic.co/guide/en/elasticsearch/reference/6.8/optimistic-concurrency-control.html
107
     */
108
    public function setPrimaryTerm(int $term): self
109
    {
110
        return $this->setParam('if_primary_term', $term);
111
    }
112
113
    /**
114
     * Returns document version.
115
     *
116
     * @return int Document version
117
     */
118
    public function getPrimaryTerm(): int
119
    {
120
        return $this->getParam('if_primary_term');
121
    }
122
123
    /**
124
     * @return bool
125
     */
126
    public function hasPrimaryTerm(): bool
127
    {
128
        return $this->hasParam('if_primary_term');
129
    }
130
131
    /**
132
     * Sets the version of a document.
133
     *
134
     * @param int $version Document version
135
     *
136
     * @return $this
137
     *
138
     * @see https://www.elastic.co/blog/versioning
139
     */
140
    public function setVersion(int $version): self
141
    {
142
        return $this->setParam('_version', $version);
143
    }
144
145
    /**
146
     * Returns document version.
147
     *
148
     * @return int Document version
149
     */
150
    public function getVersion(): int
151
    {
152
        return $this->getParam('_version');
153
    }
154
155
    /**
156
     * @return bool
157
     */
158
    public function hasVersion(): bool
159
    {
160
        return $this->hasParam('_version');
161
    }
162
163
    /**
164
     * Set operation type.
165
     *
166
     * @param string $opType Only accept create
167
     *
168
     * @return $this
169
     */
170
    public function setOpType(string $opType): self
171
    {
172
        return $this->setParam('op_type', $opType);
173
    }
174
175
    /**
176
     * Get operation type.
177
     *
178
     * @return string
179
     */
180
    public function getOpType(): string
181
    {
182
        return $this->getParam('op_type');
183
    }
184
185
    /**
186
     * @return bool
187
     */
188
    public function hasOpType(): bool
189
    {
190
        return $this->hasParam('op_type');
191
    }
192
193
    /**
194
     * Set routing query param.
195
     *
196
     * @param string $value routing
197
     *
198
     * @return $this
199
     */
200
    public function setRouting(string $value): self
201
    {
202
        return $this->setParam('routing', $value);
203
    }
204
205
    /**
206
     * Get routing parameter.
207
     *
208
     * @return string
209
     */
210
    public function getRouting(): string
211
    {
212
        return $this->getParam('_routing');
213
    }
214
215
    /**
216
     * @return bool
217
     */
218
    public function hasRouting(): bool
219
    {
220
        return $this->hasParam('_routing');
221
    }
222
223
    /**
224
     * @param array|string $fields
225
     *
226
     * @return $this
227
     */
228
    public function setFields($fields): self
229
    {
230
        if (\is_array($fields)) {
231
            $fields = \implode(',', $fields);
232
        }
233
234
        return $this->setParam('fields', (string) $fields);
235
    }
236
237
    /**
238
     * @return $this
239
     */
240
    public function setFieldsSource(): self
241
    {
242
        return $this->setFields('_source');
243
    }
244
245
    /**
246
     * @return string
247
     */
248
    public function getFields(): string
249
    {
250
        return $this->getParam('fields');
251
    }
252
253
    /**
254
     * @return bool
255
     */
256
    public function hasFields(): bool
257
    {
258
        return $this->hasParam('fields');
259
    }
260
261
    /**
262
     * @param int $num
263
     *
264
     * @return $this
265
     */
266
    public function setRetryOnConflict(int $num): self
267
    {
268
        return $this->setParam('retry_on_conflict', (int) $num);
269
    }
270
271
    /**
272
     * @return int
273
     */
274
    public function getRetryOnConflict(): int
275
    {
276
        return $this->getParam('retry_on_conflict');
277
    }
278
279
    /**
280
     * @return bool
281
     */
282
    public function hasRetryOnConflict(): bool
283
    {
284
        return $this->hasParam('_retry_on_conflict');
285
    }
286
287
    /**
288
     * @param bool|string $refresh
289
     *
290
     * @return $this
291
     */
292
    public function setRefresh($refresh = true)
293
    {
294
        \is_bool($refresh) && $refresh = $refresh
295
            ? Reindex::REFRESH_TRUE
296
            : Reindex::REFRESH_FALSE;
297
298
        return $this->setParam(Reindex::REFRESH, $refresh);
299
    }
300
301
    /**
302
     * @return bool|string
303
     */
304
    public function getRefresh()
305
    {
306
        $refresh = $this->getParam('refresh');
307
308
        return \in_array($refresh, [Reindex::REFRESH_TRUE, Reindex::REFRESH_FALSE])
309
            ? Reindex::REFRESH_TRUE === $refresh
310
            : $refresh;
311
    }
312
313
    /**
314
     * @return bool
315
     */
316
    public function hasRefresh(): bool
317
    {
318
        return $this->hasParam('refresh');
319
    }
320
321
    /**
322
     * @param string $timeout
323
     *
324
     * @return $this
325
     */
326
    public function setTimeout(string $timeout): self
327
    {
328
        return $this->setParam('timeout', $timeout);
329
    }
330
331
    /**
332
     * @return string
333
     */
334
    public function getTimeout(): string
335
    {
336
        return $this->getParam('timeout');
337
    }
338
339
    /**
340
     * @return bool
341
     */
342
    public function hasTimeout(): bool
343
    {
344
        return $this->hasParam('timeout');
345
    }
346
347
    /**
348
     * @param string $timeout
349
     *
350
     * @return $this
351
     */
352
    public function setConsistency(string $timeout): self
353
    {
354
        return $this->setParam('consistency', $timeout);
355
    }
356
357
    /**
358
     * @return string
359
     */
360
    public function getConsistency(): string
361
    {
362
        return $this->getParam('consistency');
363
    }
364
365
    /**
366
     * @return bool
367
     */
368
    public function hasConsistency(): bool
369
    {
370
        return $this->hasParam('consistency');
371
    }
372
373
    /**
374
     * @param string $timeout
375
     *
376
     * @return $this
377
     */
378
    public function setReplication(string $timeout): self
379
    {
380
        return $this->setParam('replication', $timeout);
381
    }
382
383
    /**
384
     * @return string
385
     */
386
    public function getReplication(): string
387
    {
388
        return $this->getParam('replication');
389
    }
390
391
    /**
392
     * @return bool
393
     */
394
    public function hasReplication(): bool
395
    {
396
        return $this->hasParam('replication');
397
    }
398
399
    /**
400
     * @param array|Document $data
401
     *
402
     * @return $this
403
     */
404
    public function setUpsert($data): self
405
    {
406
        $document = Document::create($data);
407
        $this->_upsert = $document;
0 ignored issues
show
Documentation Bug introduced by
It seems like $document of type object<self> is incompatible with the declared type object<Elastica\Document> of property $_upsert.

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...
408
409
        return $this;
410
    }
411
412
    /**
413
     * @return Document
414
     */
415
    public function getUpsert(): Document
416
    {
417
        return $this->_upsert;
418
    }
419
420
    /**
421
     * @return bool
422
     */
423
    public function hasUpsert(): bool
424
    {
425
        return null !== $this->_upsert;
426
    }
427
428
    /**
429
     * @param array $fields if empty array all options will be returned
430
     *
431
     * @return array
432
     */
433
    public function getOptions(array $fields = []): array
434
    {
435
        if (!empty($fields)) {
436
            return \array_filter(\array_intersect_key($this->getParams(), \array_flip($fields)));
437
        }
438
439
        return \array_filter($this->getParams());
440
    }
441
442
    /**
443
     * @param array $responseData
444
     *
445
     * @return $this
446
     */
447
    public function setVersionParams(array $responseData) :self
448
    {
449
        if (isset($responseData['_version'])) {
450
            $this->setVersion($responseData['_version']);
451
        }
452
453
        if (isset($data['_seq_no'])) {
0 ignored issues
show
Bug introduced by
The variable $data seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
454
            $this->setSequenceNumber($responseData['_seq_no']);
455
        }
456
457
        if (isset($data['_primary_term'])) {
458
            $this->setPrimaryTerm($responseData['_primary_term']);
459
        }
460
461
        return $this;
462
    }
463
}
464