Completed
Push — dev2 ( 14c647...d5d1ea )
by Gordon
32:41 queued 29:48
created

FlickrPhotoTOElasticaSearchHelper   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 59.46%
Metric Value
wmc 14
lcom 0
cbo 3
dl 0
loc 141
ccs 44
cts 74
cp 0.5946
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A augmentQuery() 0 49 2
A updateAggregation() 0 12 2
C updateFilters() 0 32 7
A getDefaultSort() 0 3 1
A getIndexFieldTitleMapping() 0 3 1
1
<?php
2
use Elastica\Aggregation\Terms;
3
use Elastica\Query;
4
use Elastica\Aggregation\TopHits;
5
use Elastica\Aggregation\Range;
6
use SilverStripe\Elastica\RangedAggregation;
7
8
9
class FlickrPhotoTOElasticaSearchHelper implements ElasticaSearchHelperInterface,TestOnly {
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "TestOnly"; 0 found
Loading history...
10
11 7
	public function __construct() {
12 7
		$aspectAgg = new RangedAggregation('Aspect', 'AspectRatio');
13 7
        $aspectAgg->addRange(0.0000001, 0.3, 'Panoramic');
14 7
        $aspectAgg->addRange(0.3, 0.9, 'Horizontal');
15 7
        $aspectAgg->addRange(0.9, 1.2, 'Square');
16 7
        $aspectAgg->addRange(1.2, 1.79, 'Vertical');
17 7
        $aspectAgg->addRange(1.79, 1e7, 'Tallest');
18 7
	}
19
20
	private static $titleFieldMapping = array(
21
		'ShutterSpeed' => 'Shutter Speed',
22
		'FocalLength35mm' => 'Focal Length'
23
	);
24
25
26
	/**
27
	 * Add a number of facets to the FlickrPhotoTO query
28
	 * @param  \Elastica\Query &$query the existing query object to be augmented.
29
	 */
30 7
	public function augmentQuery(&$query) {
31
		//FIXME probably move this back as default behaviour
32
		// set the order to be taken at in reverse if query is blank other than aggs
33 7
		$params = $query->getParams();
0 ignored issues
show
Unused Code introduced by
$params is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
34
35
		$wildcard = array(
0 ignored issues
show
Unused Code introduced by
$wildcard is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
36 7
			'query_string' => array('query' => '*')
37 7
		);
38
39 7
		if ($query->OriginalQueryText == '') {
40 7
			$query->setSort(array('TakenAt'=> 'desc'));
41 7
		}
42
43
		// add Aperture aggregate
44 7
		$agg1 = new Terms("Aperture");
45 7
		$agg1->setField("Aperture");
46 7
		$agg1->setSize(0);
47 7
		$agg1->setOrder('_term', 'asc');
48 7
		$query->addAggregation($agg1);
49
50
		// add shutter speed aggregate
51 7
		$agg2 = new Terms("ShutterSpeed");
52 7
		$agg2->setField("ShutterSpeed");
53 7
		$agg2->setSize(0);
54 7
		$agg2->setOrder('_term', 'asc');
55 7
		$query->addAggregation($agg2);
56
57
		// this currently needs to be same as the field name
58
		// needs fixed
59
		// Add focal length aggregate, may range this
60 7
		$agg3 = new Terms("FocalLength35mm");
61 7
		$agg3->setField("FocalLength35mm");
62 7
		$agg3->setSize(0);
63 7
		$agg3->setOrder('_term', 'asc');
64 7
		$query->addAggregation($agg3);
65
66
		// add film speed
67 7
		$agg4 = new Terms("ISO");
68 7
		$agg4->setField("ISO");
69 7
		$agg4->setSize(0);
70 7
		$agg4->setOrder('_term', 'asc');
71 7
		$query->addAggregation($agg4);
72
73 7
		$aspectRangedAgg = RangedAggregation::getByTitle('Aspect');
74 7
        $query->addAggregation($aspectRangedAgg->getRangeAgg());
75
76
		// remove NearestTo from the request so it does not get used as a term filter
77 7
		unset(Controller::curr()->request['NearestTo']);
78 7
	}
79
80
81
	/**
82
	 * Update filters, perhaps remaps them, prior to performing a search.
83
	 * This allows for aggregation values to be updated prior to rendering.
84
	 * @param  array &$filters array of key/value pairs for query filtering
85
	 */
86 7
	public function updateFilters(&$filters) {
87
88
		// shutter speed is stored as decimal to 6 decimal places, then a
89
		// vertical bar followed by the displayed speed as a fraction or a
90
		// whole number.  This puts the decimal back for matching purposes
91 7
		if (isset($filters['ShutterSpeed'])) {
92
93
			$sortable = $filters['ShutterSpeed'];
94
95
			$sortable = explode('/', $sortable);
96
			if (sizeof($sortable) == 1) {
97
				$sortable = trim($sortable[0]);
98
99
				if ($sortable === '1') {
100
					$sortable = '1.000000|1';
101
				}
102
103
			} else if (sizeof($sortable) == 2) {
104
				if ($sortable[0] === '' || $sortable[1] === '') {
105
					$sortable = '';
106
107
				} else {
108
					$sortable = floatval($sortable[0])/intval($sortable[1]);
109
					$sortable = round($sortable,6);
110
					$sortable = $sortable . '|' . $filters['ShutterSpeed'];
111
				}
112
113
			}
114
115
			$filters['ShutterSpeed'] = $sortable;
116
		}
117 7
	}
118
119
120
121
	/**
122
	 * Manipulate the results, e.g. fixing up values if issues with ordering in Elastica
123
	 * @param  array &$aggs Aggregates from an Elastica search to be tweaked
124
	 */
125
	public function updateAggregation(&$aggs) {
126
		// the shutter speeds are of the form decimal number | fraction, keep the latter half
127
		$shutterSpeeds = $aggs['ShutterSpeed']['buckets'];
128
		$ctr = 0;
129
		foreach ($shutterSpeeds as $bucket) {
130
			$key = $bucket['key'];
131
			$splits = explode('|', $key);
132
			$shutterSpeeds[$ctr]['key'] = end($splits);
133
			$ctr++;
134
		}
135
		$aggs['ShutterSpeed']['buckets'] = $shutterSpeeds;
136
	}
137
138
	/*
139
	In the event of aggregates being used and no query provided, sort by this (<field> => <order>)
140
	 */
141 7
	public function getDefaultSort() {
142 7
		return array('TakenAt' => 'desc');
143
	}
144
145
146
	public function getIndexFieldTitleMapping() {
147
		return self::$titleFieldMapping;
148
	}
149
}
150