Passed
Push — master ( 099c87...4cfe3e )
by Christopher
07:03 queued 01:40
created

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