ElasticaAutoCompleteController::search()   B
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20
Metric Value
dl 0
loc 46
ccs 0
cts 33
cp 0
rs 8.6316
cc 4
eloc 28
nc 8
nop 0
crap 20
1
<?php
2
use \SilverStripe\Elastica\ElasticSearcher;
3
4
class ElasticaAutoCompleteController extends Controller {
5
	private static $url_handlers = array(
0 ignored issues
show
Unused Code introduced by
The property $url_handlers is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
6
		'search' => 'search'
7
	);
8
9
10
	private static $allowed_actions = array('search');
11
12
13
	public function search() {
14
		$es = new ElasticSearcher();
15
		$query = $this->request->getVar('query');
16
		$query = trim($query);
17
		$classes = $this->request->getVar('classes');
18
		$filter = $this->request->getVar('filter');
19
20
		// Makes most sense to only provide one field here, e.g. Title, Name
21
		$field = $this->request->getVar('field');
22
23
		error_log('QUERY:'.$query);
24
25
		// start, and page length, i.e. pagination
26
		$es->setPageLength(10);
27
		if ($classes) {
28
			$es->setClasses($classes);
29
		}
30
31
		if ($filter) {
32
			$es->addFilter('IsInSiteTree', true);
33
		}
34
35
		$resultList = $es->autocomplete_search($query,$field);
36
		$result = array();
37
		$result['Query'] = $query;
38
		$suggestions = array();
39
40
		foreach ($resultList->getResults() as $singleResult) {
41
			$suggestion = array('value' => $singleResult->Title);
42
			$suggestion['data'] = array(
43
				'ID' => $singleResult->getParam('_id'),
44
				'Class' => $singleResult->getParam('_type'),
45
				'Link' => $singleResult->Link
46
			);
47
			array_push($suggestions, $suggestion);
48
		}
49
50
		$result['suggestions'] = $suggestions;
51
52
53
		$json = json_encode($result);
54
55
		$this->response->addHeader('Content-Type', 'application/json');
56
		//$this->response->setBody($json);
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
57
		return $json;
58
	}
59
}
60