Passed
Push — master ( ab8c49...f6b2fc )
by Christopher
06:09 queued 02:42
created

ElasticsearchManager::updateStatement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Triadev\Leopard;
3
4
use Elasticsearch\Client;
5
use Elasticsearch\Common\Exceptions\Missing404Exception;
6
use Illuminate\Database\Eloquent\Model;
7
use Triadev\Es\Contract\ElasticsearchContract;
8
use Triadev\Es\Dsl\Dsl\Search;
9
use Triadev\Es\Dsl\Dsl\Suggestion;
10
use Triadev\Es\Dsl\Facade\ElasticDsl;
11
use Triadev\Leopard\Business\Dsl\SearchDsl;
12
use Triadev\Leopard\Business\Helper\IsModelSearchable;
13
use Triadev\Leopard\Business\Mapping\Builder;
14
use Triadev\Leopard\Contract\ElasticsearchManagerContract;
15
use Triadev\Leopard\Contract\Repository\ElasticsearchRepositoryContract;
16
17
class ElasticsearchManager implements ElasticsearchManagerContract
18
{
19
    use IsModelSearchable;
20
    
21
    /** @var Client */
22
    private $esClient;
23
    
24
    /**
25
     * ElasticsearchManager constructor.
26
     * @param ElasticsearchContract $elasticsearch
27
     */
28 19
    public function __construct(ElasticsearchContract $elasticsearch)
29
    {
30 19
        $this->esClient = $elasticsearch->getClient();
31 19
    }
32
    
33
    /**
34
     * Get elasticsearch client
35
     *
36
     * @return Client
37
     */
38 18
    public function getEsClient(): Client
39
    {
40 18
        return $this->esClient;
41
    }
42
    
43
    /**
44
     * Get es default index
45
     *
46
     * @return string
47
     */
48 4
    public function getEsDefaultIndex() : string
49
    {
50 4
        return config('leopard.index');
51
    }
52
    
53
    /**
54
     * Search
55
     *
56
     * @param \ONGR\ElasticsearchDSL\Search|null $search
57
     * @param Model|Searchable|null $model
58
     * @return SearchDsl|Search
59
     */
60 5
    public function search(
61
        ?\ONGR\ElasticsearchDSL\Search $search = null,
62
        ?Model $model = null
63
    ) : SearchDsl {
64 5
        return app()->make(SearchDsl::class, [
65 5
            'search' => $search,
66 5
            'model' => $model
67
        ]);
68
    }
69
    
70
    /**
71
     * Suggestion
72
     *
73
     * @return Suggestion
74
     */
75
    public function suggest() : Suggestion
76
    {
77
        return ElasticDsl::suggest();
78
    }
79
    
80
    /**
81
     * Repository
82
     *
83
     * @return ElasticsearchRepositoryContract
84
     */
85 1
    public function repository() : ElasticsearchRepositoryContract
86
    {
87 1
        return app(ElasticsearchRepositoryContract::class);
88
    }
89
    
90
    /**
91
     * Map
92
     *
93
     * @param \Closure $blueprint
94
     * @param string $index
95
     * @param string $type
96
     * @param bool $createIndex
97
     */
98 11
    public function map(\Closure $blueprint, string $index, string $type, bool $createIndex = false)
99
    {
100 11
        (new Builder())->create($blueprint, $index, $type, $createIndex);
101 11
    }
102
    
103
    /**
104
     * Search statement
105
     *
106
     * @param array $params
107
     * @return array
108
     */
109
    public function searchStatement(array $params) : array
110
    {
111
        return $this->esClient->search($params);
112
    }
113
    
114
    /**
115
     * Put mapping statement
116
     *
117
     * @param array $params
118
     * @return array
119
     */
120 2
    public function putMappingStatement(array $params): array
121
    {
122 2
        return $this->esClient->indices()->putMapping($params);
123
    }
124
    
125
    /**
126
     * Index statement
127
     *
128
     * @param array $params
129
     * @return array
130
     */
131 8
    public function indexStatement(array $params) : array
132
    {
133 8
        return $this->esClient->index($params);
134
    }
135
    
136
    /**
137
     * Update statement
138
     *
139
     * @param array $params
140
     * @return array
141
     */
142 1
    public function updateStatement(array $params) : array
143
    {
144 1
        return $this->esClient->update($params);
145
    }
146
    
147
    /**
148
     * Exist statement
149
     *
150
     * @param array $params
151
     * @return bool
152
     */
153 1
    public function existStatement(array $params) : bool
154
    {
155 1
        return $this->esClient->exists($params);
156
    }
157
    
158
    /**
159
     * Exist index statement
160
     *
161
     * @param array $params
162
     * @return bool
163
     */
164 11
    public function existIndexStatement(array $params) : bool
165
    {
166 11
        return $this->esClient->indices()->exists($params);
167
    }
168
    
169
    /**
170
     * Delete statement
171
     *
172
     * @param array $params
173
     * @return array
174
     */
175 1
    public function deleteStatement(array $params) : array
176
    {
177 1
        return $this->esClient->delete($params);
178
    }
179
    
180
    /**
181
     * Get statement
182
     *
183
     * @param array $params
184
     * @return array|null
185
     */
186 7
    public function getStatement(array $params) : ?array
187
    {
188
        try {
189 7
            return $this->esClient->get($params);
190 7
        } catch (Missing404Exception $e) {
191 7
            return null;
192
        }
193
    }
194
    
195
    /**
196
     * Suggest statement
197
     *
198
     * @param array $params
199
     * @return array
200
     */
201
    public function suggestStatement(array $params): array
202
    {
203
        return $this->esClient->suggest($params);
204
    }
205
    
206
    /**
207
     * Bulk statement
208
     *
209
     * @param array $params
210
     * @return array
211
     */
212 3
    public function bulkStatement(array $params): array
213
    {
214 3
        return $this->esClient->bulk($params);
215
    }
216
}
217