Completed
Pull Request — master (#203)
by
unknown
61:13
created

MultiDynamicAggregate::modifySearch()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 3
eloc 3
nc 2
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
        if ($state && $state->isActive()) {
47
            $search->addPostFilter($this->getFilterQuery($state->getValue()));
48
        }
49
    }
50
51
    /**
52
     * A method used to add an additional filter to the aggregations
53
     * in preProcessSearch
54
     *
55
     * @param FilterAggregation $filterAggregation
56
     * @param NestedAggregation $deepLevelAggregation
57
     * @param array             $terms Terms of additional filter
58
     * @param string            $aggName
59
     *
60
     * @return BuilderInterface
61
     */
62
    protected function addSubFilterAggregation(
63
        $filterAggregation,
64
        &$deepLevelAggregation,
65
        $terms,
66
        $aggName
67
    ) {
68
        $boolQuery = $this->getFilterQuery($terms);
69
70
        if ($boolQuery->getQueries() == []) {
71
            $boolQuery->add(new MatchAllQuery());
72
        }
73
74
        $innerFilterAggregation = new FilterAggregation(
0 ignored issues
show
Deprecated Code introduced by
The class ONGR\ElasticsearchDSL\Ag...ation\FilterAggregation has been deprecated with message: Aggregations was moved to it's type namespace. Add `Metric` or `Bucketing` after `Aggregation`. This class will be removed in 3.0.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
75
            $aggName,
76
            $boolQuery
77
        );
78
        $innerFilterAggregation->addAggregation($deepLevelAggregation);
79
        $filterAggregation->addAggregation($innerFilterAggregation);
80
    }
81
82
    /**
83
     * @param string   $key
84
     * @param string   $name
85
     * @param ViewData $data
86
     * @param bool     $active True when the choice is active
87
     *
88
     * @return array
89
     */
90
    protected function getOptionUrlParameters($key, $name, ViewData $data, $active)
91
    {
92
        $value = $data->getState()->getValue();
93
        $parameters = $data->getResetUrlParameters();
94
95 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...
96
            if ($active) {
97
                unset($value[$name][array_search($key, $value[$name])]);
98
                $parameters[$this->getRequestField()] = $value;
99
100
                return $parameters;
101
            }
102
103
            $parameters[$this->getRequestField()] = $value;
104
        }
105
106
        $parameters[$this->getRequestField()][$name][] = $key;
107
108
        return $parameters;
109
    }
110
111
    /**
112
     * Returns whether choice with the specified key is active.
113
     *
114
     * @param string   $key
115
     * @param ViewData $data
116
     * @param string   $activeName
117
     *
118
     * @return bool
119
     */
120 View Code Duplication
    protected function isChoiceActive($key, ViewData $data, $activeName)
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...
121
    {
122
        if ($data->getState()->isActive()) {
123
            $value = $data->getState()->getValue();
124
125
            if (isset($value[$activeName]) && in_array($key, $value[$activeName])) {
126
                return true;
127
            }
128
        }
129
130
        return false;
131
    }
132
133
    /**
134
     * @param array $terms
135
     *
136
     * @return BoolQuery
137
     */
138
    private function getFilterQuery($terms)
139
    {
140
        list($path, $field) = explode('>', $this->getField());
141
        $boolQuery = new BoolQuery();
142
143
        foreach ($terms as $groupName => $values) {
144
            $innerBoolQuery = new BoolQuery();
145
146 View Code Duplication
            foreach ($values as $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...
147
                $nestedBoolQuery = new BoolQuery();
148
                $nestedBoolQuery->add(new TermQuery($field, $value));
149
                $nestedBoolQuery->add(new TermQuery($this->getNameField(), $groupName));
150
                $innerBoolQuery->add(
151
                    new NestedQuery(
152
                        $path,
153
                        $nestedBoolQuery
154
                    ),
155
                    BoolQuery::SHOULD
156
                );
157
            }
158
159
            $boolQuery->add($innerBoolQuery);
160
        }
161
162
        return $boolQuery;
163
    }
164
}
165