Completed
Pull Request — master (#104)
by
unknown
04:57 queued 02:15
created

ManiaExchangeWindowFactory::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 37
rs 8.8571
c 1
b 0
f 1
ccs 0
cts 34
cp 0
cc 1
eloc 32
nc 1
nop 18
crap 2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace eXpansion\Bundle\Maps\Plugins\Gui;
4
5
use eXpansion\Bundle\Maps\Plugins\Jukebox;
6
use eXpansion\Bundle\Maps\Plugins\ManiaExchange;
7
use eXpansion\Bundle\Maps\Structure\MxInfo;
8
use eXpansion\Framework\Core\Helpers\Http;
9
use eXpansion\Framework\Core\Helpers\Structures\HttpResult;
10
use eXpansion\Framework\Core\Helpers\Time;
11
use eXpansion\Framework\Core\Helpers\TMString;
12
use eXpansion\Framework\Core\Model\Gui\Grid\DataCollectionFactory;
13
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilderFactory;
14
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
15
use eXpansion\Framework\Core\Model\Gui\WindowFactoryContext;
16
use eXpansion\Framework\Core\Plugins\Gui\GridWindowFactory;
17
use eXpansion\Framework\Gui\Components\uiButton;
18
use eXpansion\Framework\Gui\Components\uiDropdown;
19
use eXpansion\Framework\Gui\Components\uiLabel;
20
21
class ManiaExchangeWindowFactory extends GridWindowFactory
22
{
23
    /** @var  uiDropdown */
24
    public $lengthBox;
25
    /** @var  uiDropdown */
26
    public $stylebox;
27
    /** @var  uiDropdown */
28
    public $sitebox;
29
    /** @var  uiDropdown */
30
    public $difficultiesBox;
31
    /** @var  uiDropdown */
32
    public $tpackBox;
33
34
    public $tpack = [
35
        "All" => "",
36
        "Stadium" => "Stadium,TMStadium",
37
        "Canyon" => "Canyon,TmCanyon",
38
        "Valley" => "Valley,TmValley",
39
        "Lagoon" => "Lagoon,TmLagoon",
40
    ];
41
42
    /** @var  uiDropdown */
43
    private $orderbox;
44
    /** @var  uiDropdown */
45
    private $opbox;
46
    /** @var  uiDropdown */
47
    private $modebox;
48
49
    /** @var  array */
50
    private $tracksearch;
51
52
    /** @var GridBuilderFactory */
53
    protected $gridBuilderFactory;
54
55
    /** @var DataCollectionFactory */
56
    protected $dataCollectionFactory;
57
58
    /** @var Time */
59
60
    protected $timeFormatter;
61
    /**
62
     * @var  ManiaExchange $mxPlugin
63
     */
64
    private $mxPlugin;
65
    /**
66
     * @var array
67
     */
68
    private $order;
69
    /**
70
     * @var array
71
     */
72
    private $length;
73
    /**
74
     * @var array
75
     */
76
    private $map_styles_tm;
77
    /**
78
     * @var array
79
     */
80
    private $map_style_sm;
81
    /**
82
     * @var array
83
     */
84
    private $difficulties;
85
    /**
86
     * @var array
87
     */
88
    private $operator;
89
90
    /**
91
     * @var Http
92
     */
93
    private $http;
94
95
    /**
96
     * ManiaExchangeWindowFactory constructor.
97
     * @param $name
98
     * @param $sizeX
99
     * @param $sizeY
100
     * @param null $posX
101
     * @param null $posY
102
     * @param WindowFactoryContext $context
103
     * @param GridBuilderFactory $gridBuilderFactory
104
     * @param DataCollectionFactory $dataCollectionFactory
105
     * @param Time $time
106
     * @param ManiaExchange $mxPlugin
107
     * @param Http $http
108
     * @param $tracksearch
109
     * @param $order
110
     * @param $length
111
     * @param $map_styles_tm
112
     * @param $map_style_sm
113
     * @param $difficulties
114
     * @param $operator
115
     */
116
    public function __construct(
117
        $name,
118
        $sizeX,
119
        $sizeY,
120
        $posX,
121
        $posY,
122
        WindowFactoryContext $context,
123
        GridBuilderFactory $gridBuilderFactory,
124
        DataCollectionFactory $dataCollectionFactory,
125
        Time $time,
126
        ManiaExchange $mxPlugin,
127
        Http $http,
128
        $tracksearch,
129
        $order,
130
        $length,
131
        $map_styles_tm,
132
        $map_style_sm,
133
        $difficulties,
134
        $operator
135
136
    ) {
137
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
138
139
        $this->gridBuilderFactory = $gridBuilderFactory;
140
        $this->dataCollectionFactory = $dataCollectionFactory;
141
        $this->timeFormatter = $time;
142
        $this->mxPlugin = $mxPlugin;
143
        $this->tracksearch = array_flip($tracksearch);
144
145
        $this->order = array_flip($order);
146
        $this->length = array_flip($length);
147
        $this->map_styles_tm = array_flip($map_styles_tm);
148
        $this->map_style_sm = array_flip($map_style_sm);
149
        $this->difficulties = array_flip($difficulties);
150
        $this->operator = array_flip($operator);
151
        $this->http = $http;
152
    }
153
154
    /**
155
     * @param ManialinkInterface $manialink
156
     * @return mixed
157
     */
158
    protected function createGrid(ManialinkInterface $manialink)
159
    {
160
        $collection = $this->dataCollectionFactory->create($this->getData());
161
        $collection->setPageSize(20);
162
163
        $x = 0;
164
165
        $tooltip = $this->uiFactory->createTooltip();
166
        $manialink->addChild($tooltip);
167
168
        $this->modebox = $this->uiFactory->createDropdown("mode", $this->tracksearch, 0);
169
        $this->modebox->setPosition($x, -6, 2);
170
        $manialink->addChild($this->modebox);
171
172
        $label = $this->uiFactory->createLabel("Sort by", uiLabel::TYPE_HEADER);
173
        $label->setPosition($x, 0);
174
        $manialink->addChild($label);
175
176
        $x += 32;
177
        $this->orderbox = $this->uiFactory->createDropdown("order", $this->order, 0);
178
        $this->orderbox->setPosition($x, -6, 2);
179
        $manialink->addChild($this->orderbox);
180
181
        $label = $this->uiFactory->createLabel("Order", uiLabel::TYPE_HEADER);
182
        $label->setPosition($x, 0);
183
        $manialink->addChild($label);
184
185
        $x += 32;
186
        $this->opbox = $this->uiFactory->createDropdown("operator", $this->operator, 0);
187
        $this->opbox->setPosition($x, -6, 2);
188
        $manialink->addChild($this->opbox);
189
190
        $label = $this->uiFactory->createLabel("Operator", uiLabel::TYPE_HEADER);
191
        $label->setPosition($x, 0);
192
        $manialink->addChild($label);
193
194
        $x += 32;
195
        $this->lengthBox = $this->uiFactory->createDropdown("length", $this->length, 0);
196
        $this->lengthBox->setPosition($x, -6, 2);
197
        $manialink->addChild($this->lengthBox);
198
199
        $label = $this->uiFactory->createLabel("Length", uiLabel::TYPE_HEADER);
200
        $label->setPosition($x, 0);
201
        $manialink->addChild($label);
202
203
        $x += 32;
204
        $this->stylebox = $this->uiFactory->createDropdown("style", $this->map_styles_tm, 0);
205
        $this->stylebox->setPosition($x, -6, 2);
206
        $manialink->addChild($this->stylebox);
207
208
        $label = $this->uiFactory->createLabel("Style", uiLabel::TYPE_HEADER);
209
        $label->setPosition($x, 0);
210
        $manialink->addChild($label);
211
212
        $x += 32;
213
        $this->difficultiesBox = $this->uiFactory->createDropdown("difficulties", $this->difficulties, 0);
214
        $this->difficultiesBox->setPosition($x, -6, 2);
215
        $manialink->addChild($this->difficultiesBox);
216
217
        $label = $this->uiFactory->createLabel("Difficulty", uiLabel::TYPE_HEADER);
218
        $label->setPosition($x, 0);
219
        $manialink->addChild($label);
220
221
        /// second line
222
223
        $this->sitebox = $this->uiFactory->createDropdown("site", ["Trackmania" => "tm", "Storm" => "sm"], 0);
224
        $this->sitebox->setPosition(0, -14, 2);
225
        $manialink->addChild($this->sitebox);
226
227
        $this->tpackBox = $this->uiFactory->createDropdown("tpack", $this->tpack, 0);
228
        $this->tpackBox->setPosition(32, -14, 2);
229
        $manialink->addChild($this->tpackBox);
230
231
        $mapname = $this->uiFactory->createInput("map");
232
        $mapname->setHeight(6);
233
        $author = $this->uiFactory->createInput("author");
234
        $author->setHeight(6);
235
236
        $search = $this->uiFactory->createButton('🔍 Search', uiButton::TYPE_DECORATED);
237
        $search->setAction($this->actionFactory->createManialinkAction($manialink, [$this, 'callbackSearch'], null));
0 ignored issues
show
Documentation introduced by
null is of type null, 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...
238
        $search->setHeight(6);
239
240
        $line = $this->uiFactory->createLayoutLine(64, -14, [$mapname, $author, $search], 2);
241
        $manialink->addChild($line);
242
243
        $addButton = $this->uiFactory->createButton('Install', uiButton::TYPE_DEFAULT);
244
        $addButton->setSize(20, 5);
245
246
        $gridBuilder = $this->gridBuilderFactory->create();
247
        $gridBuilder->setManialink($manialink)
248
            ->setDataCollection($collection)
249
            ->setManialinkFactory($this)
250
            ->addTextColumn(
251
                'index',
252
                'expansion_mx.gui.mxsearch.column.index',
253
                1,
254
                true,
255
                false
256
            )->addTextColumn(
257
                'name',
258
                'expansion_mx.gui.mxsearch.column.name',
259
                5,
260
                true,
261
                false
262
            )->addTextColumn(
263
                'author',
264
                'expansion_mx.gui.mxsearch.column.author',
265
                3,
266
                false
267
            )->addTextColumn(
268
                'envir',
269
                'expansion_mx.gui.mxsearch.column.envir',
270
                2,
271
                true,
272
                false
273
            )->addTextColumn(
274
                'awards',
275
                'expansion_mx.gui.mxsearch.column.awards',
276
                1,
277
                true,
278
                false
279
            )->addTextColumn(
280
                'length',
281
                'expansion_mx.gui.mxsearch.column.length',
282
                2,
283
                true,
284
                false
285
            )->addTextColumn(
286
                'style',
287
                'expansion_mx.gui.mxsearch.column.style',
288
                2,
289
                true,
290
                false
291
            )
292
            ->addActionColumn('add', 'expansion_mx.gui.mxsearch.column.add', 2, array($this, 'callbackAdd'),
293
                $addButton);
294
        $this->setGridPosition(0, -24);
295
296
        $content = $manialink->getContentFrame();
297
        $this->setGridSize($content->getWidth(), $content->getHeight() - 24);
298
        $manialink->setData('grid', $gridBuilder);
299
300
    }
301
302
303
    public function findIndex($arr, $search)
304
    {
305
        $x = 0;
306
        foreach ($arr as $idx => $value) {
307
            if ($value == $search) {
308
                return $x;
309
            }
310
            $x++;
311
        }
312
313
        return -1;
314
    }
315
316
    public function callbackAdd($login, $params, $args)
317
    {
318
        $this->mxPlugin->addMap($login, $args['mxid'], $params['site']);
319
    }
320
321
    public function callbackSearch($login, $params, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $args 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...
322
    {
323
        $params = (object)$params;
324
325
        $this->modebox->setSelectedIndex($this->findIndex($this->tracksearch, $params->mode));
326
        $this->orderbox->setSelectedIndex($this->findIndex($this->order, $params->order));
327
        $this->opbox->setSelectedIndex($this->findIndex($this->operator, $params->operator));
328
        $this->lengthBox->setSelectedIndex($this->findIndex($this->length, $params->length));
329
        $this->stylebox->setSelectedIndex($this->findIndex($this->map_styles_tm, $params->style));
330
        $this->difficultiesBox->setSelectedIndex($this->findIndex($this->difficulties, $params->difficulties));
331
        $this->tpackBox->setSelectedIndex($this->findIndex($this->tpack, $params->tpack));
332
333
        $options = "";
334
335
        if ($params->tpack) {
336
            $options .= "&tpack=".$params->tpack;
337
        }
338
        if ($params->operator != -1) {
339
            $options .= "&lengthop=".$params->operator;
340
        }
341
342
        $args = "&mode=".$params->mode."&trackname=".urlencode($params->map)."&anyauthor=".urlencode($params->author).
343
            "&style=".$params->style."&priord=".$params->order."&length=".$params->length.
344
            "&limit=100&gv=1".$options;
345
346
        $query = 'https://'.$params->site.'.mania-exchange.com/tracksearch2/search?api=on'.$args;
347
        $this->http->get($query, [$this, 'setMaps'], ['login' => $login]);
348
349
    }
350
351
352
    public function setMaps(HttpResult $result)
353
    {
354
355
356
        if ($result->hasError()) {
357
            echo $result->getError();
358
359
            return;
360
        }
361
362
        $json = json_decode($result->getResponse(), true);
363
        $data = [];
364
        foreach ($json['results'] as $idx => $mxInfo) {
365
            $map = new MxInfo($mxInfo);
366
            $data[] = [
367
                "index" => $idx + 1,
368
                "name" => TMString::trimControls($map->GbxMapName),
369
                "author" => $map->Username,
370
                "envir" => $map->EnvironmentName,
371
                "awards" => $map->AwardCount ? '$ff0🏆 $fff'.$map->AwardCount : "",
372
                "length" => $map->LengthName,
373
                "style" => $map->StyleName,
374
                "mxid" => $map->TrackID,
375
            ];
376
        }
377
378
        $this->setData($data);
379
380
        $group = $this->groupFactory->createForPlayer($result->getAdditionalData()['login']);
381
        $this->update($group);
382
    }
383
}
384