IndexTrait::set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace BEdita\ElasticSearch\Model\Index;
5
6
use Cake\Datasource\EntityInterface;
7
use Cake\ORM\Exception\PersistenceFailedException;
8
use Elastica\Document;
9
use Elastica\Exception\NotFoundException;
10
11
trait IndexTrait
12
{
13
    /**
14
     * Returns the ElasticSearch connection instance for this index.
15
     *
16
     * @return \Cake\ElasticSearch\Datasource\Connection
17
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint
18
     */
19
    abstract public function getConnection();
20
21
    /**
22
     * Retrieve a document from the index.
23
     *
24
     * @param string $primaryKey Document ID.
25
     * @param array $options Array of options.
26
     * @return \Cake\Datasource\EntityInterface
27
     */
28
    abstract public function get(string $primaryKey, array $options = []): EntityInterface;
29
30
    /**
31
     * Persist a document to the index.
32
     *
33
     * @param \Cake\Datasource\EntityInterface $entity Document.
34
     * @param array $options Array of options.
35
     * @return \Cake\Datasource\EntityInterface|false
36
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint
37
     */
38
    abstract public function save(EntityInterface $entity, array $options);
39
40
    /**
41
     * Delete a document from the index.
42
     *
43
     * @param \Cake\Datasource\EntityInterface $entity Document.
44
     * @param array $options Array of options.
45
     * @return bool
46
     */
47
    abstract public function delete(EntityInterface $entity, array $options): bool;
48
49
    /**
50
     * Retrieve a document from the index if exists, or `null` on failure.
51
     *
52
     * @see \Cake\ElasticSearch\Index::get()
53
     * @param string $primaryKey Document ID.
54
     * @param array $options Array of options.
55
     * @return \Cake\Datasource\EntityInterface|null
56
     */
57
    public function getIfExists(string $primaryKey, array $options = []): EntityInterface|null
58
    {
59
        try {
60
            return $this->get($primaryKey, $options);
61
        } catch (NotFoundException) {
62
            return null;
63
        }
64
    }
65
66
    /**
67
     * Persist a document to the index, or throw an exception upon failure.
68
     *
69
     * @param \Cake\Datasource\EntityInterface $entity Document.
70
     * @param array $options
71
     * @return \Cake\Datasource\EntityInterface
72
     */
73
    public function saveOrFail(EntityInterface $entity, array $options = []): EntityInterface
74
    {
75
        if ($this->save($entity, $options) === false) {
76
            throw new PersistenceFailedException($entity, ['save']);
77
        }
78
79
        return $entity;
80
    }
81
82
    /**
83
     * Delete a document from the index, or throw an exception upon failure.
84
     *
85
     * @param \Cake\Datasource\EntityInterface $entity Document.
86
     * @param array $options Array of options.
87
     * @return void
88
     */
89
    public function deleteOrFail(EntityInterface $entity, array $options): void
90
    {
91
        if ($this->delete($entity, $options) === false) {
92
            throw new PersistenceFailedException($entity, ['delete']);
93
        }
94
    }
95
96
    /**
97
     * Update a single field on an existing document.
98
     *
99
     * @param string $primaryKey Document ID.
100
     * @param string $field Name of field to update.
101
     * @param mixed $value New value.
102
     * @return bool Success.
103
     */
104
    public function set(string $primaryKey, string $field, mixed $value): bool
105
    {
106
        $esIndex = $this->getConnection()->getIndex($this->getName());
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
        $esIndex = $this->getConnection()->getIndex($this->/** @scrutinizer ignore-call */ getName());
Loading history...
107
        $document = new Document($primaryKey, [$field => $value]);
108
        $response = $esIndex->updateDocument($document);
109
110
        return $response->isOk();
111
    }
112
}
113