Completed
Push — master ( e2173c...0d60f0 )
by Simonas
01:10
created

FilterContainer::buildSearch()   A

Complexity

Conditions 3
Paths 4

Duplication

Lines 12
Ratio 100 %

Size

Total Lines 12
Code Lines 6

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 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\Search;
13
14
use Doctrine\Common\Cache\Cache;
15
use ONGR\ElasticsearchDSL\Search;
16
use ONGR\FilterManagerBundle\Filter\FilterInterface;
17
use ONGR\FilterManagerBundle\Relation\FilterIterator;
18
use ONGR\FilterManagerBundle\Relation\RelationInterface;
19
use Symfony\Component\HttpFoundation\ParameterBag;
20
use Symfony\Component\HttpFoundation\Request;
21
22
/**
23
 * This class holds collection of FilterInterface objects labeled by name.
24
 */
25
class FilterContainer extends ParameterBag
26
{
27
    /**
28
     * @var Cache
29
     */
30
    private $cache;
0 ignored issues
show
Unused Code introduced by Tomas Pocevičius
The property $cache is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
31
32
    /**
33
     * @var int
34
     */
35
    private $lifeTime;
0 ignored issues
show
Unused Code introduced by Tomas Pocevičius
The property $lifeTime is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
36
37
    /**
38
     * @var array
39
     */
40
    private $exclude = [];
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function add(array $parameters = [])
46
    {
47
        foreach ($parameters as $key => $value) {
48
            $this->set($key, $value);
49
        }
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function set($key, $value)
56
    {
57
        if ($value instanceof FilterInterface) {
58
            parent::set($key, $value);
59
        }
60
    }
61
62
    /**
63
     * Sets array of filter names not to be cached
64
     *
65
     * @param array $exclude
66
     */
67
    public function setExclude(array $exclude)
68
    {
69
        $this->exclude = $exclude;
70
    }
71
72
    /**
73
     * Filters accepted.
74
     *
75
     * @param RelationInterface $relation
76
     *
77
     * @return FilterInterface[]|FilterIterator
78
     */
79
    public function getFiltersByRelation(RelationInterface $relation)
80
    {
81
        return new FilterIterator($this->getIterator(), $relation);
82
    }
83
84
    /**
85
     * Builds search request according to given filters.
86
     *
87
     * @param Request $request
88
     *
89
     * @return SearchRequest
90
     */
91 View Code Duplication
    public function buildSearchRequest(Request $request)
0 ignored issues
show
Duplication introduced by ONGR Team
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...
92
    {
93
        $search = new SearchRequest();
94
        /** @var FilterInterface[] $filters */
95
        $filters = $this->all();
96
97
        foreach ($filters as $name => $filter) {
98
            $state = $filter->getState($request);
99
            $state->setName($name);
100
            $search->set($name, $state);
101
        }
102
103
        return $search;
104
    }
105
106
    /**
107
     * Builds elastic search query by given SearchRequest and filters.
108
     *
109
     * @param SearchRequest       $request
110
     * @param \ArrayIterator|null $filters
111
     *
112
     * @return Search
113
     */
114 View Code Duplication
    public function buildSearch(SearchRequest $request, $filters = null)
0 ignored issues
show
Duplication introduced by ONGR Team
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...
115
    {
116
        $search = new Search();
117
118
        /** @var FilterInterface[] $filters */
119
        $filters = $filters ? $filters : $this->all();
120
        foreach ($filters as $name => $filter) {
121
            $filter->modifySearch($search, $request->get($name), $request);
122
        }
123
124
        return $search;
125
    }
126
}
127