DateRangeAggregation::__construct()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4888
c 0
b 0
f 0
cc 5
nc 9
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\Bucketing;
13
14
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
15
use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait;
16
17
/**
18
 * Class representing date range aggregation.
19
 *
20
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html
21
 */
22
class DateRangeAggregation extends AbstractAggregation
23
{
24
    use BucketingTrait;
25
26
    /**
27
     * @var string
28
     */
29
    private $format;
30
31
    /**
32
     * @var array
33
     */
34
    private $ranges = [];
35
36
    /**
37
     * @var bool
38
     */
39
    private $keyed = false;
40
41
    /**
42
     * @param string $name
43
     * @param string $field
44
     * @param string $format
45
     * @param array  $ranges
46
     * @param bool   $keyed
47
     */
48 View Code Duplication
    public function __construct($name, $field = null, $format = null, array $ranges = [], $keyed = false)
0 ignored issues
show
Duplication introduced by pmishev
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...
49
    {
50
        parent::__construct($name);
51
52
        $this->setField($field);
53
        $this->setFormat($format);
54
        $this->setKeyed($keyed);
55
        foreach ($ranges as $range) {
56
            $from = isset($range['from']) ? $range['from'] : null;
57
            $to = isset($range['to']) ? $range['to'] : null;
58
            $key = isset($range['key']) ? $range['key'] : null;
59
            $this->addRange($from, $to, $key);
60
        }
61
    }
62
63
    /**
64
     * Sets if result buckets should be keyed.
65
     *
66
     * @param bool $keyed
67
     *
68
     * @return DateRangeAggregation
69
     */
70
    public function setKeyed($keyed)
71
    {
72
        $this->keyed = $keyed;
73
74
        return $this;
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function getFormat()
81
    {
82
        return $this->format;
83
    }
84
85
    /**
86
     * @param string $format
87
     */
88
    public function setFormat($format)
89
    {
90
        $this->format = $format;
91
    }
92
93
    /**
94
     * Add range to aggregation.
95
     *
96
     * @param string|null $from
97
     * @param string|null $to
98
     * @param string|null $key
99
     *
100
     * @return $this
101
     *
102
     * @throws \LogicException
103
     */
104 View Code Duplication
    public function addRange($from = null, $to = null, $key = null)
0 ignored issues
show
Duplication introduced by David Higgins
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...
105
    {
106
        $range = array_filter(
107
            [
108
                'from' => $from,
109
                'to' => $to,
110
                'key' => $key,
111
            ],
112
            function ($v) {
113
                return !is_null($v);
114
            }
115
        );
116
117
        if (empty($range)) {
118
            throw new \LogicException('Either from or to must be set. Both cannot be null.');
119
        }
120
121
        $this->ranges[] = $range;
122
123
        return $this;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function getArray()
130
    {
131
        if ($this->getField() && $this->getFormat() && !empty($this->ranges)) {
132
            $data = [
133
                'format' => $this->getFormat(),
134
                'field' => $this->getField(),
135
                'ranges' => $this->ranges,
136
                'keyed' => $this->keyed,
137
            ];
138
139
            return $data;
140
        }
141
        throw new \LogicException('Date range aggregation must have field, format set and range added.');
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function getType()
148
    {
149
        return 'date_range';
150
    }
151
}
152