Completed
Pull Request — v0.10 (#630)
by Tautrimas
93:19
created

FiltersAggregation   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 63
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getArray() 0 4 1
A getType() 0 4 1
A setAnonymous() 0 6 1
B addFilter() 0 12 5
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
use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
16
17
/**
18
 * Class representing filters aggregation.
19
 */
20
class FiltersAggregation extends AbstractAggregation
21
{
22
    use BucketingTrait;
23
24
    /**
25
     * @var BuilderInterface[]
26
     */
27
    private $filters = [];
28
29
    /**
30
     * @var bool
31
     */
32
    private $anonymous = false;
33
34
    /**
35
     * @param bool $anonymous
36
     *
37
     * @return FiltersAggregation
38
     */
39
    public function setAnonymous($anonymous)
40
    {
41
        $this->anonymous = $anonymous;
42
43
        return $this;
44
    }
45
46
    /**
47
     * @param BuilderInterface $filter
48
     * @param string           $name
49
     *
50
     * @throws \LogicException
51
     *
52
     * @return FiltersAggregation
53
     */
54
    public function addFilter(BuilderInterface $filter, $name = '')
55
    {
56
        if ($this->anonymous === false && empty($name)) {
57
            throw new \LogicException('In not anonymous filters filter name must be set.');
58
        } elseif ($this->anonymous === false && !empty($name)) {
59
            $this->filters['filters'][$name] = [$filter->getType() => $filter->toArray()];
60
        } else {
61
            $this->filters['filters'][] = [$filter->getType() => $filter->toArray()];
62
        }
63
64
        return $this;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getArray()
71
    {
72
        return $this->filters;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getType()
79
    {
80
        return 'filters';
81
    }
82
}
83