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

FilterAggregation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 62
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A setFilter() 0 4 1
A setField() 0 4 1
A getArray() 0 8 2
A getType() 0 4 1
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
use ONGR\ElasticsearchDSL\BuilderInterface;
17
18
/**
19
 * Class representing FilterAggregation.
20
 *
21
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html
22
 */
23
class FilterAggregation extends AbstractAggregation
24
{
25
    use BucketingTrait;
26
27
    /**
28
     * @var BuilderInterface
29
     */
30
    protected $filter;
31
32
    /**
33
     * Inner aggregations container init.
34
     *
35
     * @param string           $name
36
     * @param BuilderInterface $filter
37
     */
38
    public function __construct($name, BuilderInterface $filter = null)
39
    {
40
        parent::__construct($name);
41
42
        if ($filter !== null) {
43
            $this->setFilter($filter);
44
        }
45
    }
46
47
    /**
48
     * Sets a filter.
49
     *
50
     * @param BuilderInterface $filter
51
     */
52
    public function setFilter(BuilderInterface $filter)
53
    {
54
        $this->filter = $filter;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function setField($field)
61
    {
62
        throw new \LogicException("Filter aggregation, doesn't support `field` parameter");
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getArray()
69
    {
70
        if (!$this->filter) {
71
            throw new \LogicException("Filter aggregation `{$this->getName()}` has no filter added");
72
        }
73
74
        return $this->filter->toArray();
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getType()
81
    {
82
        return 'filter';
83
    }
84
}
85