Completed
Push — master ( 62f765...d71e72 )
by Simonas
15:54
created

DateRangeAggregation   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 104
Duplicated Lines 12.5 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 13
loc 104
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormat() 0 4 1
B __construct() 13 13 5
A setFormat() 0 4 1
A getArray() 0 13 4
A getType() 0 4 1
A addRange() 0 18 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
     * @return string
33
     */
34
    public function getFormat()
35
    {
36
        return $this->format;
37
    }
38
39
    /**
40
     * @param string $name
41
     * @param string $field
42
     * @param string $format
43
     * @param array  $ranges
44
     */
45 View Code Duplication
    public function __construct($name, $field = null, $format = null, array $ranges = [])
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...
46
    {
47
        parent::__construct($name);
48
49
        $this->setField($field);
50
        $this->setFormat($format);
51
        foreach ($ranges as $range) {
52
            $from = isset($range['from']) ? $range['from'] : null;
53
            $to = isset($range['to']) ? $range['to'] : null;
54
            $key = isset($range['key']) ? $range['key'] : null;
55
            $this->addRange($from, $to, $key);
56
        }
57
    }
58
59
    /**
60
     * @param string $format
61
     */
62
    public function setFormat($format)
63
    {
64
        $this->format = $format;
65
    }
66
67
    /**
68
     * @var array
69
     */
70
    private $ranges = [];
71
72
    /**
73
     * Add range to aggregation.
74
     *
75
     * @param string|null $from
76
     * @param string|null $to
77
     *
78
     * @return $this
79
     *
80
     * @throws \LogicException
81
     */
82
    public function addRange($from = null, $to = null, $key = null)
83
    {
84
        $range = array_filter(
85
            [
86
                'from' => $from,
87
                'to' => $to,
88
                'key' => $key,
89
            ]
90
        );
91
92
        if (empty($range)) {
93
            throw new \LogicException('Either from or to must be set. Both cannot be null.');
94
        }
95
96
        $this->ranges[] = $range;
97
98
        return $this;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getArray()
105
    {
106
        if ($this->getField() && $this->getFormat() && !empty($this->ranges)) {
107
            $data = [
108
                'format' => $this->getFormat(),
109
                'field' => $this->getField(),
110
                'ranges' => $this->ranges,
111
            ];
112
113
            return $data;
114
        }
115
        throw new \LogicException('Date range aggregation must have field, format set and range added.');
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function getType()
122
    {
123
        return 'date_range';
124
    }
125
}
126