Completed
Push — master ( 358457...d8cdc6 )
by Sergey
05:23
created

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