Completed
Branch master (ab3fbf)
by Christopher
03:46
created

Blueprint   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 397
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 98.78%

Importance

Changes 0
Metric Value
dl 0
loc 397
c 0
b 0
f 0
wmc 34
lcom 1
cbo 5
ccs 81
cts 82
cp 0.9878
rs 9.68

30 Methods

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