Completed
Pull Request — master (#22)
by Sergey
23:00 queued 08:02
created

ElasticsearchRepository::all()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 17
nc 3
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\IElasticsearchCollection;
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(
38
        Storable $model,
39
        ElasticsearchItemResponse $response,
40
        RelationshipKey $relationshipKey = null
41
    ) {
42
        $model->fill($response->source());
43
        $model->setPrimaryKey($relationshipKey != null ? $relationshipKey->build() : $response->id());
44
    }
45
46
    public function find($id, string $class, array $columns = []): Storable
47
    {
48
        $model = $this->instantiate($class);
49
        $relationshipKey = RelationshipKey::parse($id);
50
        $collection = new ElasticsearchCollectionParser($model->getCollection());
51
        $params = [
52
            'index' => $collection->getIndex(),
53
            'type' => $collection->getType(),
54
            'id' => $relationshipKey->getId(),
55
            'parent' => $relationshipKey->getParentId(),
56
            '_source' => $columns
57
        ];
58
        try {
59
            $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...
60
        } catch (Missing404Exception $e) {
61
            throw new ModelNotFoundException($class, $id);
62
        }
63
        $this->fill($model, $response, $relationshipKey);
64
        return $model;
65
    }
66
67
    public function all(IQueryBuilder $query, string $class, callable $callback = null): IElasticsearchCollection
68
    {
69
        $model = $this->instantiate($class);
70
        $collection = new ElasticsearchCollectionParser($model->getCollection());
71
        $params = [
72
            'index' => $collection->getIndex(),
73
            'type' => $collection->getType(),
74
            'body' => $query->build()
75
        ];
76
        $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...
77
        $models = new ElasticsearchCollection([], $response);
78
        foreach ($response->hits() as $hit) {
79
            $model = $this->instantiate($class);
80
            $item = new ElasticsearchItemResponse($hit);
81
            $this->fill($model, $item/*, new RelationshipKey($item->id())*/);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
82
            if ($callback != null) {
83
                $callback($model);
84
            }
85
            $models->put($model->getPrimaryKey(), $model);
86
        }
87
        return $models;
88
    }
89
90 View Code Duplication
    public function insert(Storable $model)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $relationshipKey = RelationshipKey::parse($model->getPrimaryKey());
93
        $collection = new ElasticsearchCollectionParser($model->getCollection());
94
        $params = [
95
            'index' => $collection->getIndex(),
96
            'type' => $collection->getType(),
97
            'id' => $relationshipKey->getId(),
98
            'parent' => $relationshipKey->getParentId(),
99
            'body' => $model->toArray()
100
        ];
101
        $this->client->index($params);
102
    }
103
104 View Code Duplication
    public function update(Storable $model)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        $relationshipKey = RelationshipKey::parse($model->getPrimaryKey());
107
        $collection = new ElasticsearchCollectionParser($model->getCollection());
108
        $params = [
109
            'index' => $collection->getIndex(),
110
            'type' => $collection->getType(),
111
            'id' => $relationshipKey->getId(),
112
            'parent' => $relationshipKey->getParentId(),
113
            'body' => $model->toArray()
114
        ];
115
        $this->client->index($params);
116
    }
117
118
    public function delete(Storable $model)
119
    {
120
        $collection = new ElasticsearchCollectionParser($model->getCollection());
121
        $params = [
122
            'index' => $collection->getIndex(),
123
            'type' => $collection->getType(),
124
            'id' => $model->getPrimaryKey()
125
        ];
126
        $this->client->delete($params);
127
    }
128
}