SearchIndexTask   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 53
ccs 0
cts 33
cp 0
rs 10
c 1
b 1
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
C run() 0 36 8
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