Completed
Push — master ( 197eed...6f0e33 )
by Simonas
10:57 queued 11s
created

CompositeAggregation   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 130
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A addSource() 0 12 2
A getArray() 0 16 3
A getType() 0 4 1
A setSize() 0 6 1
A getSize() 0 4 1
A setAfter() 0 6 1
A getAfter() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing;
13
14
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
15
use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait;
16
use ONGR\ElasticsearchDSL\BuilderInterface;
17
18
/**
19
 * Class representing composite aggregation.
20
 *
21
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html
22
 */
23
class CompositeAggregation extends AbstractAggregation
24
{
25
    use BucketingTrait;
26
27
    /**
28
     * @var BuilderInterface[]
29
     */
30
    private $sources = [];
31
32
    /**
33
     * @var int
34
     */
35
    private $size;
36
37
    /**
38
     * @var array
39
     */
40
    private $after;
41
42
    /**
43
     * Inner aggregations container init.
44
     *
45
     * @param string             $name
46
     * @param AbstractAggregation[] $sources
47
     */
48
    public function __construct($name, $sources = [])
49
    {
50
        parent::__construct($name);
51
52
        foreach ($sources as $agg) {
53
            $this->addSource($agg);
54
        }
55
    }
56
57
    /**
58
     * @param AbstractAggregation $agg
59
     *
60
     * @throws \LogicException
61
     *
62
     * @return self
63
     */
64
    public function addSource(AbstractAggregation $agg)
65
    {
66
        $array = $agg->getArray();
67
68
        $array = is_array($array) ? array_merge($array, $agg->getParameters()) : $array;
69
70
        $this->sources[] = [
71
            $agg->getName() => [ $agg->getType() => $array ]
72
        ];
73
74
        return $this;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getArray()
81
    {
82
        $array = [
83
            'sources' => $this->sources,
84
        ];
85
86
        if ($this->size !== null) {
87
            $array['size'] = $this->size;
88
        }
89
90
        if (!empty($this->after)) {
91
            $array['after'] = $this->after;
92
        }
93
94
        return $array;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function getType()
101
    {
102
        return 'composite';
103
    }
104
105
    /**
106
     * Sets size
107
     *
108
     * @param int $size Size
109
     *
110
     * @return $this
111
     */
112
    public function setSize($size)
113
    {
114
        $this->size = $size;
115
116
        return $this;
117
    }
118
119
    /**
120
     * Returns size
121
     *
122
     * @return int
123
     */
124
    public function getSize()
125
    {
126
        return $this->size;
127
    }
128
129
    /**
130
     * Sets after
131
     *
132
     * @param array $after After
133
     *
134
     * @return $this
135
     */
136
    public function setAfter(array $after)
137
    {
138
        $this->after = $after;
139
140
        return $this;
141
    }
142
143
    /**
144
     * Returns after
145
     *
146
     * @return array
147
     */
148
    public function getAfter()
149
    {
150
        return $this->after;
151
    }
152
}
153