Completed
Push — master ( 816340...ab4a72 )
by Sergey
05:20
created

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