Completed
Pull Request — master (#158)
by Simonas
106:46 queued 41:36
created

MultiTermChoice::modifySearch()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 7
loc 7
rs 9.4285
cc 3
eloc 4
nc 2
nop 3
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\FilterManagerBundle\Filter\Widget\Choice;
13
14
use ONGR\ElasticsearchDSL\Query\TermsQuery;
15
use ONGR\ElasticsearchDSL\Search;
16
use ONGR\FilterManagerBundle\Filter\FilterState;
17
use ONGR\FilterManagerBundle\Filter\ViewData;
18
use ONGR\FilterManagerBundle\Search\SearchRequest;
19
use Symfony\Component\HttpFoundation\Request;
20
21
/**
22
 * This class provides multiple terms choice filter.
23
 */
24
class MultiTermChoice extends SingleTermChoice
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 View Code Duplication
    public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null)
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...
30
    {
31
        if ($state && $state->isActive()) {
32
            $filter = new TermsQuery($this->getField(), $state->getValue());
33
            $search->addPostFilter($filter);
34
        }
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getState(Request $request)
41
    {
42
        $value = $request->get($this->getRequestField());
43
44
        if (isset($value) && $value !== '' && !is_array($value)) {
45
            $request->query->set($this->getRequestField(), [ $value ]);
46
        }
47
48
        return parent::getState($request);
49
    }
50
51
    /**
52
     * Returns url with selected term applied.
53
     *
54
     * @param string   $key
55
     * @param ViewData $data
56
     *
57
     * @return array
58
     */
59
    protected function getOptionUrlParameters($key, ViewData $data)
60
    {
61
        $parameters = $data->getUrlParameters();
62
63
        if (isset($parameters[$this->getRequestField()])) {
64
            $parameters[$this->getRequestField()][] = $key;
65
        } else {
66
            $parameters[$this->getRequestField()] = [$key];
67
        }
68
69
        return $parameters;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    protected function getUnsetUrlParameters($key, ViewData $data)
76
    {
77
        $parameters = $data->getUrlParameters();
78
79
        if (isset($parameters[$this->getRequestField()]) && count($parameters[$this->getRequestField()]) > 1) {
80
            $parameters[$this->getRequestField()] = array_values(
81
                array_diff($parameters[$this->getRequestField()], [$key])
82
            );
83
        } else {
84
            $parameters = $data->getResetUrlParameters();
85
        }
86
87
        return $parameters;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    protected function isChoiceActive($key, ViewData $data)
94
    {
95
        return $data->getState()->isActive() && in_array($key, $data->getState()->getValue());
96
    }
97
}
98