Completed
Push — master ( 542ec1...80785b )
by
unknown
02:03
created

AddonsController   C

Complexity

Total Complexity 30

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 25

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 30
c 2
b 1
f 0
lcom 1
cbo 25
dl 0
loc 207
rs 5

10 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 3 1
A setElasticaService() 0 3 1
A addon() 0 11 2
A vendor() 0 10 2
A Title() 0 3 1
A Link() 0 7 2
A ListView() 0 8 2
F Addons() 0 75 17
B AddonsSearchForm() 0 37 1
A rss() 0 15 1
1
<?php
2
3
use Elastica\Query;
4
use Elastica\Query\Match;
5
use SilverStripe\Elastica\ElasticaService;
6
use SilverStripe\Elastica\ResultList;
7
8
/**
9
 * Lists and searches add-ons.
10
 */
11
class AddonsController extends SiteController {
12
13
	public static $url_handlers = array(
14
		'rss' => 'rss',
15
		'$Vendor!/$Name!' => 'addon',
16
		'$Vendor!' => 'vendor',
17
	);
18
19
	public static $allowed_actions = array(
20
		'index',
21
		'addon',
22
		'vendor',
23
		'rss',
24
	);
25
26
	public static $dependencies = array(
27
		'ElasticaService' => '%$ElasticaService'
28
	);
29
30
	/**
31
	 * @var \SilverStripe\Elastica\ElasticaService
32
	 */
33
	private $elastica;
34
35
	public function index() {
36
		return $this->renderWith(array('Addons', 'Page'));
37
	}
38
39
	public function setElasticaService(ElasticaService $elastica) {
40
		$this->elastica = $elastica;
41
	}
42
43
	public function addon($request) {
44
		$vendor = $request->param('Vendor');
45
		$name = $request->param('Name');
46
		$addon = Addon::get()->filter('Name', "$vendor/$name")->first();
47
48
		if (!$addon) {
49
			$this->httpError(404);
50
		}
51
52
		return new AddonController($this, $addon);
53
	}
54
55
	public function vendor($request) {
56
		$name = $request->param('Vendor');
57
		$vendor = AddonVendor::get()->filter('Name', $name)->first();
58
59
		if (!$vendor) {
60
			$this->httpError(404);
61
		}
62
63
		return new VendorController($this, $vendor);
64
	}
65
66
	public function Title() {
67
		return 'Add-ons';
68
	}
69
70
	public function Link($slug = null) {
71
		if($slug){
72
			return Controller::join_links(Director::baseURL(), 'add-ons', $slug);
73
		} else {
74
			return Controller::join_links(Director::baseURL(), 'add-ons');
75
		}
76
	}
77
78
	public function ListView() {
79
		$view = $this->request->getVar('view');
80
		if($view) {
81
			return $view;
82
		} else {
83
			return 'list';
84
		}
85
	}
86
87
	public function Addons() {
88
		$list = Addon::get();
89
90
		$search = $this->request->getVar('search');
91
		$type = $this->request->getVar('type');
92
		$compat = $this->request->getVar('compatibility');
93
		$tags = $this->request->getVar('tags');
94
		$sort = $this->request->getVar('sort');
95
		$view = $this->request->getVar('view');
96
97
		if (!$view) {
98
			$view = 'list';
0 ignored issues
show
Unused Code introduced by
$view 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...
99
		}
100
101
		if (!in_array($sort, array('name', 'downloads', 'newest'))) {
102
			$sort = null;
103
		}
104
105
		// Proxy out a search to elastic if any parameters are set.
106
		if ($search || $type || $compat || $tags) {
107
108
			$bool = new Query\BoolQuery();
109
110
			$query = new Query();
111
			$query->setQuery($bool);
112
			$query->setSize(count($list));
113
114
115
			if ($search) {
116
				$match = new Match();
117
				$match->setField('_all', $search);
118
119
				$bool->addMust($match);
120
			}
121
122
			if ($type) {
123
				$bool->addMust(new Query\Term(array('type' => $type)));
124
			}
125
126
			if ($compat) {
127
				$bool->addMust(new Query\Terms('compatibility', (array) $compat));
128
			}
129
130
			if ($tags) {
131
				$bool->addMust(new Query\Terms('tag', (array) $tags));
132
			}
133
134
			$list = new ResultList($this->elastica->getIndex(), $query);
135
136
			if ($sort) {
137
				$ids = $list->column('ID');
138
139
				if ($ids) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ids of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
140
					$list = Addon::get()->byIDs($ids);
141
				} else {
142
					$list = new ArrayList();
143
				}
144
			} else {
145
				$list = $list->toArrayList();
146
			}
147
		} else {
148
			if (!$sort) $sort = 'downloads';
149
		}
150
151
		switch ($sort) {
152
			case 'name': $list = $list->sort('Name'); break;
153
			case 'newest': $list = $list->sort('Released', 'DESC'); break;
154
			case 'downloads': $list = $list->sort('Downloads', 'DESC'); break;
155
		}
156
157
		$list = new PaginatedList($list, $this->request);
0 ignored issues
show
Documentation introduced by
$this->request is of type object<SS_HTTPRequest>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
158
		$list->setPageLength(16);
159
160
		return $list;
161
	}
162
163
	public function AddonsSearchForm() {
164
		$form = new Form(
165
			$this,
166
			'AddonsSearchForm',
167
			new FieldList(array(
168
				TextField::create('search', 'Search for')
169
					->setValue($this->request->getVar('search'))
170
					->addExtraClass('input-block-level'),
171
				DropdownField::create('sort', 'Sort by')
172
					->setSource(array(
173
						'name' => 'Name',
174
						'downloads' => 'Most downloaded',
175
						'newest' => 'Newest'
176
					))
177
					->setEmptyString('Best match')
178
					->setValue($this->request->getVar('sort'))
179
					->addExtraClass('input-block-level'),
180
				DropdownField::create('type', 'Add-on type')
181
					->setSource(array(
182
						'module' => 'Modules',
183
						'theme' => 'Themes'
184
					))
185
					->setEmptyString('Modules and themes')
186
					->setValue($this->request->getVar('type'))
187
					->addExtraClass('input-block-level'),
188
				CheckboxSetField::create('compatibility', 'Compatible SilverStripe versions')
189
					->setSource(SilverStripeVersion::get()->map('Name', 'Name'))
0 ignored issues
show
Documentation introduced by
\SilverStripeVersion::get()->map('Name', 'Name') is of type object<SS_Map>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
190
					->setValue($this->request->getVar('compatibility'))
191
					->setTemplate('AddonsSearchCheckboxSetField')
192
			)),
193
			new FieldList()
194
		);
195
196
		return $form
197
			->setFormMethod('GET')
198
			->setFormAction($this->Link());
199
	}
200
201
	public function rss($request, $limit = 10) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
202
		$addons = Addon::get()
203
			->sort('Released', 'DESC')
204
			->limit($limit);
205
206
		$rss = new RSSFeed(
207
			$addons, 
208
			$this->Link(), 
209
			"Newest addons on addons.silverstripe.org",
210
			null,
211
			'RSSTitle'
212
		);
213
214
    	return $rss->outputToBrowser();
215
	}
216
217
}
218