Completed
Push — master ( 72c2fc...074e1b )
by Simonas
04:21
created

DateRangeAggregation   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 102
Duplicated Lines 28.43 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormat() 0 4 1
A __construct() 12 12 4
A setFormat() 0 4 1
A addRange() 17 17 2
A getArray() 0 13 4
A getType() 0 4 1

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