Completed
Push — dev2 ( 507d7b...9f9038 )
by Gordon
11:12
created

RangedAggregation::getRangeAgg()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace SilverStripe\Elastica;
3
4
class RangedAggregation {
5
6
	private static $ranged_aggregations = array();
7
8
9
	/**
10
	 * @param string $title
11
	 * @param string $field
12
	 */
13 10
	public function __construct($title, $field) {
14 10
		$this->Title = $title;
0 ignored issues
show
Bug introduced by
The property Title 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...
15 10
		$this->Range = new \Elastica\Aggregation\Range($title);
0 ignored issues
show
Bug introduced by
The property Range 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...
16 10
		$this->Range->setField($field);
17 10
		self::$ranged_aggregations[$title] = $this;
18 10
	}
19
20
21
	/**
22
	 * @param double $from
23
	 * @param double $to
24
	 * @param string $name
25
	 */
26 10
	public function addRange($from, $to, $name) {
27 10
		$this->Range->addRange($from, $to, $name);
28 10
	}
29
30
31 9
	public function getRangeAgg() {
32 9
		return $this->Range;
33
	}
34
35
36 2
	public function getFilter($chosenName) {
37 2
		$rangeArray = $this->Range->toArray()['range']['ranges'];
38 2
		$result = null;
39 2
		foreach($rangeArray as $range) {
40 2
			if($range['key'] === $chosenName) {
41 2
				$from = null;
42 2
				$to = null;
43 2
				if(isset($range['from'])) {
44 2
					$from = $range['from'];
45 2
				}
46 2
				if(isset($range['to'])) {
47 2
					$to = $range['to'];
48 2
				}
49 2
				$rangeFilter = array('gte' => (string)$from, 'lt' => (string)$to);
50 2
		        $filter = new \Elastica\Filter\Range('AspectRatio', $rangeFilter);
51 2
		        $result = $filter;
52 2
			}
53 2
		}
54
55 2
		return $result;
56
	}
57
58
59 9
	public static function getByTitle($title) {
60 9
		return self::$ranged_aggregations[$title];
61
	}
62
63
64 9
	public static function getTitles() {
65 9
		return array_keys(self::$ranged_aggregations);
66
	}
67
}
68