Conditions | 13 |
Paths | 4608 |
Total Lines | 118 |
Code Lines | 62 |
Lines | 0 |
Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
170 | public function index() { |
||
171 | $data = array( |
||
172 | 'Content' => $this->Content, |
||
173 | 'Title' => $this->Title, |
||
174 | 'SearchPerformed' => false |
||
175 | ); |
||
176 | |||
177 | // record the time |
||
178 | $startTime = microtime(true); |
||
179 | |||
180 | //instance of ElasticPage associated with this controller |
||
181 | $ep = Controller::curr()->dataRecord; |
||
182 | |||
183 | // use an Elastic Searcher, which needs primed from URL params |
||
184 | $es = new ElasticSearcher(); |
||
185 | |||
186 | // start, and page length, i.e. pagination |
||
187 | $startParam = $this->request->getVar('start'); |
||
188 | $start = isset($startParam) ? $startParam : 0; |
||
189 | $es->setStart($start); |
||
190 | $es->setPageLength($ep->ResultsPerPage); |
||
191 | |||
192 | |||
193 | // Do not show suggestions if this flag is set |
||
194 | $ignoreSuggestions = isset($this->request->getVar('is')); |
||
195 | |||
196 | |||
197 | // query string |
||
198 | $queryTextParam = $this->request->getVar('q'); |
||
199 | $queryText = isset($queryTextParam) ? $queryTextParam : ''; |
||
200 | |||
201 | $testMode = isset($this->request->getVar('TestMode')); |
||
202 | |||
203 | // filters for aggregations |
||
204 | $ignore = \Config::inst()->get('Elastica', 'BlackList'); |
||
205 | foreach($this->request->getVars() as $key => $value) { |
||
206 | if(!in_array($key, $ignore)) { |
||
207 | $es->addFilter($key, $value); |
||
208 | } |
||
209 | } |
||
210 | |||
211 | // filter by class or site tree |
||
212 | if($ep->SiteTreeOnly) { |
||
213 | $es->addFilter('IsInSiteTree', true); |
||
214 | } else { |
||
215 | $es->setClasses($ep->ClassesToSearch); |
||
216 | } |
||
217 | |||
218 | // set the optional aggregation manipulator |
||
219 | // In the event of a manipulator being present, show all the results for search |
||
220 | // Otherwise aggregations are all zero |
||
221 | if($this->SearchHelper) { |
||
222 | $es->setQueryResultManipulator($this->SearchHelper); |
||
223 | $es->showResultsForEmptySearch(); |
||
224 | } else { |
||
225 | $es->hideResultsForEmptySearch(); |
||
226 | } |
||
227 | |||
228 | // get the edited fields to search from the database for this search page |
||
229 | // Convert this into a name => weighting array |
||
230 | $fieldsToSearch = array(); |
||
231 | $editedSearchFields = $this->ElasticaSearchableFields()->filter(array( |
||
232 | 'Active' => true, |
||
233 | 'Searchable' => true |
||
234 | )); |
||
235 | |||
236 | foreach($editedSearchFields->getIterator() as $searchField) { |
||
237 | $fieldsToSearch[$searchField->Name] = $searchField->Weight; |
||
238 | } |
||
239 | |||
240 | $paginated = null; |
||
241 | try { |
||
242 | // Simulate server being down for testing purposes |
||
243 | if(isset($this->request->getVar('ServerDown')) { |
||
244 | throw new Elastica\Exception\Connection\HttpException('Unable to reach search server'); |
||
245 | } |
||
246 | |||
247 | // now actually perform the search using the original query |
||
248 | $paginated = $es->search($queryText, $fieldsToSearch, $testMode); |
||
249 | |||
250 | // This is the case of the original query having a better one suggested. Do a |
||
251 | // second search for the suggested query, throwing away the original |
||
252 | if($es->hasSuggestedQuery() && !$ignoreSuggestions) { |
||
253 | $data['SuggestedQuery'] = $es->getSuggestedQuery(); |
||
254 | $data['SuggestedQueryHighlighted'] = $es->getSuggestedQueryHighlighted(); |
||
255 | //Link for if the user really wants to try their original query |
||
256 | $sifLink = rtrim($this->Link(), '/') . '?q=' . $queryText . '&is=1'; |
||
257 | $data['SearchInsteadForLink'] = $sifLink; |
||
258 | $paginated = $es->search($es->getSuggestedQuery(), $fieldsToSearch); |
||
259 | |||
260 | } |
||
261 | |||
262 | // calculate time |
||
263 | $endTime = microtime(true); |
||
264 | $elapsed = round(100 * ($endTime - $startTime)) / 100; |
||
265 | |||
266 | // store variables for the template to use |
||
267 | $data['ElapsedTime'] = $elapsed; |
||
268 | $this->Aggregations = $es->getAggregations(); |
||
269 | $data['SearchResults'] = $paginated; |
||
270 | $data['SearchPerformed'] = true; |
||
271 | $data['NumberOfResults'] = $paginated->getTotalItems(); |
||
272 | |||
273 | } catch (Elastica\Exception\Connection\HttpException $e) { |
||
274 | $data['ErrorMessage'] = 'Unable to connect to search server'; |
||
275 | $data['SearchPerformed'] = false; |
||
276 | } |
||
277 | |||
278 | $data['OriginalQuery'] = $queryText; |
||
279 | $data['IgnoreSuggestions'] = $ignoreSuggestions; |
||
280 | |||
281 | if($this->has_extension('PageControllerTemplateOverrideExtension')) { |
||
282 | return $this->useTemplateOverride($data); |
||
283 | } else { |
||
284 | return $data; |
||
285 | } |
||
286 | } |
||
287 | |||
288 | |||
356 |