Persistence::delete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Isswp101\Persimmon\Persistence;
4
5
use Elasticsearch\Client;
6
use Exception;
7
use Isswp101\Persimmon\Contracts\PersistenceContract;
8
use Isswp101\Persimmon\DTO\Id;
9
use Isswp101\Persimmon\DTO\Path;
10
use Isswp101\Persimmon\DTO\SearchResponse;
11
12
final class Persistence implements PersistenceContract
13
{
14
    private Client $client;
15
16
    public function __construct(Client $client)
17
    {
18
        $this->client = $client;
19
    }
20
21
    public function find(Path $path, array $columns = []): array|null
0 ignored issues
show
Bug introduced by
The type Isswp101\Persimmon\Persistence\null was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
    {
23
        $params = [
24
            'index' => $path->getIndex(),
25
            'type' => $path->getType(),
26
            'id' => $path->getId()->value()
27
        ];
28
29
        if ($columns) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $columns of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
30
            $params['_source'] = $columns;
31
        }
32
33
        try {
34
            $response = $this->client->get($params);
35
        } catch (Exception) {
36
            return null;
37
        }
38
39
        return $response['_source'];
40
    }
41
42
    public function create(Path $path, array $attributes): Id
43
    {
44
        $params = [
45
            'index' => $path->getIndex(),
46
            'type' => $path->getType(),
47
            'id' => $path->getId()->value(),
48
            'body' => $attributes
49
        ];
50
51
        $response = $this->client->index($params);
52
53
        return new Id($response['_id']);
54
    }
55
56
    public function update(Path $path, array $attributes): Id
57
    {
58
        $params = [
59
            'index' => $path->getIndex(),
60
            'type' => $path->getType(),
61
            'id' => $path->getId()->value(),
62
            'body' => [
63
                'doc' => $attributes
64
            ]
65
        ];
66
67
        $this->client->update($params);
68
69
        return $path->getId();
70
    }
71
72
    public function delete(Path $path): void
73
    {
74
        $params = [
75
            'index' => $path->getIndex(),
76
            'type' => $path->getType(),
77
            'id' => $path->getId()->value(),
78
        ];
79
80
        $this->client->delete($params);
81
    }
82
83
    public function search(Path $path, array $query): SearchResponse
84
    {
85
        $params = [
86
            'index' => $path->getIndex(),
87
            'type' => $path->getType(),
88
            'body' => $query
89
        ];
90
91
        return new SearchResponse($this->client->search($params));
92
    }
93
}
94