Completed
Pull Request — master (#148)
by Simonas
03:24
created

PercentilesAggregation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 5
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\Metric;
13
14
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
15
use ONGR\ElasticsearchDSL\Aggregation\Type\MetricTrait;
16
use ONGR\ElasticsearchDSL\ScriptAwareTrait;
17
18
/**
19
 * Class representing PercentilesAggregation.
20
 *
21
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-aggregation.html
22
 */
23
class PercentilesAggregation extends AbstractAggregation
24
{
25
    use MetricTrait;
26
    use ScriptAwareTrait;
27
28
    /**
29
     * @var array
30
     */
31
    private $percents;
32
33
    /**
34
     * @var int
35
     */
36
    private $compression;
37
38
    /**
39
     * Inner aggregations container init.
40
     *
41
     * @param string $name
42
     * @param string $field
43
     * @param array  $percents
44
     * @param string $script
45
     * @param int    $compression
46
     */
47 View Code Duplication
    public function __construct($name, $field = null, $percents = null, $script = null, $compression = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49
        parent::__construct($name);
50
51
        $this->setField($field);
52
        $this->setPercents($percents);
0 ignored issues
show
Bug introduced by
It seems like $percents defined by parameter $percents on line 47 can also be of type null; however, ONGR\ElasticsearchDSL\Ag...regation::setPercents() does only seem to accept array, 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...
53
        $this->setScript($script);
54
        $this->setCompression($compression);
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    public function getPercents()
61
    {
62
        return $this->percents;
63
    }
64
65
    /**
66
     * @param array $percents
67
     */
68
    public function setPercents($percents)
69
    {
70
        $this->percents = $percents;
71
    }
72
73
    /**
74
     * @return int
75
     */
76
    public function getCompression()
77
    {
78
        return $this->compression;
79
    }
80
81
    /**
82
     * @param int $compression
83
     */
84
    public function setCompression($compression)
85
    {
86
        $this->compression = $compression;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function getType()
93
    {
94
        return 'percentiles';
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 View Code Duplication
    public function getArray()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        $out = array_filter(
103
            [
104
                'compression' => $this->getCompression(),
105
                'percents' => $this->getPercents(),
106
                'field' => $this->getField(),
107
                'script' => $this->getScript(),
108
            ],
109
            function ($val) {
110
                return ($val || is_numeric($val));
111
            }
112
        );
113
114
        $this->isRequiredParametersSet($out);
115
116
        return $out;
117
    }
118
119
    /**
120
     * @param array $a
121
     *
122
     * @throws \LogicException
123
     */
124
    private function isRequiredParametersSet($a)
125
    {
126
        if (!array_key_exists('field', $a) && !array_key_exists('script', $a)) {
127
            throw new \LogicException('Percentiles aggregation must have field or script set.');
128
        }
129
    }
130
}
131