Completed
Push — dev2 ( e0c580...5942a1 )
by Gordon
12:38
created

SearchIndexTask::run()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 36
rs 5.3846
cc 8
eloc 23
nc 9
nop 1
1
<?php
2
3
namespace SilverStripe\Elastica;
4
5
/**
6
 * Defines and refreshes the elastic search index.
7
 */
8
class SearchIndexTask extends \BuildTask {
9
10
	protected $title = 'Elastic Search Reindex';
11
12
	protected $description = 'Searches the elastic search index';
13
14
	/**
15
	 * @var ElasticaService
16
	 */
17
	private $service;
18
19
	public function __construct(ElasticaService $service) {
20
		$this->service = $service;
21
	}
22
23
	public function run($request) {
24
		$message = ElasticaUtil::getPrinter();
25
26
		$locales = array();
27
28
		if ($this->locale == null) {
29
			if (class_exists('Translatable') && \SiteTree::has_extension('Translatable')) {
30
				$this->locale = \Translatable::get_current_locale();
0 ignored issues
show
Bug introduced by
The property locale does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
			} else {
32
				foreach (\Translatable::get_existing_content_languages('SiteTree') as $code => $val) {
33
					array_push($locales, $code);
34
				}
35
			}
36
		}
37
38
		// search SiteTree showing highlights
39
		$query = $request->getVar('q');
40
		$es = new \ElasticSearcher();
41
		$es->setStart(0);
42
		$es->setPageLength(20);
43
		$es->addFilter('IsInSiteTree', true);
44
		$results = $es->search($query);
45
		foreach ($results as $result) {
46
			$title = '['.$result->ClassName.', '.$result->ID.']  ';
47
			$title .= $result->Title;
48
			$message($title);
49
			if ($result->SearchHighlightsByField->Content) {
50
				foreach ($result->SearchHighlightsByField->Content as $highlight) {
51
					$message("- ".$highlight->Snippet);
52
				}
53
			}
54
55
			echo "\n\n";
56
		}
57
58
	}
59
60
}
61