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