| 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); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 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 |  |  |  | 
            
                        
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.