Completed
Pull Request — master (#22)
by Sergey
04:34
created

ElasticsearchRepository::all()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 22
nc 6
nop 4
1
<?php
2
3
namespace Isswp101\Persimmon\Repository;
4
5
use Elasticsearch\Client;
6
use Isswp101\Persimmon\Collection\Collection;
7
use Isswp101\Persimmon\Collection\ICollection;
8
use Isswp101\Persimmon\Contracts\Storable;
9
use Isswp101\Persimmon\Exceptions\ClassTypeErrorException;
10
use Isswp101\Persimmon\Model\IElasticsearchModel;
11
use Isswp101\Persimmon\QueryBuilder\IQueryBuilder;
12
use Isswp101\Persimmon\Response\ElasticsearchResponse;
13
14
class ElasticsearchRepository implements IRepository
15
{
16
    const SOURCE_FALSE = [false];
17
18
    private $client;
19
20
    public function __construct(Client $client)
21
    {
22
        $this->client = $client;
23
    }
24
25
    protected function instantiate(string $class): Storable
26
    {
27
        $instance = new $class;
28
        if (!$instance instanceof IElasticsearchModel) {
29
            throw new ClassTypeErrorException(IElasticsearchModel::class);
30
        }
31
        return $instance;
32
    }
33
34
    protected function fill(Storable $model, ElasticsearchResponse $response)
35
    {
36
        $model->fill($response->source());
37
        $model->setPrimaryKey($response->id());
38
    }
39
40
    public function find($id, string $class, array $columns = []): Storable
41
    {
42
        $model = $this->instantiate($class);
43
        $collection = new ElasticsearchCollectionParser($model->getCollection());
44
        $params = [
45
            'index' => $collection->getIndex(),
46
            'type' => $collection->getType(),
47
            'id' => $id,
48
            '_source' => $columns == ElasticsearchRepository::SOURCE_FALSE ? false : $columns
49
        ];
50
        $response = new ElasticsearchResponse($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...
51
        $this->fill($model, $response);
52
        return $model;
53
    }
54
55
    public function all(
56
        IQueryBuilder $query,
57
        string $class,
58
        array $columns = [],
59
        callable $callback = null
60
    ): ICollection {
61
        $model = $this->instantiate($class);
62
        $collection = new ElasticsearchCollectionParser($model->getCollection());
63
        $params = [
64
            'index' => $collection->getIndex(),
65
            'type' => $collection->getType(),
66
            'body' => $query->build(),
67
            '_source' => $columns == ElasticsearchRepository::SOURCE_FALSE ? false : $columns
68
        ];
69
        $response = new ElasticsearchResponse($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...
70
        $models = new Collection();
71
        foreach ($response->hits() as $hit) {
72
            $model = $this->instantiate($class);
73
            $this->fill($model, new ElasticsearchResponse($hit));
74
            if ($callback != null) {
75
                $callback($model);
76
            }
77
            $models->put($model->getPrimaryKey(), $model);
78
        }
79
        return $models;
80
    }
81
82 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...
83
    {
84
        $collection = new ElasticsearchCollectionParser($model->getCollection());
85
        $params = [
86
            'index' => $collection->getIndex(),
87
            'type' => $collection->getType(),
88
            'id' => $model->getPrimaryKey(),
89
            'body' => $model->toArray()
90
        ];
91
        $this->client->index($params);
92
    }
93
94 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...
95
    {
96
        $collection = new ElasticsearchCollectionParser($model->getCollection());
97
        $params = [
98
            'index' => $collection->getIndex(),
99
            'type' => $collection->getType(),
100
            'id' => $model->getPrimaryKey(),
101
            'body' => $model->toArray()
102
        ];
103
        $this->client->index($params);
104
    }
105
106 View Code Duplication
    public function delete(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...
107
    {
108
        $collection = new ElasticsearchCollectionParser($model->getCollection());
109
        $params = [
110
            'index' => $collection->getIndex(),
111
            'type' => $collection->getType(),
112
            'id' => $model->getPrimaryKey()
113
        ];
114
        $this->client->delete($params);
115
    }
116
}