Completed
Pull Request — master (#1803)
by thomas
03:00 queued 44s
created

AbstractUpdateAction::hasPrimaryTerm()   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 version parameters of a document for use with optimistic concurrency control.
69
     *
70
     * @param array $responseData
71
     *
72
     * @return $this
73
     */
74
    public function setVersionParams(array $responseData): self
75
    {
76
        if (isset($responseData['_version'])) {
77
            $this->setVersion($responseData['_version']);
78
        }
79
80
        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...
81
            $this->setSequenceNumber($responseData['_seq_no']);
82
        }
83
84
        if (isset($data['_primary_term'])) {
85
            $this->setPrimaryTerm($responseData['_primary_term']);
86
        }
87
88
        return $this;
89
    }
90
91
    /**
92
     * Sets the sequence number of a document for use with optimistic concurrency control.
93
     *
94
     * @param int $number Sequence Number
95
     *
96
     * @return $this
97
     *
98
     * @see https://www.elastic.co/guide/en/elasticsearch/reference/6.8/optimistic-concurrency-control.html
99
     */
100
    public function setSequenceNumber(int $number): self
101
    {
102
        return $this->setParam('if_seq_no', $number);
103
    }
104
105
    /**
106
     * Returns document version.
107
     *
108
     * @return int Document version
109
     */
110
    public function getSequenceNumber(): int
111
    {
112
        return $this->getParam('if_seq_no');
113
    }
114
115
    /**
116
     * @return bool
117
     */
118
    public function hasSequenceNumber(): bool
119
    {
120
        return $this->hasParam('if_seq_no');
121
    }
122
123
    /**
124
     * Sets the primary term of a document for use with optimistic concurrency control.
125
     *
126
     * @param int $term Primary Term
127
     *
128
     * @return $this
129
     *
130
     * @see https://www.elastic.co/guide/en/elasticsearch/reference/6.8/optimistic-concurrency-control.html
131
     */
132
    public function setPrimaryTerm(int $term): self
133
    {
134
        return $this->setParam('if_primary_term', $term);
135
    }
136
137
    /**
138
     * Returns document version.
139
     *
140
     * @return int Document version
141
     */
142
    public function getPrimaryTerm(): int
143
    {
144
        return $this->getParam('if_primary_term');
145
    }
146
147
    /**
148
     * @return bool
149
     */
150
    public function hasPrimaryTerm(): bool
151
    {
152
        return $this->hasParam('if_primary_term');
153
    }
154
155
    /**
156
     * Sets the version of a document.
157
     *
158
     * @param int $version Document version
159
     *
160
     * @return $this
161
     *
162
     * @see https://www.elastic.co/blog/versioning
163
     */
164
    public function setVersion(int $version): self
165
    {
166
        return $this->setParam('_version', $version);
167
    }
168
169
    /**
170
     * Returns document version.
171
     *
172
     * @return int Document version
173
     */
174
    public function getVersion(): int
175
    {
176
        return $this->getParam('_version');
177
    }
178
179
    /**
180
     * @return bool
181
     */
182
    public function hasVersion(): bool
183
    {
184
        return $this->hasParam('_version');
185
    }
186
187
    /**
188
     * Set operation type.
189
     *
190
     * @param string $opType Only accept create
191
     *
192
     * @return $this
193
     */
194
    public function setOpType(string $opType): self
195
    {
196
        return $this->setParam('op_type', $opType);
197
    }
198
199
    /**
200
     * Get operation type.
201
     *
202
     * @return string
203
     */
204
    public function getOpType(): string
205
    {
206
        return $this->getParam('op_type');
207
    }
208
209
    /**
210
     * @return bool
211
     */
212
    public function hasOpType(): bool
213
    {
214
        return $this->hasParam('op_type');
215
    }
216
217
    /**
218
     * Set routing query param.
219
     *
220
     * @param string $value routing
221
     *
222
     * @return $this
223
     */
224
    public function setRouting(string $value): self
225
    {
226
        return $this->setParam('routing', $value);
227
    }
228
229
    /**
230
     * Get routing parameter.
231
     *
232
     * @return string
233
     */
234
    public function getRouting(): string
235
    {
236
        return $this->getParam('_routing');
237
    }
238
239
    /**
240
     * @return bool
241
     */
242
    public function hasRouting(): bool
243
    {
244
        return $this->hasParam('_routing');
245
    }
246
247
    /**
248
     * @param array|string $fields
249
     *
250
     * @return $this
251
     */
252
    public function setFields($fields): self
253
    {
254
        if (\is_array($fields)) {
255
            $fields = \implode(',', $fields);
256
        }
257
258
        return $this->setParam('fields', (string) $fields);
259
    }
