Completed
Push — master ( 28c895...260a8b )
by Robbie
02:07
created

AddonsController   D

Complexity

Total Complexity 32

Size/Duplication

Total Lines 236
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 25

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 25
dl 0
loc 236
rs 4.8
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 4 1
A setElasticaService() 0 4 1
A addon() 0 12 2
A vendor() 0 11 2
A Title() 0 4 1
A Link() 0 8 2
A ListView() 0 9 2
F Addons() 0 93 19
B AddonsSearchForm() 0 39 1
A rss() 0 16 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
14
    public static $url_handlers = array(
15
        'rss'             => 'rss',
16
        '$Vendor!/$Name!' => 'addon',
17
        '$Vendor!'        => 'vendor',
18
    );
19
20
    public static $allowed_actions = array(
21
        'index',
22
        'addon',
23
        'vendor',
24
        'rss',
25
    );
26
27
    public static $dependencies = array(
28
        'ElasticaService' => '%$ElasticaService'
29
    );
30
31
    /**
32
     * @var \SilverStripe\Elastica\ElasticaService
33
     */
34
    private $elastica;
35
36
    public function index()
37
    {
38
        return $this->renderWith(array('Addons', 'Page'));
39
    }
40
41
    public function setElasticaService(ElasticaService $elastica)
42
    {
43
        $this->elastica = $elastica;
44
    }
45
46
    public function addon($request)
47
    {
48
        $vendor = $request->param('Vendor');
49
        $name = $request->param('Name');
50
        $addon = Addon::get()->filter('Name', "$vendor/$name")->first();
51
52
        if (!$addon) {
53
            $this->httpError(404);
54
        }
55
56
        return new AddonController($this, $addon);
57
    }
58
59
    public function vendor($request)
60
    {
61
        $name = $request->param('Vendor');
62
        $vendor = AddonVendor::get()->filter('Name', $name)->first();
63
64
        if (!$vendor) {
65
            $this->httpError(404);
66
        }
67
68
        return new VendorController($this, $vendor);
69
    }
70
71
    public function Title()
72
    {
73
        return 'Add-ons';
74
    }
75
76
    public function Link($slug = null)
77
    {
78
        if ($slug) {
79
            return Controller::join_links(Director::baseURL(), 'add-ons', $slug);
80
        } else {
81
            return Controller::join_links(Director::baseURL(), 'add-ons');
82
        }
83
    }
84
85
    public function ListView()
86
    {
87
        $view = $this->request->getVar('view');
88
        if ($view) {
89
            return $view;
90
        } else {
91
            return 'list';
92
        }
93
    }
94
95
    public function Addons()
96
    {
97
        $list = Addon::get();
98
99
        $search = $this->request->getVar('search');
100
        $type = $this->request->getVar('type');
101
        $compat = $this->request->getVar('compatibility');
102
        $tags = $this->request->getVar('tags');
103
        $sort = $this->request->getVar('sort');
104
105
        if (!in_array($sort, array('name', 'downloads', 'newest', 'relative'))) {
106
            $sort = null;
107
        }
108
109
        // Proxy out a search to elastic if any parameters are set.
110
        if ($search || $type || $compat || $tags) {
111
112
            $bool = new Query\BoolQuery();
113
114
            $query = new Query();
115
            $query->setQuery($bool);
116
            $query->setSize(count($list));
117
118
119
            if ($search) {
120
                $match = new Match();
121
                $match->setField('_all', $search);
122
123
                $bool->addMust($match);
124
            }
125
126
            if ($type) {
127
                $bool->addMust(new Query\Term(array('type' => $type)));
128
            }
129
130
            if ($compat) {
131
                $bool->addMust(new Query\Terms('compatibility', (array)$compat));
132
            }
133
134
            if ($tags) {
135
                $bool->addMust(new Query\Terms('tag', (array)$tags));
136
            }
137
138
            $list = new ResultList($this->elastica->getIndex(), $query);
139
140
            if ($sort) {
141
                $ids = $list->column('ID');
142
143
                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...
144
                    $list = Addon::get()->byIDs($ids);
145
                } else {
146
                    $list = new ArrayList();
147
                }
148
            } else {
149
                $list = $list->toArrayList();
150
            }
151
        } else {
152
            if (!$sort) {
153
                $sort = 'relative';
154
            }
155
        }
156
157
        switch ($sort) {
158
            case 'name':
159
                $list = $list->sort('Name');
160
                break;
161
            case 'newest':
162
                $list = $list->sort('Released', 'DESC');
163
                break;
164
            case 'downloads':
165
                $list = $list->sort('Downloads', 'DESC');
166
                break;
167
            case 'relative':
168
                if (!$list instanceof ArrayList) {
169
                    /** @var ArrayList|Addon[] $unsorted */
170
                    $unsorted = ArrayList::create($list->toArray());
171
                } else {
172
                    $unsorted = $list;
173
                }
174
                foreach ($unsorted as $item) {
175
                    $item->Score = $item->relativePopularityFormatted() . ' per day';
176
                }
177
                $list = $unsorted->sort('relativePopularity DESC');
178
                break;
179
            default:
180
                $list = $list->sort('Downloads', 'DESC');
181
        }
182
183
        $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...
184
        $list->setPageLength(16);
185
186
        return $list;
187
    }
188
189
    public function AddonsSearchForm()
190
    {
191
        $form = new Form(
192
            $this,
193
            'AddonsSearchForm',
194
            new FieldList(array(
195
                TextField::create('search', 'Search for')
196
                    ->setValue($this->request->getVar('search'))
197
                    ->addExtraClass('input-block-level'),
198
                DropdownField::create('sort', 'Sort by')
199
                    ->setSource(array(
200
                        'name'      => 'Name',
201
                        'downloads' => 'Most downloaded',
202
                        'relative'  => 'Average downloads per day',
203
                        'newest'    => 'Newest'
204
                    ))
205
                    ->setEmptyString('Best match')
206
                    ->setValue($this->request->getVar('sort'))
207
                    ->addExtraClass('input-block-level'),
208
                DropdownField::create('type', 'Add-on type')
209
                    ->setSource(array(
210
                        'module' => 'Modules',
211
                        'theme'  => 'Themes'
212
                    ))
213
                    ->setEmptyString('Modules and themes')
214
                    ->setValue($this->request->getVar('type'))
215
                    ->addExtraClass('input-block-level'),
216
                CheckboxSetField::create('compatibility', 'Compatible SilverStripe versions')
217
                    ->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...
218
                    ->setValue($this->request->getVar('compatibility'))
219
                    ->setTemplate('AddonsSearchCheckboxSetField')
220
            )),
221
            new FieldList()
222
        );
223
224
        return $form
225
            ->setFormMethod('GET')
226
            ->setFormAction($this->Link());
227
    }
228
229
    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...
230
    {
231
        $addons = Addon::get()
232
            ->sort('Released', 'DESC')
233
            ->limit($limit);
234
235
        $rss = new RSSFeed(
236
            $addons,
237
            $this->Link(),
238
            "Newest addons on addons.silverstripe.org",
239
            null,
240
            'RSSTitle'
241
        );
242
243
        return $rss->outputToBrowser();
244
    }
245
246
}
247