Completed
Pull Request — master (#111)
by
unknown
02:42
created

DateHistogramAggregation   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
c 2
b 0
f 1
lcom 1
cbo 2
dl 0
loc 80
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getInterval() 0 4 1
A setInterval() 0 4 1
A getType() 0 4 1
A getArray() 0 11 1
A checkRequiredParameters() 0 6 3
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;
13
14
use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait;
15
16
/**
17
 * Class representing Histogram aggregation.
18
 */
19
class DateHistogramAggregation extends AbstractAggregation
20
{
21
    use BucketingTrait;
22
23
    /**
24
     * @var string
25
     */
26
    protected $interval;
27
28
    /**
29
     * Inner aggregations container init.
30
     *
31
     * @param string $name
32
     * @param string $field
33
     * @param string    $interval
34
     */
35
    public function __construct(
36
        $name,
37
        $field = null,
38
        $interval = null
39
    ) {
40
        parent::__construct($name);
41
42
        $this->setField($field);
43
        $this->setInterval($interval);
44
    }
45
46
    /**
47
     * @return int
48
     */
49
    public function getInterval()
50
    {
51
        return $this->interval;
52
    }
53
54
    /**
55
     * @param string $interval
56
     */
57
    public function setInterval($interval)
58
    {
59
        $this->interval = $interval;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getType()
66
    {
67
        return 'date_histogram';
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getArray()
74
    {
75
        $out = [
76
            'field' => $this->getField(),
77
            'interval' => $this->getInterval(),
78
        ];
79
        $out = $this->processArray($out);
80
        $this->checkRequiredParameters($out);
81
82
        return $out;
83
    }
84
85
    /**
86
     * Checks if all required parameters are set.
87
     *
88
     * @param array $data
89
     *
90
     * @throws \LogicException
91
     */
92
    protected function checkRequiredParameters(array $data)
93
    {
94
        if (!$data['field'] || !$data['interval']) {
95
            throw new \LogicException('Date histogram aggregation must have field and interval set.');
96
        }
97
    }
98
}
99