Completed
Push — master ( 6e82ed...9473d1 )
by Sergey
11:37
created

ElasticsearchDAL::setModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Isswp101\Persimmon\DAL;
4
5
use Elasticsearch\Client;
6
use Illuminate\Support\Arr;
7
use Isswp101\Persimmon\Collection\ElasticsearchCollection;
8
use Isswp101\Persimmon\ElasticsearchModel;
9
use Isswp101\Persimmon\Event\EventEmitter;
10
use Isswp101\Persimmon\Model;
11
12
class ElasticsearchDAL implements IDAL
13
{
14
    protected $model;
15
    protected $client;
16
    protected $emitter;
17
18 54
    public function __construct(ElasticsearchModel $model, Client $client, EventEmitter $emitter)
19
    {
20 54
        $this->model = $model;
21 54
        $this->client = $client;
22 54
        $this->emitter = $emitter;
23 54
    }
24
25
    public function setModel(Model $model)
26
    {
27
        $this->model = $model;
28
    }
29
30
    public function getModel()
31
    {
32
        return $this->model;
33
    }
34
35
    public function getEventEmitter()
36
    {
37
        return $this->emitter;
38
    }
39
40 14
    public function get($id, array $options = [])
41
    {
42 14
        $params = $this->model->getPath()->toArray();
43
44 14
        if (!empty($options['columns'])) {
45 2
            $params['_source'] = $options['columns'];
46 2
        }
47
48 14
        if (!empty($options['parent'])) {
49 2
            $params['parent'] = $options['parent'];
50 2
        }
51
52 14
        $response = $this->client->get($params);
53
54 10
        $this->model->fillByResponse($response);
55
56 10
        if (isset($params['parent'])) {
57 2
            $this->model->setParentId($params['parent']);
58 2
        }
59
60 10
        return $this->model;
61
    }
62
63 24
    public function put(array $columns = ['*'])
64
    {
65 24
        $params = $this->getParams();
66
67 24
        if (!$this->model->_exist || $columns == ['*']) {
68 22
            if (!$params['id']) {
69
                unset($params['id']);
70
            }
71
72 22
            $params['body'] = $this->model->toArray();
73
74 22
            $response = $this->client->index($params);
75 22
        } else {
76 2
            $params['body'] = [
77 2
                'doc' => array_only($this->model->toArray(), $columns)
78 2
            ];
79
80 2
            $response = $this->client->update($params);
81
        }
82
83 24
        $this->model->setId($response['_id']);
84
85 24
        return $this->model->getId();
86
    }
87
88 6
    public function delete()
89
    {
90 6
        return $this->client->delete($this->getParams());
91
    }
92
93 24
    protected function getParams()
94
    {
95 24
        $params = $this->model->getPath()->toArray();
96
97 24
        if ($this->model->getParentId()) {
98 4
            $params['parent'] = $this->model->getParentId();
99 4
        }
100
101 24
        return $params;
102
    }
103
104 24
    public function search(array $query)
105
    {
106 22
        if (empty($query['body']['query']) && empty($query['body']['filter'])) {
107 12
            $query['body']['query'] = [
108 14
                'match_all' => []
109 12
            ];
110 12
        }
111
112
        $params = [
113 22
            'index' => $this->model->getIndex(),
114 22
            'type' => $this->model->getType(),
115 22
            'from' => Arr::get($query, 'from', 0),
116 22
            'size' => Arr::get($query, 'size', 50),
117 22
            'body' => $query['body']
118 22
        ];
119
120 22
        $collection = new ElasticsearchCollection();
121
122 22
        $this->emitter->trigger(DALEvents::BEFORE_SEARCH, $params);
123
124 22
        $response = $this->client->search($params);
125
126 22
        $this->emitter->trigger(DALEvents::AFTER_SEARCH, $response);
127
128 24
        $collection->response($response);
129
130 22
        $from = (int)$params['from'];
131 22
        foreach ($response['hits']['hits'] as $hit) {
132 20
            $model = $this->model->createInstance();
133 20
            $model->_score = $hit['_score'];
134 20
            $model->_position = $from++;
135 20
            $model->_exist = true;
136 20
            $model->fillByResponse($hit);
137 20
            $model->fillByInnerHits($hit);
138 20
            $collection->put($model->getId(), $model);
139 22
        }
140
141 22
        return $collection;
142
    }
143
}
144