Passed
Pull Request — master (#1)
by Christopher
05:53
created

Aggregation::metric()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Triadev\Es\Dsl\Dsl;
3
4
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
5
use Triadev\Es\Dsl\Dsl\Aggregation\Bucketing;
6
use Triadev\Es\Dsl\Dsl\Aggregation\Metric;
7
use Triadev\Es\Dsl\Dsl\Aggregation\Pipeline;
8
9
class Aggregation
10
{
11
    /** @var \ONGR\ElasticsearchDSL\Search */
12
    private $search;
13
    
14
    /**
15
     * Aggregation constructor.
16
     * @param \ONGR\ElasticsearchDSL\Search $search
17
     */
18
    public function __construct(\ONGR\ElasticsearchDSL\Search $search)
19
    {
20
        $this->search = $search;
21
    }
22
    
23
    /**
24
     * Bucketing
25
     *
26
     * @param \Closure $bucketing
27
     * @return Aggregation
28
     */
29
    public function bucketing(\Closure $bucketing) : Aggregation
30
    {
31
        $bucketingBuilder = new Bucketing();
32
        $bucketing($bucketingBuilder);
33
        
34
        $this->append($bucketingBuilder->getAggregations());
35
        return $this;
36
    }
37
    
38
    /**
39
     * Metric
40
     *
41
     * @param \Closure $metric
42
     * @return Aggregation
43
     */
44
    public function metric(\Closure $metric) : Aggregation
45
    {
46
        $metricBuilder = new Metric();
47
        $metric($metricBuilder);
48
        
49
        $this->append($metricBuilder->getAggregations());
50
        return $this;
51
    }
52
    
53
    /**
54
     * Pipeline
55
     *
56
     * @param \Closure $pipeline
57
     * @return Aggregation
58
     */
59
    public function pipeline(\Closure $pipeline) : Aggregation
60
    {
61
        $pipelineBuilder = new Pipeline();
62
        $pipeline($pipelineBuilder);
63
        
64
        $this->append($pipelineBuilder->getAggregations());
65
        return $this;
66
    }
67
    
68
    /**
69
     * @param AbstractAggregation[] $aggs
70
     * @return Aggregation
71
     */
72
    private function append(array $aggs) : Aggregation
73
    {
74
        foreach ($aggs as $agg) {
75
            if ($agg instanceof AbstractAggregation) {
76
                $this->search->addAggregation($agg);
77
            }
78
        }
79
        
80
        return $this;
81
    }
82
}
83