260
261
    /**
262
     * @return $this
263
     */
264
    public function setFieldsSource(): self
265
    {
266
        return $this->setFields('_source');
267
    }
268
269
    /**
270
     * @return string
271
     */
272
    public function getFields(): string
273
    {
274
        return $this->getParam('fields');
275
    }
276
277
    /**
278
     * @return bool
279
     */
280
    public function hasFields(): bool
281
    {
282
        return $this->hasParam('fields');
283
    }
284
285
    /**
286
     * @param int $num
287
     *
288
     * @return $this
289
     */
290
    public function setRetryOnConflict(int $num): self
291
    {
292
        return $this->setParam('retry_on_conflict', (int) $num);
293
    }
294
295
    /**
296
     * @return int
297
     */
298
    public function getRetryOnConflict(): int
299
    {
300
        return $this->getParam('retry_on_conflict');
301
    }
302
303
    /**
304
     * @return bool
305
     */
306
    public function hasRetryOnConflict(): bool
307
    {
308
        return $this->hasParam('_retry_on_conflict');
309
    }
310
311
    /**
312
     * @param bool|string $refresh
313
     *
314
     * @return $this
315
     */
316
    public function setRefresh($refresh = true)
317
    {
318
        \is_bool($refresh) && $refresh = $refresh
319
            ? Reindex::REFRESH_TRUE
320
            : Reindex::REFRESH_FALSE;
321
322
        return $this->setParam(Reindex::REFRESH, $refresh);
323
    }
324
325
    /**
326
     * @return bool|string
327
     */
328
    public function getRefresh()
329
    {
330
        $refresh = $this->getParam('refresh');
331
332
        return \in_array($refresh, [Reindex::REFRESH_TRUE, Reindex::REFRESH_FALSE])
333
            ? Reindex::REFRESH_TRUE === $refresh
334
            : $refresh;
335
    }
336
337
    /**
338
     * @return bool
339
     */
340
    public function hasRefresh(): bool
341
    {
342
        return $this->hasParam('refresh');
343
    }
344
345
    /**
346
     * @param string $timeout
347
     *
348
     * @return $this
349
     */
350
    public function setTimeout(string $timeout): self
351
    {
352
        return $this->setParam('timeout', $timeout);
353
    }
354
355
    /**
356
     * @return string
357
     */
358
    public function getTimeout(): string
359
    {
360
        return $this->getParam('timeout');
361
    }
362
363
    /**
364
     * @return bool
365
     */
366
    public function hasTimeout(): bool
367
    {
368
        return $this->hasParam('timeout');
369
    }
370
371
    /**
372
     * @param string $timeout
373
     *
374
     * @return $this
375
     */
376
    public function setConsistency(string $timeout): self
377
    {
378
        return $this->setParam('consistency', $timeout);
379
    }
380
381
    /**
382
     * @return string
383
     */
384
    public function getConsistency(): string
385
    {
386
        return $this->getParam('consistency');
387
    }
388
389
    /**
390
     * @return bool
391
     */
392
    public function hasConsistency(): bool
393
    {
394
        return $this->hasParam('consistency');
395
    }
396
397
    /**
398
     * @param string $timeout
399
     *
400
     * @return $this
401
     */
402
    public function setReplication(string $timeout): self
403
    {
404
        return $this->setParam('replication', $timeout);
405
    }
406
407
    /**
408
     * @return string
409
     */
410
    public function getReplication(): string
411
    {
412
        return $this->getParam('replication');
413
    }
414
415
    /**
416
     * @return bool
417
     */
418
    public function hasReplication(): bool
419
    {
420
        return $this->hasParam('replication');
421
    }
422
423
    /**
424
     * @param array|Document $data
425
     *
426
     * @return $this
427
     */
428
    public function setUpsert($data): self
429
    {
430
        $document = Document::create($data);
431
        $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...
432
433
        return $this;
434
    }
435
436
    /**
437
     * @return Document
438
     */
439
    public function getUpsert(): Document
440
    {
441
        return $this->_upsert;
442
    }
443
444
    /**
445
     * @return bool
446
     */
447
    public function hasUpsert(): bool
448
    {
449
        return null !== $this->_upsert;
450
    }
451
452
    /**
453
     * @param array $fields if empty array all options will be returned
454
     *
455
     * @return array
456
     */
457
    public function getOptions(array $fields = []): array
458
    {
459
        if (!empty($fields)) {
460
            return \array_filter(\array_intersect_key($this->getParams(), \array_flip($fields)));
461
        }
462
463
        return \array_filter($this->getParams());
464
    }
465
}
466