These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\NodeSearchBundle\Services; |
||
4 | |||
5 | use Elastica\Aggregation\Terms; |
||
6 | use Kunstmaan\NodeBundle\Helper\RenderContext; |
||
7 | use Kunstmaan\NodeSearchBundle\PagerFanta\Adapter\SearcherRequestAdapter; |
||
8 | use Kunstmaan\NodeSearchBundle\Search\AbstractElasticaSearcher; |
||
9 | use Pagerfanta\Exception\NotValidCurrentPageException; |
||
10 | use Pagerfanta\Pagerfanta; |
||
11 | use Symfony\Component\DependencyInjection\Container; |
||
12 | use Symfony\Component\DependencyInjection\ContainerInterface; |
||
13 | use Symfony\Component\HttpFoundation\Request; |
||
14 | use Symfony\Component\HttpFoundation\RequestStack; |
||
15 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
16 | |||
17 | /** |
||
18 | * Class SearchService |
||
19 | * @package Kunstmaan\NodeSearchBundle\Services |
||
20 | */ |
||
21 | class SearchService |
||
22 | { |
||
23 | /** |
||
24 | * @var RenderContext |
||
25 | * |
||
26 | */ |
||
27 | protected $renderContext; |
||
28 | |||
29 | /** |
||
30 | * @var Container |
||
31 | */ |
||
32 | protected $container; |
||
33 | |||
34 | /** |
||
35 | * @var RequestStack |
||
36 | * |
||
37 | */ |
||
38 | protected $requestStack; |
||
39 | |||
40 | /** |
||
41 | * @var int |
||
42 | */ |
||
43 | protected $defaultPerPage; |
||
44 | |||
45 | /** |
||
46 | * @param ContainerInterface $container |
||
47 | * @param RequestStack $requestStack |
||
48 | * @param int $defaultPerPage |
||
49 | */ |
||
50 | public function __construct(ContainerInterface $container, RequestStack $requestStack, $defaultPerPage = 10) |
||
51 | { |
||
52 | $this->container = $container; |
||
0 ignored issues
–
show
|
|||
53 | $this->requestStack = $requestStack; |
||
54 | $this->defaultPerPage = $defaultPerPage; |
||
55 | $this->renderContext = new RenderContext(); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @param int $defaultPerPage |
||
60 | * |
||
61 | */ |
||
62 | public function setDefaultPerPage($defaultPerPage) |
||
63 | { |
||
64 | $this->defaultPerPage = $defaultPerPage; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @return RenderContext |
||
69 | */ |
||
70 | public function getRenderContext() |
||
71 | { |
||
72 | return $this->renderContext; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param RenderContext $renderContext |
||
77 | */ |
||
78 | public function setRenderContext($renderContext) |
||
79 | { |
||
80 | $this->renderContext = $renderContext; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return int |
||
85 | */ |
||
86 | public function getDefaultPerPage() |
||
87 | { |
||
88 | return $this->defaultPerPage; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return Container |
||
93 | */ |
||
94 | public function getContainer() |
||
95 | { |
||
96 | return $this->container; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param Container $container |
||
101 | */ |
||
102 | public function setContainer($container) |
||
103 | { |
||
104 | $this->container = $container; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @return Request |
||
109 | */ |
||
110 | public function getRequest() |
||
111 | { |
||
112 | return $this->requestStack->getCurrentRequest(); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @return Pagerfanta |
||
117 | */ |
||
118 | public function search() |
||
119 | { |
||
120 | $request = $this->requestStack->getCurrentRequest(); |
||
121 | |||
122 | // Retrieve the current page number from the URL, if not present of lower than 1, set it to 1 |
||
123 | $entity = $request->attributes->get('_entity'); |
||
124 | |||
125 | $pageNumber = $this->getRequestedPage($request); |
||
126 | $searcher = $this->container->get($entity->getSearcher()); |
||
127 | $this->applySearchParams($searcher, $request, $this->renderContext); |
||
128 | |||
129 | $adapter = new SearcherRequestAdapter($searcher); |
||
130 | $pagerfanta = new Pagerfanta($adapter); |
||
131 | try { |
||
132 | $pagerfanta |
||
133 | ->setMaxPerPage($this->getDefaultPerPage()) |
||
134 | ->setCurrentPage($pageNumber); |
||
135 | } catch (NotValidCurrentPageException $e) { |
||
136 | throw new NotFoundHttpException(); |
||
137 | } |
||
138 | |||
139 | return $pagerfanta; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param AbstractElasticaSearcher $searcher |
||
144 | * @param Request $request |
||
145 | * @param RenderContext $context |
||
146 | */ |
||
147 | protected function applySearchParams(AbstractElasticaSearcher $searcher, Request $request, RenderContext &$context) |
||
148 | { |
||
149 | // Retrieve the search parameters |
||
150 | $queryString = trim($request->query->get('query')); |
||
151 | $queryType = $request->query->get('type'); |
||
152 | $lang = $request->getLocale(); |
||
153 | |||
154 | $context['q_query'] = $queryString; |
||
155 | $context['q_type'] = $queryType; |
||
156 | |||
157 | $searcher |
||
158 | ->setData($this->sanitizeSearchQuery($queryString)) |
||
159 | ->setContentType($queryType) |
||
160 | ->setLanguage($lang); |
||
161 | |||
162 | $query = $searcher->getQuery(); |
||
163 | |||
164 | // Aggregations |
||
165 | $termsAggregation = new Terms('type'); |
||
166 | $termsAggregation->setField('type'); |
||
167 | |||
168 | $query->addAggregation($termsAggregation); |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Currently we just search for a complete match... |
||
173 | * |
||
174 | * @param string $query |
||
175 | * |
||
176 | * @return string |
||
177 | */ |
||
178 | protected function sanitizeSearchQuery($query) |
||
179 | { |
||
180 | return '"' . $query . '"'; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @param Request $request |
||
185 | * |
||
186 | * @return int |
||
187 | */ |
||
188 | private function getRequestedPage(Request $request) |
||
189 | { |
||
190 | $pageNumber = $request->query->getInt('page', 1); |
||
191 | if (!$pageNumber || $pageNumber < 1) { |
||
192 | $pageNumber = 1; |
||
193 | } |
||
194 | |||
195 | return $pageNumber; |
||
196 | } |
||
197 | } |
||
198 | |||
199 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.