Completed
Push — master ( a07d82...358457 )
by Sergey
07:15
created

ElasticsearchDAL::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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