Completed
Pull Request — master (#144)
by Mantas
63:58
created

MultiTermChoice   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 73
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A modifySearch() 0 6 3
A getState() 0 10 4
A getOptionUrlParameters() 0 12 2
A getUnsetUrlParameters() 0 14 3
A isChoiceActive() 0 4 2
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\Filter\TermsFilter;
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
    public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null)
30
    {
31
        if ($state && $state->isActive()) {
32
            $search->addPostFilter(new TermsFilter($this->getField(), $state->getValue()));
33
        }
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getState(Request $request)
40
    {
41
        $value = $request->get($this->getRequestField());
42
43
        if (isset($value) && $value !== '' && !is_array($value)) {
44
            $request->query->set($this->getRequestField(), [ $value ]);
45
        }
46
47
        return parent::getState($request);
48
    }
49
50
    /**
51
     * Returns url with selected term applied.
52
     *
53
     * @param string   $key
54
     * @param ViewData $data
55
     *
56
     * @return array
57
     */
58
    protected function getOptionUrlParameters($key, ViewData $data)
59
    {
60
        $parameters = $data->getUrlParameters();
61
62
        if (isset($parameters[$this->getRequestField()])) {
63
            $parameters[$this->getRequestField()][] = $key;
64
        } else {
65
            $parameters[$this->getRequestField()] = [$key];
66
        }
67
68
        return $parameters;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    protected function getUnsetUrlParameters($key, ViewData $data)
75
    {
76
        $parameters = $data->getUrlParameters();
77
78
        if (isset($parameters[$this->getRequestField()]) && count($parameters[$this->getRequestField()]) > 1) {
79
            $parameters[$this->getRequestField()] = array_values(
80
                array_diff($parameters[$this->getRequestField()], [$key])
81
            );
82
        } else {
83
            $parameters = $data->getResetUrlParameters();
84
        }
85
86
        return $parameters;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    protected function isChoiceActive($key, ViewData $data)
93
    {
94
        return $data->getState()->isActive() && in_array($key, $data->getState()->getValue());
95
    }
96
}
97