Completed
Push — master ( 4059cd...ef58c8 )
by Simonas
84:03 queued 19:25
created

MultiDynamicAggregate::fetchAggregation()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 24
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 24
loc 24
rs 8.6845
cc 4
eloc 13
nc 4
nop 3
1
<?php
2
3
namespace ONGR\FilterManagerBundle\Filter\Widget\Dynamic;
4
5
use ONGR\ElasticsearchBundle\Result\Aggregation\AggregationValue;
6
use ONGR\ElasticsearchBundle\Result\DocumentIterator;
7
use ONGR\ElasticsearchDSL\Aggregation\FilterAggregation;
8
use ONGR\ElasticsearchDSL\Aggregation\NestedAggregation;
9
use ONGR\ElasticsearchDSL\Aggregation\TermsAggregation;
10
use ONGR\ElasticsearchDSL\BuilderInterface;
11
use ONGR\ElasticsearchDSL\Query\BoolQuery;
12
use ONGR\ElasticsearchDSL\Query\MatchAllQuery;
13
use ONGR\ElasticsearchDSL\Query\NestedQuery;
14
use ONGR\ElasticsearchDSL\Query\TermQuery;
15
use ONGR\ElasticsearchDSL\Query\TermsQuery;
16
use ONGR\ElasticsearchDSL\Search;
17
use ONGR\FilterManagerBundle\Filter\FilterState;
18
use ONGR\FilterManagerBundle\Filter\ViewData;
19
use ONGR\FilterManagerBundle\Search\SearchRequest;
20
use Symfony\Component\HttpFoundation\Request;
21
22
class MultiDynamicAggregate extends DynamicAggregate
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getState(Request $request)
28
    {
29
        $state = new FilterState();
30
        $value = $request->get($this->getRequestField());
31
32
        if (isset($value) && is_array($value) && reset($value) && is_array(reset($value))) {
33
            $state->setActive(true);
34
            $state->setValue($value);
35
            $state->setUrlParameters([$this->getRequestField() => $value]);
36
        }
37
38
        return $state;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null)
45
    {
46
        list($path, $field) = explode('>', $this->getField());
47
48
        if ($state && $state->isActive()) {
49
            $boolQuery = new BoolQuery();
50
51
            foreach ($state->getValue() as $groupName => $values) {
52
                $innerBoolQuery = new BoolQuery();
53
54
                foreach ($values as $value) {
55
                    $innerBoolQuery->add(
56
                        new NestedQuery(
57
                            $path,
58
                            new TermQuery($field, $value)
59
                        ),
60
                        BoolQuery::SHOULD
61
                    );
62
                }
63
64
                $boolQuery->add($innerBoolQuery);
65
            }
66
67
            $search->addPostFilter($boolQuery);
68
        }
69
    }
70
71
    /**
72
     * A method used to add an additional filter to the aggregations
73
     * in preProcessSearch
74
     *
75
     * @param FilterAggregation $filterAggregation
76
     * @param NestedAggregation $deepLevelAggregation
77
     * @param array             $terms Terms of additional filter
78
     * @param string            $aggName
79
     *
80
     * @return BuilderInterface
81
     */
82 View Code Duplication
    protected function addSubFilterAggregation(
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...
83
        $filterAggregation,
84
        $deepLevelAggregation,
85
        $terms,
86
        $aggName
87
    ) {
88
        list($path, $field) = explode('>', $this->getField());
89
        $boolQuery = new BoolQuery();
90
91
        foreach ($terms as $namedTerms) {
92
            $boolQuery->add(
93
                new NestedQuery($path, new TermsQuery($field, array_values($namedTerms)))
94
            );
95
        }
96
97
        if ($boolQuery->getQueries() == []) {
98
            $boolQuery->add(new MatchAllQuery());
99
        }
100
101
        $innerFilterAggregation = new FilterAggregation(
102
            $aggName,
103
            $boolQuery
104
        );
105
        $innerFilterAggregation->addAggregation($deepLevelAggregation);
106
        $filterAggregation->addAggregation($innerFilterAggregation);
107
    }
108
109
    /**
110
     * @param string   $key
111
     * @param string   $name
112
     * @param ViewData $data
113
     * @param bool     $active True when the choice is active
114
     *
115
     * @return array
116
     */
117
    protected function getOptionUrlParameters($key, $name, ViewData $data, $active)
118
    {
119
        $value = $data->getState()->getValue();
120
        $parameters = $data->getResetUrlParameters();
121
122 View Code Duplication
        if (!empty($value)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
123
            if ($active) {
124
                unset($value[$name][array_search($key, $value[$name])]);
125
                $parameters[$this->getRequestField()] = $value;
126
127
                return $parameters;
128
            }
129
130
            $parameters[$this->getRequestField()] = $value;
131
        }
132
133
        $parameters[$this->getRequestField()][$name][] = $key;
134
135
        return $parameters;
136
    }
137
138
    /**
139
     * Returns whether choice with the specified key is active.
140
     *
141
     * @param string   $key
142
     * @param ViewData $data
143
     * @param string   $activeName
144
     *
145
     * @return bool
146
     */
147
    protected function isChoiceActive($key, ViewData $data, $activeName)
148
    {
149
        if ($data->getState()->isActive()) {
150
            $value = $data->getState()->getValue();
151
152
            if (isset($value[$activeName]) && in_array($key, $value[$activeName])) {
153
                return true;
154
            }
155
        }
156
157
        return false;
158
    }
159
}
160