Passed
Push — master ( d023fc...efcb6f )
by Christopher
02:59 queued 11s
created

Blueprint::shouldCreateIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Triadev\Leopard\Business\Mapping;
3
4
use Illuminate\Support\Fluent;
5
use Triadev\Leopard\Facade\Leopard;
6
7
class Blueprint
8
{
9
    /** @var Fluent[] */
10
    private $fields = [];
11
    
12
    /** @var array|null */
13
    private $settings;
14
    
15
    /**
16
     * Blueprint constructor.
17
     * @param \Closure|null $callback
18
     */
19 15
    public function __construct(\Closure $callback = null)
20
    {
21 15
        if (!is_null($callback)) {
22
            $callback($this);
23
        }
24 15
    }
25
    
26
    /**
27
     * Build mapping
28
     *
29
     * @param string $index
30
     * @param string $type
31
     * @param bool $createIndex
32
     * @return array
33
     */
34 10
    public function build(string $index, string $type, bool $createIndex = false) : array
35
    {
36 10
        if ($this->shouldCreateIndex($index, $createIndex)) {
37 8
            return $this->createIndex($index, $type);
38
        }
39
        
40 2
        return $this->updateIndex($index, $type);
41
    }
42
    
43 10
    private function shouldCreateIndex(string $index, bool $createIndex) : bool
44
    {
45 10
        return $createIndex && !Leopard::existIndexStatement(['index' => $index]);
46
    }
47
    
48 8
    private function createIndex(string $index, string $type) : array
49
    {
50
        $body = [
51
            'mappings' => [
52
                $type => [
53 8
                    'properties' => $this->toDsl()
54
                ]
55
            ]
56
        ];
57
    
58 8
        if (is_array($this->settings)) {
59 8
            $body['settings'] = $this->settings;
60
        }
61
    
62 8
        return Leopard::getEsClient()->indices()->create([
63 8
            'index' => $index,
64 8
            'body' => $body
65
        ]);
66
    }
67
    
68 2
    private function updateIndex(string $index, string $type) : array
69
    {
70 2
        return Leopard::putMappingStatement([
71 2
            'index' => $index,
72 2
            'type' => $type,
73
            'body' => [
74
                $type => [
75
                    '_source' => [
76
                        'enabled' => true
77
                    ],
78 2
                    'properties' => $this->toDsl()
79
                ]
80
            ]
81
        ]);
82
    }
83
    
84
    /**
85
     * To dsl
86
     *
87
     * @return array
88
     */
89 12
    public function toDsl() : array
90
    {
91 12
        return (new Compiler())->compileFields($this->fields);
92
    }
93
    
94
    /**
95
     * Get fields
96
     *
97
     * @return array
98
     */
99 3
    public function getFields() : array
100
    {
101 3
        return $this->fields;
102
    }
103
    
104
    /**
105
     * Text
106
     *
107
     * @param string $field
108
     * @param array $attributes
109
     * @return Fluent
110
     */
111 12
    public function text(string $field, array $attributes = []) : Fluent
112
    {
113 12
        return $this->addField('text', $field, $attributes);
114
    }
115
    
116
    /**
117
     * Keyword
118
     *
119
     * @param string $field
120
     * @param array $attributes
121
     * @return Fluent
122
     */
123 13
    public function keyword(string $field, array $attributes = []) : Fluent
124
    {
125 13
        return $this->addField('keyword', $field, $attributes);
126
    }
127
    
128
    /**
129
     * Long
130
     *
131
     * @param string $field
132
     * @param array $attributes
133
     * @return Fluent
134
     */
135 1
    public function long(string $field, array $attributes = []) : Fluent
136
    {
137 1
        return $this->addField('long', $field, $attributes);
138
    }
139
    
140
    /**
141
     * Integer
142
     *
143
     * @param string $field
144
     * @param array $attributes
145
     * @return Fluent
146
     */
147 11
    public function integer(string $field, array $attributes = []) : Fluent
148
    {
149 11
        return $this->addField('integer', $field, $attributes);
150
    }
151
    
152
    /**
153
     * Short
154
     *
155
     * @param string $field
156
     * @param array $attributes
157
     * @return Fluent
158
     */
159 1
    public function short(string $field, array $attributes = []) : Fluent
160
    {
161 1
        return $this->addField('short', $field, $attributes);
162
    }
163
    
164
    /**
165
     * Byte
166
     *
167
     * @param string $field
168
     * @param array $attributes
169
     * @return Fluent
170
     */
171 1
    public function byte(string $field, array $attributes = []) : Fluent
172
    {
173 1
        return $this->addField('byte', $field, $attributes);
174
    }
175
    
176
    /**
177
     * Double
178
     *
179
     * @param string $field
180
     * @param array $attributes
181
     * @return Fluent
182
     */
183 1
    public function double(string $field, array $attributes = []) : Fluent
184
    {
185 1
        return $this->addField('double', $field, $attributes);
186
    }
187
    
188
    /**
189
     * Float
190
     *
191
     * @param string $field
192
     * @param array $attributes
193
     * @return Fluent
194
     */
195 1
    public function float(string $field, array $attributes = []) : Fluent
196
    {
197 1
        return $this->addField('float', $field, $attributes);
198
    }
199
    
200
    /**
201
     * Half float
202
     *
203
     * @param string $field
204
     * @param array $attributes
205
     * @return Fluent
206
     */
207 1
    public function halfFloat(string $field, array $attributes = []) : Fluent
208
    {
209 1
        return $this->addField('half_float', $field, $attributes);
210
    }
211
    
212
    /**
213
     * Scaled float
214
     *
215
     * @param string $field
216
     * @param array $attributes
217
     * @return Fluent
218
     */
219 1
    public function scaledFloat(string $field, array $attributes = []) : Fluent
220
    {
221 1
        return $this->addField('scaled_float', $field, $attributes);
222
    }
223
    
224
    /**
225
     * Date
226
     *
227
     * @param string $field
228
     * @param array $attributes
229
     * @return Fluent
230
     */
231 1
    public function date(string $field, array $attributes = []) : Fluent
232
    {
233 1
        return $this->addField('date', $field, $attributes);
234
    }
235
    
236
    /**
237
     * Boolean
238
     *
239
     * @param string $field
240
     * @param array $attributes
241
     * @return Fluent
242
     */
243 1
    public function boolean(string $field, array $attributes = []) : Fluent
244
    {
245 1
        return $this->addField('boolean', $field, $attributes);
246
    }
247
    
248
    /**
249
     * Binary
250
     *
251
     * @param string $field
252
     * @param array $attributes
253
     * @return Fluent
254
     */
255 1
    public function binary(string $field, array $attributes = []) : Fluent
256
    {
257 1
        return $this->addField('binary', $field, $attributes);
258
    }
259
    
260
    /**
261
     * Range
262
     *
263
     * @param string $field
264
     * @param string $type
265
     * @param array $attributes
266
     * @return Fluent
267
     *
268
     * @throws \InvalidArgumentException
269
     */
270 2
    public function range(string $field, string $type, array $attributes = []) : Fluent
271
    {
272
        $validTypes = [
273 2
            'integer',
274
            'float',
275
            'long',
276
            'double',
277
            'date',
278
            'ip'
279
        ];
280
        
281 2
        if (!in_array($type, $validTypes)) {
282 1
            throw new \InvalidArgumentException();
283
        }
284
        
285 1
        return $this->addField(strtolower($type) . '_range', $field, $attributes);
286
    }
287
    
288
    /**
289
     * Nested
290
     *
291
     * @param string $field
292
     * @param array $attributes
293
     * @return Fluent
294
     */
295 1
    public function nested(string $field, array $attributes = []) : Fluent
296
    {
297 1
        return $this->addField('nested', $field, $attributes);
298
    }
299
    
300
    /**
301
     * Object
302
     *
303
     * @param string $field
304
     * @param array $attributes
305
     * @return Fluent
306
     */
307 1
    public function object(string $field, array $attributes = []) : Fluent
308
    {
309 1
        return $this->addField('object', $field, $attributes);
310
    }
311
    
312
    /**
313
     * Geo point
314
     *
315
     * @param string $field
316
     * @param array $attributes
317
     * @return Fluent
318
     */
319 1
    public function geoPoint(string $field, array $attributes = []) : Fluent
320
    {
321 1
        return $this->addField('geo_point', $field, $attributes);
322
    }
323
    
324
    /**
325
     * Geo shape
326
     *
327
     * @param string $field
328
     * @param array $attributes
329
     * @return Fluent
330
     */
331 1
    public function geoShape(string $field, array $attributes = []) : Fluent
332
    {
333 1
        return $this->addField('geo_shape', $field, $attributes);
334
    }
335
    
336
    /**
337
     * Ip
338
     *
339
     * @param string $field
340
     * @param array $attributes
341
     * @return Fluent
342
     */
343 1
    public function ip(string $field, array $attributes = []) : Fluent
344
    {
345 1
        return $this->addField('ip', $field, $attributes);
346
    }
347
    
348
    /**
349
     * Completion
350
     *
351
     * @param string $field
352
     * @param array $attributes
353
     * @return Fluent
354
     */
355 1
    public function completion(string $field, array $attributes = []) : Fluent
356
    {
357 1
        return $this->addField('completion', $field, $attributes);
358
    }
359
    
360
    /**
361
     * Token count
362
     *
363
     * @param string $field
364
     * @param array $attributes
365
     * @return Fluent
366
     */
367 1
    public function tokenCount(string $field, array $attributes = []) : Fluent
368
    {
369 1
        return $this->addField('token_count', $field, $attributes);
370
    }
371
    
372
    /**
373
     * Add field
374
     *
375
     * @param string $type
376
     * @param string $name
377
     * @param array $attributes
378
     * @return Fluent
379
     */
380 14
    public function addField(string $type, string $name, array $attributes = []) : Fluent
381
    {
382 14
        $this->fields[] = $field = new Fluent(
383 14
            array_merge(
384 14
                compact(
385 14
                    'type',
386 14
                    'name'
387
                ),
388 14
                $attributes
389
            )
390
        );
391
        
392 14
        return $field;
393
    }
394
    
395
    /**
396
     * Settings
397
     *
398
     * @param array $settings
399
     */
400 10
    public function settings(array $settings)
401
    {
402 10
        $this->settings = $settings;
403 10
    }
404
}
405