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