Completed
Push — v0.10 ( f42b97...309689 )
by Simonas
8s
created

DateRangeAggregation   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 20.48 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 9
lcom 1
cbo 2
dl 17
loc 83
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormat() 0 4 1
A setFormat() 0 4 1
A getArray() 0 13 4
A getType() 0 4 1
A addRange() 17 17 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\ElasticsearchBundle\DSL\Aggregation;
13
14
use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\BucketingTrait;
15
16
/**
17
 * Class representing date range aggregation.
18
 */
19
class DateRangeAggregation extends AbstractAggregation
20
{
21
    use BucketingTrait;
22
23
    /**
24
     * @var string
25
     */
26
    private $format;
27
28
    /**
29
     * @return string
30
     */
31
    public function getFormat()
32
    {
33
        return $this->format;
34
    }
35
36
    /**
37
     * @param string $format
38
     */
39
    public function setFormat($format)
40
    {
41
        $this->format = $format;
42
    }
43
44
    /**
45
     * @var array
46
     */
47
    private $ranges = [];
48
49
    /**
50
     * Add range to aggregation.
51
     *
52
     * @param string|null $from
53
     * @param string|null $to
54
     *
55
     * @return RangeAggregation
56
     *
57
     * @throws \LogicException
58
     */
59 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...
60
    {
61
        $range = array_filter(
62
            [
63
                'from' => $from,
64
                'to' => $to,
65
            ]
66
        );
67
68
        if (empty($range)) {
69
            throw new \LogicException('Either from or to must be set. Both cannot be null.');
70
        }
71
72
        $this->ranges[] = $range;
73
74
        return $this;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getArray()
81
    {
82
        if ($this->getField() && $this->getFormat() && !empty($this->ranges)) {
83
            $data = [
84
                'format' => $this->getFormat(),
85
                'field' => $this->getField(),
86
                'ranges' => $this->ranges,
87
            ];
88
89
            return $data;
90
        }
91
        throw new \LogicException('Date range aggregation must have field, format set and range added.');
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function getType()
98
    {
99
        return 'date_range';
100
    }
101
}
102