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

MultiDynamicAggregate::isChoiceActive()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

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