Passed
Branch master (8cb5b2)
by Christopher
04:37
created

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