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

RangedAggregation   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%
Metric Value
wmc 10
lcom 2
cbo 2
dl 0
loc 64
ccs 34
cts 34
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A addRange() 0 3 1
A getRangeAgg() 0 3 1
B getFilter() 0 21 5
A getByTitle() 0 3 1
A getTitles() 0 3 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