Completed
Pull Request — master (#168)
by
unknown
61:07
created

FilterContainer::buildSearch()   D

Complexity

Conditions 9
Paths 18

Size

Total Lines 37
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 37
rs 4.909
cc 9
eloc 19
nc 18
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\Filter\FilterState;
18
use ONGR\FilterManagerBundle\Relation\FilterIterator;
19
use ONGR\FilterManagerBundle\Relation\RelationInterface;
20
use Symfony\Component\HttpFoundation\ParameterBag;
21
use Symfony\Component\HttpFoundation\Request;
22
23
/**
24
 * This class holds collection of FilterInterface objects labeled by name.
25
 */
26
class FilterContainer extends ParameterBag
27
{
28
    /**
29
     * @var Cache
30
     */
31
    private $cache = null;
32
33
    /**
34
     * @var int
35
     */
36
    private $lifeTime;
37
38
    /**
39
     * @var array
40
     */
41
    private $exclude = [];
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function add(array $parameters = [])
47
    {
48
        foreach ($parameters as $key => $value) {
49
            $this->set($key, $value);
50
        }
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function set($key, $value)
57
    {
58
        if ($value instanceof FilterInterface) {
59
            parent::set($key, $value);
60
        }
61
    }
62
63
    /**
64
     * Sets cache engine
65
     * 
66
     * @param Cache|null $cache
67
     */
68
    public function setCache(Cache $cache = null)
69
    {
70
        $this->cache = $cache;
71
    }
72
73
    /**
74
     * Sets cached search life time
75
     *
76
     * @param $lifeTime
77
     */
78
    public function setLifeTime($lifeTime)
79
    {
80
        $this->lifeTime = $lifeTime;
81
    }
82
83
    /**
84
     * Sets array of filter names not to be cached
85
     *
86
     * @param array $exclude
87
     */
88
    public function setExclude(array $exclude)
89
    {
90
        $this->exclude = $exclude;
91
    }
92
93
    /**
94
     * Filters accepted.
95
     *
96
     * @param RelationInterface $relation
97
     *
98
     * @return FilterInterface[]
99
     */
100
    public function getFiltersByRelation(RelationInterface $relation)
101
    {
102
        return new FilterIterator($this->getIterator(), $relation);
103
    }
104
105
    /**
106
     * Builds search request according to given filters.
107
     *
108
     * @param Request $request
109
     *
110
     * @return SearchRequest
111
     */
112
    public function buildSearchRequest(Request $request)
113
    {
114
        $search = new SearchRequest();
115
        /** @var FilterInterface[] $filters */
116
        $filters = $this->all();
117
118
        foreach ($filters as $name => $filter) {
119
            $state = $filter->getState($request);
120
            $state->setName($name);
121
            $search->set($name, $state);
122
        }
123
124
        return $search;
125
    }
126
127
    /**
128
     * Builds elastic search query by given SearchRequest and filters.
129
     *
130
     * @param SearchRequest          $request
131
     * @param FilterInterface[]|null $filters
132
     *
133
     * @return Search
134
     */
135
    public function buildSearch(SearchRequest $request, $filters = null)
136
    {
137
        $search = new Search();
138
139
        /** @var FilterInterface[] $filters */
140
        $filters = $filters ? $filters : $this->all();
141
142
        $cachedFilters = [];
143
144
        if ($this->cache) {
145
            foreach ($filters as $name => $filter) {
146
                if (!in_array($name, $this->exclude)) {
147
                    $cachedFilters[$name] = ['filter' => $filter, 'state' => $request->get($name)];
148
                }
149
            }
150
151
            $searchHash = md5(serialize($cachedFilters));
152
153
            if ($this->cache->contains($searchHash)) {
154
                $search = $this->cache->fetch($searchHash);
155
            } else {
156
                foreach ($cachedFilters as $name => $cachedFilter) {
157
                    /** @var FilterInterface[]|FilterState[] $cachedFilter */
158
                    $cachedFilter['filter']->modifySearch($search, $cachedFilter['state'], $request);
0 ignored issues
show
Bug introduced by
It seems like $cachedFilter['state'] can also be of type object<ONGR\FilterManage...Filter\FilterInterface>; however, ONGR\FilterManagerBundle...terface::modifySearch() does only seem to accept null|object<ONGR\FilterM...dle\Filter\FilterState>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
The method modifySearch does only exist in ONGR\FilterManagerBundle\Filter\FilterInterface, but not in ONGR\FilterManagerBundle\Filter\FilterState.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
159
                }
160
                $this->cache->save($searchHash, $search, $this->lifeTime);
161
            }
162
        }
163
164
        foreach ($filters as $name => $filter) {
165
            if (!in_array($name, array_keys($cachedFilters))) {
166
                $filter->modifySearch($search, $request->get($name), $request);
167
            }
168
        }
169
170
        return $search;
171
    }
172
}
173