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

Bucketing::histogram()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 9
dl 0
loc 23
ccs 12
cts 12
cp 1
crap 1
rs 9.9
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
namespace Triadev\Leopard\Business\Dsl\Aggregation;
3
4
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\ChildrenAggregation;
5
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\DateHistogramAggregation;
6
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\DateRangeAggregation;
7
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\DiversifiedSamplerAggregation;
8
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\FilterAggregation;
9
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\FiltersAggregation;
10
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\GeoDistanceAggregation;
11
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\GeoHashGridAggregation;
12
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\HistogramAggregation;
13
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\Ipv4RangeAggregation;
14
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\MissingAggregation;
15
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\NestedAggregation;
16
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\RangeAggregation;
17
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\SamplerAggregation;
18
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\SignificantTermsAggregation;
19
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\TermsAggregation;
20
use ONGR\ElasticsearchDSL\BuilderInterface;
21
22
class Bucketing extends Aggs
23
{
24
    /**
25
     * Children
26
     *
27
     * @param string $name
28
     * @param string $children
29
     * @param \Closure $bucketing
30
     * @return Bucketing
31
     */
32 1
    public function children(string $name, string $children, \Closure $bucketing) : Bucketing
33
    {
34 1
        $resultAgg = new ChildrenAggregation($name, $children);
35
        
36 1
        $bucketingBuilder = new self();
37 1
        $bucketing($bucketingBuilder);
38
        
39 1
        foreach ($bucketingBuilder->getAggregations() as $agg) {
40 1
            $resultAgg->addAggregation($agg);
41
        }
42
        
43 1
        $this->addAggregation($resultAgg);
44 1
        return $this;
45
    }
46
    
47
    /**
48
     * Date histogram
49
     *
50
     * @param string $name
51
     * @param string $field
52
     * @param string $interval
53
     * @param string|null $format
54
     * @return Bucketing
55
     *
56
     * @throws \InvalidArgumentException
57
     */
58 1
    public function dateHistogram(
59
        string $name,
60
        string $field,
61
        string $interval,
62
        ?string $format = null
63
    ) : Bucketing {
64
        $validInterval = [
65 1
            'year',
66
            'quarter',
67
            'month',
68
            'week',
69
            'day',
70
            'hour',
71
            'minute',
72
            'second'
73
        ];
74
        
75 1
        if (!in_array($interval, $validInterval)) {
76
            throw new \InvalidArgumentException();
77
        }
78
        
79 1
        $this->addAggregation(new DateHistogramAggregation($name, $field, $interval, $format));
80 1
        return $this;
81
    }
82
    
83
    /**
84
     * Date range
85
     *
86
     * @param string $name
87
     * @param string $field
88
     * @param string $format
89
     * @param array $ranges
90
     * @return Bucketing
91
     */
92 1
    public function dateRange(
93
        string $name,
94
        string $field,
95
        string $format,
96
        array $ranges = []
97
    ) : Bucketing {
98 1
        $this->addAggregation(new DateRangeAggregation($name, $field, $format, $ranges));
99 1
        return $this;
100
    }
101
    
102
    /**
103
     * Diversified sampler
104
     *
105
     * @param string $name
106
     * @param string $field
107
     * @param int|null $shardSize
108
     * @return Bucketing
109
     */
110 1
    public function diversifiedSampler(string $name, string $field, ?int $shardSize = null) : Bucketing
111
    {
112 1
        $this->addAggregation(new DiversifiedSamplerAggregation($name, $field, $shardSize));
113 1
        return $this;
114
    }
115
    
116
    /**
117
     * Filter
118
     *
119
     * @param string $name
120
     * @param BuilderInterface $aggregation
121
     * @param BuilderInterface[] $aggregations
122
     * @return Bucketing
123
     */
124 1
    public function filter(string $name, BuilderInterface $aggregation, array $aggregations) : Bucketing
125
    {
126 1
        $agg = new FilterAggregation($name, $aggregation);
127
        
128 1
        foreach ($aggregations as $a) {
129 1
            if ($a instanceof BuilderInterface) {
130 1
                $agg->addAggregation($a);
131
            }
132
        }
133
        
134 1
        $this->addAggregation($agg);
135 1
        return $this;
136
    }
137
    
138
    /**
139
     * Filters
140
     *
141
     * @param string $name
142
     * @param BuilderInterface[] $filters
143
     * @param BuilderInterface[] $aggregations
144
     * @param bool $anonymous
145
     * @return Bucketing
146
     */
147 1
    public function filters(string $name, array $filters, array $aggregations, bool $anonymous = false) : Bucketing
148
    {
149 1
        $agg = new FiltersAggregation($name, $filters, $anonymous);
150
    
151 1
        foreach ($aggregations as $a) {
152 1
            if ($a instanceof BuilderInterface) {
153 1
                $agg->addAggregation($a);
154
            }
155
        }
156
    
157 1
        $this->addAggregation($agg);
158
        
159 1
        return $this;
160
    }
161
    
162
    /**
163
     * Geo distance
164
     *
165
     * @param string $name
166
     * @param string $field
167
     * @param string $origin
168
     * @param array $ranges
169
     * @param string|null $unit
170
     * @param string|null $distanceType
171
     * @return Bucketing
172
     */
173 1
    public function geoDistance(
174
        string $name,
175
        string $field,
176
        string $origin,
177
        array $ranges = [],
178
        string $unit = null,
179
        string $distanceType = null
180
    ) : Bucketing {
181 1
        $this->addAggregation(new GeoDistanceAggregation(
182 1
            $name,
183 1
            $field,
184 1
            $origin,
185 1
            $ranges,
186 1
            $unit,
187 1
            $distanceType
188
        ));
189 1
        return $this;
190
    }
191
    
192
    /**
193
     * Geo hash grid
194
     *
195
     * @param string $name
196
     * @param string $field
197
     * @param int|null $precision
198
     * @param int|null $size
199
     * @param int|null $shardSize
200
     * @return Bucketing
201
     */
202 1
    public function geoHashGrid(
203
        string $name,
204
        string $field,
205
        ?int $precision = null,
206
        ?int $size = null,
207
        ?int $shardSize = null
208
    ) : Bucketing {
209 1
        $this->addAggregation(new GeoHashGridAggregation(
210 1
            $name,
211 1
            $field,
212 1
            $precision,
213 1
            $size,
214 1
            $shardSize
215
        ));
216 1
        return $this;
217
    }
218
    
219
    /**
220
     * Histogram
221
     *
222
     * @param string $name
223
     * @param string $field
224
     * @param int $interval
225
     * @param int|null $minDocCount
226
     * @param string|null $orderMode
227
     * @param string $orderDirection
228
     * @param int|null $extendedBoundsMin
229
     * @param int|null $extendedBoundsMax
230
     * @param bool|null $keyed
231
     * @return Bucketing
232
     */
233 1
    public function histogram(
234
        string $name,
235
        string $field,
236
        int $interval,
237
        ?int $minDocCount = null,
238
        ?string $orderMode = null,
239
        string $orderDirection = HistogramAggregation::DIRECTION_ASC,
240
        ?int $extendedBoundsMin = null,
241
        ?int $extendedBoundsMax = null,
242
        bool $keyed = null
243
    ) : Bucketing {
244 1
        $this->addAggregation(new HistogramAggregation(
245 1
            $name,
246 1
            $field,
247 1
            $interval,
248 1
            $minDocCount,
249 1
            $orderMode,
250 1
            $orderDirection,
251 1
            $extendedBoundsMin,
252 1
            $extendedBoundsMax,
253 1
            $keyed
254
        ));
255 1
        return $this;
256
    }
257
    
258
    /**
259
     * Ipv4
260
     *
261
     * @param string $name
262
     * @param string $field
263
     * @param array $ranges
264
     * @return Bucketing
265
     */
266 1
    public function ipv4Range(string $name, string $field, array $ranges = []) : Bucketing
267
    {
268 1
        $this->addAggregation(new Ipv4RangeAggregation(
269 1
            $name,
270 1
            $field,
271 1
            $ranges
272
        ));
273 1
        return $this;
274
    }
275
    
276
    /**
277
     * Missing
278
     *
279
     * @param string $name
280
     * @param string $field
281
     * @return Bucketing
282
     */
283 1
    public function missing(string $name, string $field) : Bucketing
284
    {
285 1
        $this->addAggregation(new MissingAggregation($name, $field));
286 1
        return $this;
287
    }
288
    
289
    /**
290
     * Nested
291
     *
292
     * @param string $name
293
     * @param string $path
294
     * @param array $aggregations
295
     * @return Bucketing
296
     */
297 1
    public function nested(string $name, string $path, array $aggregations = []) : Bucketing
298
    {
299 1
        $agg = new NestedAggregation($name, $path);
300
        
301 1
        foreach ($aggregations as $aggregation) {
302 1
            if ($aggregation instanceof BuilderInterface) {
303 1
                $agg->addAggregation($aggregation);
304
            }
305
        }
306
        
307 1
        $this->addAggregation($agg);
308 1
        return $this;
309
    }
310
    
311
    /**
312
     * Range
313
     *
314
     * @param string $name
315
     * @param string $field
316
     * @param array $ranges
317
     * @param bool $keyed
318
     * @return Bucketing
319
     */
320 1
    public function range(string $name, string $field, array $ranges = [], bool $keyed = false) : Bucketing
321
    {
322 1
        $this->addAggregation(new RangeAggregation($name, $field, $ranges, $keyed));
323 1
        return $this;
324
    }
325
    
326
    public function reverseNested() : Bucketing
327
    {
328
        return $this;
329
    }
330
    
331
    /**
332
     * Sampler
333
     *
334
     * @param string $name
335
     * @param string $field
336
     * @param int|null $shardSize
337
     * @param array $aggregations
338
     * @return Bucketing
339
     */
340 1
    public function sampler(string $name, string $field, ?int $shardSize = null, array $aggregations = []) : Bucketing
341
    {
342 1
        $agg = new SamplerAggregation($name, $field, $shardSize);
343
        
344 1
        foreach ($aggregations as $aggregation) {
345 1
            if ($aggregation instanceof BuilderInterface) {
346 1
                $agg->addAggregation($aggregation);
347
            }
348
        }
349
        
350 1
        $this->addAggregation($agg);
351 1
        return $this;
352
    }
353
    
354
    /**
355
     * Significant terms
356
     *
357
     * @param string $name
358
     * @param string $field
359
     * @param string|null $script
360
     * @return Bucketing
361
     */
362 1
    public function significantTerms(string $name, string $field, ?string $script = null) : Bucketing
363
    {
364 1
        $this->addAggregation(new SignificantTermsAggregation($name, $field, $script));
365 1
        return $this;
366
    }
367
    
368
    /**
369
     * Terms
370
     *
371
     * @param string $name
372
     * @param string|null $field
373
     * @param string|null $script
374
     * @return Bucketing
375
     */
376 2
    public function terms(string $name, ?string $field = null, ?string $script = null) : Bucketing
377
    {
378 2
        $this->addAggregation(new TermsAggregation($name, $field, $script));
379 2
        return $this;
380
    }
381
}
382