HistogramAggregation::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 9

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
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
17
/**
18
 * Class representing Histogram aggregation.
19
 *
20
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html
21
 */
22
class HistogramAggregation extends AbstractAggregation
23
{
24
    use BucketingTrait;
25
26
    const DIRECTION_ASC = 'asc';
27
    const DIRECTION_DESC = 'desc';
28
29
    /**
30
     * @var int
31
     */
32
    protected $interval;
33
34
    /**
35
     * @var int
36
     */
37
    protected $minDocCount;
38
39
    /**
40
     * @var array
41
     */
42
    protected $extendedBounds;
43
44
    /**
45
     * @var string
46
     */
47
    protected $orderMode;
48
49
    /**
50
     * @var string
51
     */
52
    protected $orderDirection;
53
54
    /**
55
     * @var bool
56
     */
57
    protected $keyed;
58
59
    /**
60
     * Inner aggregations container init.
61
     *
62
     * @param string $name
63
     * @param string $field
64
     * @param int    $interval
65
     * @param int    $minDocCount
66
     * @param string $orderMode
67
     * @param string $orderDirection
68
     * @param int    $extendedBoundsMin
69
     * @param int    $extendedBoundsMax
70
     * @param bool   $keyed
71
     */
72
    public function __construct(
73
        $name,
74
        $field = null,
75
        $interval = null,
76
        $minDocCount = null,
77
        $orderMode = null,
78
        $orderDirection = self::DIRECTION_ASC,
79
        $extendedBoundsMin = null,
80
        $extendedBoundsMax = null,
81
        $keyed = null
82
    ) {
83
        parent::__construct($name);
84
85
        $this->setField($field);
86
        $this->setInterval($interval);
87
        $this->setMinDocCount($minDocCount);
88
        $this->setOrder($orderMode, $orderDirection);
89
        $this->setExtendedBounds($extendedBoundsMin, $extendedBoundsMax);
90
        $this->setKeyed($keyed);
0 ignored issues
show
Bug introduced by
It seems like $keyed defined by parameter $keyed on line 81 can also be of type null; however, ONGR\ElasticsearchDSL\Ag...Aggregation::setKeyed() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
91
    }
92
93
    /**
94
     * @return bool
95
     */
96
    public function isKeyed()
97
    {
98
        return $this->keyed;
99
    }
100
101
    /**
102
     * Get response as a hash instead keyed by the buckets keys.
103
     *
104
     * @param bool $keyed
105
     *
106
     * @return $this
107
     */
108
    public function setKeyed($keyed)
109
    {
110
        $this->keyed = $keyed;
111
112
        return $this;
113
    }
114
115
    /**
116
     * Sets buckets ordering.
117
     *
118
     * @param string $mode
119
     * @param string $direction
120
     *
121
     * @return $this
122
     */
123
    public function setOrder($mode, $direction = self::DIRECTION_ASC)
124
    {
125
        $this->orderMode = $mode;
126
        $this->orderDirection = $direction;
127
128
        return $this;
129
    }
130
131
    /**
132
     * @return array
133
     */
134
    public function getOrder()
135
    {
136
        if ($this->orderMode && $this->orderDirection) {
137
            return [$this->orderMode => $this->orderDirection];
138
        } else {
139
            return null;
140
        }
141
    }
142
143
    /**
144
     * @return int
145
     */
146
    public function getInterval()
147
    {
148
        return $this->interval;
149
    }
150
151
    /**
152
     * @param int $interval
153
     *
154
     * @return $this
155
     */
156
    public function setInterval($interval)
157
    {
158
        $this->interval = $interval;
159
160
        return $this;
161
    }
162
163
    /**
164
     * @return int
165
     */
166
    public function getMinDocCount()
167
    {
168
        return $this->minDocCount;
169
    }
170
171
    /**
172
     * Set limit for document count buckets should have.
173
     *
174
     * @param int $minDocCount
175
     *
176
     * @return $this
177
     */
178
    public function setMinDocCount($minDocCount)
179
    {
180
        $this->minDocCount = $minDocCount;
181
182
        return $this;
183
    }
184
185
    /**
186
     * @return array
187
     */
188
    public function getExtendedBounds()
189
    {
190
        return $this->extendedBounds;
191
    }
192
193
    /**
194
     * @param int $min
195
     * @param int $max
196
     *
197
     * @return $this
198
     */
199
    public function setExtendedBounds($min = null, $max = null)
200
    {
201
        $bounds = array_filter(
202
            [
203
                'min' => $min,
204
                'max' => $max,
205
            ],
206
            'strlen'
207
        );
208
        $this->extendedBounds = $bounds;
209
210
        return $this;
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216
    public function getType()
217
    {
218
        return 'histogram';
219
    }
220
221
    /**
222
     * {@inheritdoc}
223
     */
224
    public function getArray()
225
    {
226
        $out = array_filter(
227
            [
228
                'field' => $this->getField(),
229
                'interval' => $this->getInterval(),
230
                'min_doc_count' => $this->getMinDocCount(),
231
                'extended_bounds' => $this->getExtendedBounds(),
232
                'keyed' => $this->isKeyed(),
233
                'order' => $this->getOrder(),
234
            ],
235
            function ($val) {
236
                return ($val || is_numeric($val));
237
            }
238
        );
239
        $this->checkRequiredParameters($out, ['field', 'interval']);
240
241
        return $out;
242
    }
243
244
    /**
245
     * Checks if all required parameters are set.
246
     *
247
     * @param array $data
248
     * @param array $required
249
     *
250
     * @throws \LogicException
251
     */
252
    protected function checkRequiredParameters($data, $required)
253
    {
254
        if (count(array_intersect_key(array_flip($required), $data)) !== count($required)) {
255
            throw new \LogicException('Histogram aggregation must have field and interval set.');
256
        }
257
    }
258
}
259