Ipv4RangeAggregation   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 93
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 5
A addRange() 0 16 1
A addMask() 0 6 1
A getType() 0 4 1
A getArray() 0 10 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\Bucketing;
13
14
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
15
use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait;
16
17
/**
18
 * Class representing ip range aggregation.
19
 *
20
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-iprange-aggregation.html
21
 */
22
class Ipv4RangeAggregation extends AbstractAggregation
23
{
24
    use BucketingTrait;
25
26
    /**
27
     * @var array
28
     */
29
    private $ranges = [];
30
31
    /**
32
     * Inner aggregations container init.
33
     *
34
     * @param string $name
35
     * @param string $field
36
     * @param array  $ranges
37
     */
38
    public function __construct($name, $field = null, $ranges = [])
39
    {
40
        parent::__construct($name);
41
42
        $this->setField($field);
43
        foreach ($ranges as $range) {
44
            if (is_array($range)) {
45
                $from = isset($range['from']) ? $range['from'] : null;
46
                $to = isset($range['to']) ? $range['to'] : null;
47
                $this->addRange($from, $to);
48
            } else {
49
                $this->addMask($range);
50
            }
51
        }
52
    }
53
54
    /**
55
     * Add range to aggregation.
56
     *
57
     * @param string|null $from
58
     * @param string|null $to
59
     *
60
     * @return Ipv4RangeAggregation
61
     */
62
    public function addRange($from = null, $to = null)
63
    {
64
        $range = array_filter(
65
            [
66
                'from' => $from,
67
                'to' => $to,
68
            ],
69
            function ($v) {
70
                return !is_null($v);
71
            }
72
        );
73
74
        $this->ranges[] = $range;
75
76
        return $this;
77
    }
78
79
    /**
80
     * Add ip mask to aggregation.
81
     *
82
     * @param string $mask
83
     *
84
     * @return Ipv4RangeAggregation
85
     */
86
    public function addMask($mask)
87
    {
88
        $this->ranges[] = ['mask' => $mask];
89
90
        return $this;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function getType()
97
    {
98
        return 'ip_range';
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getArray()
105
    {
106
        if ($this->getField() && !empty($this->ranges)) {
107
            return [
108
                'field' => $this->getField(),
109
                'ranges' => array_values($this->ranges),
110
            ];
111
        }
112
        throw new \LogicException('Ip range aggregation must have field set and range added.');
113
    }
114
}
115