Passed
Pull Request — main (#3)
by Paolo
14:04 queued 10s
created

IndexTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 92
rs 10
c 1
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteOrFail() 0 4 2
A saveOrFail() 0 7 2
A getIfExists() 0 6 2
A set() 0 7 1
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
     * Retrieve a document from the index.
15
     *
16
     * @param string $primaryKey Document ID.
17
     * @param array $options Array of options.
18
     * @return \Cake\Datasource\EntityInterface
19
     */
20
    abstract public function get(string $primaryKey, array $options = []): EntityInterface;
21
22
    /**
23
     * Persist a document to the index.
24
     *
25
     * @param \Cake\Datasource\EntityInterface $entity Document.
26
     * @param array $options Array of options.
27
     * @return \Cake\Datasource\EntityInterface|false
28
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint
29
     */
30
    abstract public function save(EntityInterface $entity, array $options);
31
32
    /**
33
     * Delete a document from the index.
34
     *
35
     * @param \Cake\Datasource\EntityInterface $entity Document.
36
     * @param array $options Array of options.
37
     * @return bool
38
     */
39
    abstract public function delete(EntityInterface $entity, array $options): bool;
40
41
    /**
42
     * Retrieve a document from the index if exists, or `null` on failure.
43
     *
44
     * @see \Cake\ElasticSearch\Index::get()
45
     * @param string $primaryKey Document ID.
46
     * @param array $options Array of options.
47
     * @return \Cake\Datasource\EntityInterface|null
48
     */
49
    public function getIfExists(string $primaryKey, array $options = []): EntityInterface|null
50
    {
51
        try {
52
            return $this->get($primaryKey, $options);
53
        } catch (NotFoundException) {
54
            return null;
55
        }
56
    }
57
58
    /**
59
     * Persist a document to the index, or throw an exception upon failure.
60
     *
61
     * @param \Cake\Datasource\EntityInterface $entity Document.
62
     * @param array $options
63
     * @return \Cake\Datasource\EntityInterface
64
     */
65
    public function saveOrFail(EntityInterface $entity, array $options = []): EntityInterface
66
    {
67
        if ($this->save($entity, $options) === false) {
68
            throw new PersistenceFailedException($entity, ['save']);
69
        }
70
71
        return $entity;
72
    }
73
74
    /**
75
     * Delete a document from the index, or throw an exception upon failure.
76
     *
77
     * @param \Cake\Datasource\EntityInterface $entity Document.
78
     * @param array $options Array of options.
79
     * @return void
80
     */
81
    public function deleteOrFail(EntityInterface $entity, array $options): void
82
    {
83
        if ($this->delete($entity, $options) === false) {
84
            throw new PersistenceFailedException($entity, ['delete']);
85
        }
86
    }
87
88
    /**
89
     * Update a single field on an existing document.
90
     *
91
     * @param string $primaryKey Document ID.
92
     * @param string $field Name of field to update.
93
     * @param mixed $value New value.
94
     * @return bool Success.
95
     */
96
    public function set(string $primaryKey, string $field, mixed $value): bool
97
    {
98
        $esIndex = $this->getConnection()->getIndex($this->getName());
0 ignored issues
show
Bug introduced by
It seems like getConnection() 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

98
        $esIndex = $this->/** @scrutinizer ignore-call */ getConnection()->getIndex($this->getName());
Loading history...
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

98
        $esIndex = $this->getConnection()->getIndex($this->/** @scrutinizer ignore-call */ getName());
Loading history...
99
        $document = new Document($primaryKey, [$field => $value]);
100
        $response = $esIndex->updateDocument($document);
101
102
        return $response->isOk();
103
    }
104
}
105