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

FilterContainer::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
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 ONGR\ElasticsearchDSL\Search;
15
use ONGR\FilterManagerBundle\Filter\FilterInterface;
16
use ONGR\FilterManagerBundle\Relation\FilterIterator;
17
use ONGR\FilterManagerBundle\Relation\RelationInterface;
18
use Symfony\Component\HttpFoundation\ParameterBag;
19
use Symfony\Component\HttpFoundation\Request;
20
21
/**
22
 * This class holds collection of FilterInterface objects labeled by name.
23
 */
24
class FilterContainer extends ParameterBag
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function add(array $parameters = [])
30
    {
31
        foreach ($parameters as $key => $value) {
32
            $this->set($key, $value);
33
        }
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function set($key, $value)
40
    {
41
        if ($value instanceof FilterInterface) {
42
            parent::set($key, $value);
43
        }
44
    }
45
46
    /**
47
     * Filters accepted.
48
     *
49
     * @param RelationInterface $relation
50
     *
51
     * @return FilterInterface[]
52
     */
53
    public function getFiltersByRelation(RelationInterface $relation)
54
    {
55
        return new FilterIterator($this->getIterator(), $relation);
56
    }
57
58
    /**
59
     * Builds search request according to given filters.
60
     *
61
     * @param Request $request
62
     *
63
     * @return SearchRequest
64
     */
65 View Code Duplication
    public function buildSearchRequest(Request $request)
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...
66
    {
67
        $search = new SearchRequest();
68
        /** @var FilterInterface[] $filters */
69
        $filters = $this->all();
70
71
        foreach ($filters as $name => $filter) {
72
            $state = $filter->getState($request);
73
            $state->setName($name);
74
            $search->set($name, $state);
75
        }
76
77
        return $search;
78
    }
79
80
    /**
81
     * Builds elastic search query by given SearchRequest and filters.
82
     *
83
     * @param SearchRequest          $request
84
     * @param FilterInterface[]|null $filters
85
     *
86
     * @return Search
87
     */
88 View Code Duplication
    public function buildSearch(SearchRequest $request, $filters = 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...
89
    {
90
        $search = new Search();
91
92
        /** @var FilterInterface[] $filters */
93
        $filters = $filters ? $filters : $this->all();
94
95
        foreach ($filters as $name => $filter) {
96
            $filter->modifySearch($search, $request->get($name), $request);
97
        }
98
99
        return $search;
100
    }
101
}
102