Completed
Pull Request — master (#22)
by Sergey
14:20
created

ElasticsearchRepository::find()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
rs 9.3142
cc 2
eloc 17
nc 2
nop 3
1
<?php
2
3
namespace Isswp101\Persimmon\Repository;
4
5
use Elasticsearch\Client;
6
use Elasticsearch\Common\Exceptions\Missing404Exception;
7
use Isswp101\Persimmon\Collection\ElasticsearchCollection;
8
use Isswp101\Persimmon\Collection\ICollection;
9
use Isswp101\Persimmon\CollectionParser\ElasticsearchCollectionParser;
10
use Isswp101\Persimmon\Contracts\Storable;
11
use Isswp101\Persimmon\Exceptions\ClassTypeErrorException;
12
use Isswp101\Persimmon\Exceptions\ModelNotFoundException;
13
use Isswp101\Persimmon\Model\IElasticsearchModel;
14
use Isswp101\Persimmon\QueryBuilder\IQueryBuilder;
15
use Isswp101\Persimmon\Relationship\RelationshipKey;
16
use Isswp101\Persimmon\Response\ElasticsearchCollectionResponse;
17
use Isswp101\Persimmon\Response\ElasticsearchItemResponse;
18
19
class ElasticsearchRepository implements IRepository
20
{
21
    private $client;
22
23
    public function __construct(Client $client)
24
    {
25
        $this->client = $client;
26
    }
27
28
    public function instantiate(string $class): Storable
29
    {
30
        $instance = new $class;
31
        if (!$instance instanceof IElasticsearchModel) {
32
            throw new ClassTypeErrorException(IElasticsearchModel::class);
33
        }
34
        return $instance;
35
    }
36
37
    protected function fill(Storable $model, ElasticsearchItemResponse $response)
38
    {
39
        $relationshipKey = new RelationshipKey($response->id(), $response->parent());
40
        $model->setPrimaryKey($relationshipKey->build());
41
        $model->fill($response->source());
42
    }
43
44
    public function find(string $id, string $class, array $columns = []): ?Storable
45
    {
46
        var_dump('--- --- --- Find() called | Columns=' . json_encode($columns));
0 ignored issues
show
Security Debugging Code introduced by
var_dump('--- --- --- Fi...json_encode($columns)); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
47
        $model = $this->instantiate($class);
48
        $relationshipKey = RelationshipKey::parse($id);
49
        $collection = new ElasticsearchCollectionParser($model->getCollection());
50
        $params = [
51
            'index' => $collection->getIndex(),
52
            'type' => $collection->getType(),
53
            'id' => $relationshipKey->getId(),
54
            'parent' => $relationshipKey->getParentId(),
55
            '_source' => $columns
56
        ];
57
        try {
58
            $response = new ElasticsearchItemResponse($this->client->get($params));
0 ignored issues
show
Documentation introduced by
$this->client->get($params) is of type callable, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
        } catch (Missing404Exception $e) {
60
            throw new ModelNotFoundException($class, $id);
61
        }
62
        $this->fill($model, $response);
63
        return $model;
64
    }
65
66
    public function all(IQueryBuilder $query, string $class, callable $callback = null): ICollection
67
    {
68
        $model = $this->instantiate($class);
69
        $collection = new ElasticsearchCollectionParser($model->getCollection());
70
        $params = [
71
            'index' => $collection->getIndex(),
72
            'type' => $collection->getType(),
73
            'body' => $query->build()
74
        ];
75
        $response = new ElasticsearchCollectionResponse($this->client->search($params));
0 ignored issues
show
Documentation introduced by
$this->client->search($params) is of type callable, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
        $models = new ElasticsearchCollection([], $response);
77
        foreach ($response->hits() as $hit) {
78
            $model = $this->instantiate($class);
79
            $this->fill($model, new ElasticsearchItemResponse($hit));
80
            if ($callback != null) {
81
                $callback($model);
82
            }
83
            $models->put($model->getPrimaryKey(), $model);
84
        }
85
        return $models;
86
    }
87
88
    public function insert(Storable $model): void
89
    {
90
        $relationshipKey = RelationshipKey::parse($model->getPrimaryKey());
91
        $collection = new ElasticsearchCollectionParser($model->getCollection());
92
        $params = [
93
            'index' => $collection->getIndex(),
94
            'type' => $collection->getType(),
95
            'id' => $relationshipKey->getId(),
96
            'parent' => $relationshipKey->getParentId(),
97
            'body' => $model->toArray()
98
        ];
99
        $this->client->index($params);
100
    }
101
102
    public function update(Storable $model): void
103
    {
104
        $relationshipKey = RelationshipKey::parse($model->getPrimaryKey());
105
        $collection = new ElasticsearchCollectionParser($model->getCollection());
106
        $params = [
107
            'index' => $collection->getIndex(),
108
            'type' => $collection->getType(),
109
            'id' => $relationshipKey->getId(),
110
            'parent' => $relationshipKey->getParentId(),
111
            'body' => $model->toArray()
112
        ];
113
        $this->client->index($params);
114
    }
115
116
    public function delete(Storable $model): void
117
    {
118
        $relationshipKey = RelationshipKey::parse($model->getPrimaryKey());
119
        $collection = new ElasticsearchCollectionParser($model->getCollection());
120
        $params = [
121
            'index' => $collection->getIndex(),
122
            'type' => $collection->getType(),
123
            'id' => $relationshipKey->getId(),
124
            'parent' => $relationshipKey->getParentId()
125
        ];
126
        $this->client->delete($params);
127
    }
128
}