Completed
Pull Request — master (#304)
by
unknown
04:02
created

TotoWindowFactory::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 28
nc 1
nop 16

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
4
namespace eXpansion\Bundle\Acme\Plugins\Gui;
5
6
7
use eXpansion\Bundle\LocalRecords\Model\RecordQueryBuilder;
8
use eXpansion\Bundle\Maps\Model\MxmapQuery;
9
use eXpansion\Framework\AdminGroups\Helpers\AdminGroups;
10
use eXpansion\Framework\Core\Helpers\Countries;
11
use eXpansion\Framework\Core\Helpers\Time;
12
use eXpansion\Framework\Core\Helpers\TMString;
13
use eXpansion\Framework\Core\Model\Gui\Grid\DataCollectionFactory;
14
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilderFactory;
15
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
16
use eXpansion\Framework\Core\Model\Gui\Window;
17
use eXpansion\Framework\Core\Model\Gui\WindowFactoryContext;
18
use eXpansion\Framework\Core\Plugins\Gui\GridWindowFactory;
19
use eXpansion\Framework\Core\Services\DedicatedConnection\Factory;
20
use eXpansion\Framework\Core\Storage\MapStorage;
21
use eXpansion\Framework\GameManiaplanet\DataProviders\ChatCommandDataProvider;
22
use eXpansion\Framework\Gui\Builders\uiBuilder;
23
use eXpansion\Framework\Gui\Components\Button;
24
use FML\Controls\Frame;
25
26
class TotoWindowFactory extends GridWindowFactory
27
{
28
    /**
29
     * @var MapStorage
30
     */
31
    private $mapStorage;
32
    /**
33
     * @var ChatCommandDataProvider
34
     */
35
    private $chatCommandDataProvider;
36
    /**
37
     * @var Factory
38
     */
39
    private $factory;
40
    /**
41
     * @var AdminGroups
42
     */
43
    private $adminGroups;
44
45
    /** @var Countries */
46
    protected $countries;
47
48
    /** @var Time */
49
    protected $time;
50
    /**
51
     * @var RecordQueryBuilder
52
     */
53
    private $queryBuilder;
54
    /**
55
     * @var MxmapQuery
56
     */
57
    private $mxmapQuery;
58
59
    /**
60
     * PlayersWindow constructor.
61
     * @param                         $name
62
     * @param                         $sizeX
63
     * @param                         $sizeY
64
     * @param null                    $posX
65
     * @param null                    $posY
66
     * @param WindowFactoryContext    $context
67
     * @param MapStorage              $mapStorage
68
     * @param DataCollectionFactory   $dataCollectionFactory
69
     * @param GridBuilderFactory      $gridBuilderFactory
70
     * @param ChatCommandDataProvider $chatCommandDataProvider
71
     * @param Factory                 $factory
72
     * @param AdminGroups             $adminGroups
73
     * @param Countries               $countries
74
     * @param Time                    $time
75
     * @param RecordQueryBuilder      $queryBuilder
76
     * @param MxmapQuery              $mxmapQuery
77
     */
78
    public function __construct(
79
        $name,
80
        $sizeX,
81
        $sizeY,
82
        $posX = null,
83
        $posY = null,
84
        WindowFactoryContext $context,
85
        MapStorage $mapStorage,
86
        DataCollectionFactory $dataCollectionFactory,
87
        GridBuilderFactory $gridBuilderFactory,
88
        ChatCommandDataProvider $chatCommandDataProvider,
89
        Factory $factory,
90
        AdminGroups $adminGroups,
91
        Countries $countries,
92
        Time $time,
93
        RecordQueryBuilder $queryBuilder,
94
        MxmapQuery $mxmapQuery
95
    ) {
96
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
97
98
        $this->mapStorage = $mapStorage;
99
        $this->dataCollectionFactory = $dataCollectionFactory;
100
        $this->gridBuilderFactory = $gridBuilderFactory;
101
        $this->chatCommandDataProvider = $chatCommandDataProvider;
102
        $this->factory = $factory;
103
        $this->adminGroups = $adminGroups;
104
        $this->countries = $countries;
105
106
        $this->time = $time;
107
        $this->queryBuilder = $queryBuilder;
108
        $this->mxmapQuery = $mxmapQuery;
109
    }
110
111
    protected function createContent(
112
        ManialinkInterface $manialink
113
    ) {
114
        parent::createContent($manialink);
115
116
        $manialink->setData('playerActions', []);
117
        $manialink->setData('mapUid', $this->mapStorage->getCurrentMap()->uId);
118
119
        $frame = Frame::create();;
120
        $frame->setPosition(75, 0);
121
        $manialink->setData("playerFrame", $frame);
122
        $manialink->addChild($frame);
123
124
    }
125
126
    /**
127
     * @param ManialinkInterface $manialink
128
     */
129
    protected function createGrid(
130
        ManialinkInterface $manialink
131
    ) {
132
        $this->updateData($manialink);
133
134
        $selectButton = $this->uiFactory->createButton('expansion_players.gui.players.window.column.select',
135
            Button::TYPE_DEFAULT)->setSize(10, 5)->setTranslate(true);
136
137
        $gridBuilder = $this->gridBuilderFactory->create();
138
        $gridBuilder->setManialink($manialink)
139
            ->setDataCollection($manialink->getData('dataCollection'))
140
            ->setManialinkFactory($this)
141
            ->addTextColumn(
142
                'length',
143
                'Length',
144
                1,
145
                true,
146
                false
147
148
            )
149
            ->addTextColumn(
150
                'name',
151
                'Name',
152
                3,
153
                true,
154
                false
155
            )
156
            ->addActionColumn('uid', "Select",
157
                1, [$this, "callbackSetMap"], $selectButton);
158
159
160
        $manialink->setData('grid', $gridBuilder);
161
162
        $frame = $manialink->getContentFrame();
163
        $this->setGridSize(73, $frame->getHeight());
164
        $this->setGridPosition(0, 0);
165
166
    }
167
168
    protected function updateContent(
169
        ManialinkInterface $manialink
170
    ) {
171
        parent::updateContent($manialink);
172
173
174
        $recipient = $manialink->getUserGroup()->getLogins()[0];
0 ignored issues
show
Unused Code introduced by
$recipient 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...
175
        $map = $this->mapStorage->getMap($manialink->getData('mapUid'));
176
177
178
        $recs = $this->queryBuilder->getMapRecords($map->uId, 1, "asc", 10);
179
        $inc = <<<EOL
180
           <uiLabel size="40 6" textSize="3"></uiLabel>
181
           <uiLabel width="40" textSize="2">Local Records</uiLabel>
182
EOL;
183
184
        $rank = 1;
185
        foreach ($recs as $rec) {
186
            $inc .= "<uiLayoutLine margin='1'>";
187
            $inc .= '<uiLabel width="3">'.$rank.'.</uiLabel>';
188
            $inc .= '<uiLabel width="10">'.$this->time->timeToText($rec->getScore(), true).'</uiLabel>';
189
            $inc .= '<uiLabel width="30">'.$rec->getPlayer()->getNickname().'</uiLabel>';
190
            $inc .= "</uiLayoutLine>";
191
            $rank++;
192
        }
193
194
        /** @var Frame $frame */
195
        $frame = $manialink->getData('playerFrame');
196
        $frame->removeAllChildren();
197
198
        $builder = new uiBuilder($this->uiFactory, $this->actionFactory, $manialink, $this);
199
200
        $contents = $builder->build(/** @lang text */
201
            <<<EOL
202
        <window>
203
            <uiLayoutLine margin="3">
204
                <uiLayoutRow margin="1">
205
                    <uiLabel pos="45 0" align="center top" textSize="4" size="90 4">{$map->name}</uiLabel>
206
                    <uiLabel pos="45 0" align="center top" textSize="2" size="90 3">{$map->author}</uiLabel>
207
                    <quad size="90 0.2" backgroundColor="fff"/>
208
                    <uiLabel>Environment: $map->environnement</uiLabel>
209
                    <uiLabel>Mood: $map->mood</uiLabel>
210
                </uiLayoutRow>
211
                <uiLayoutRow margin="1">
212
                   $inc          
213
                </uiLayoutRow>     
214
             </uiLayoutLine>
215
        </window>
216
EOL
217
        );
218
219
        $frame->addChild($contents);
220
221
        $builder = new uiBuilder($this->uiFactory, $this->actionFactory, $manialink, $this);
222
        $xm = /** @lang text */
223
            <<<EOL
224
        <frame pos="-10 -80">
225
            <uiLayoutLine margin="2">
226
                <uiButton backgroundColor="090">Jukebox</uiButton>
227
                <uiConfirmButton backgroundColor="900">Delete</uiConfirmButton>
228
                <uiButton>Action</uiButton>
229
            </uiLayoutLine>
230
        </frame>
231
EOL;
232
233
        $actions = $builder->build($xm);
234
        $frame->addChild($actions);
235
236
    }
237
238
239
    /**
240
     * @param ManialinkInterface|Window $manialink
241
     * @param string                    $login
242
     * @param array                     $entries
243
     * @param array                     $args
244
     */
245
    public function callbackSetMap(
246
        $manialink,
247
        $login,
248
        $entries,
249
        $args
250
    ) {
251
        $manialink->setData("mapUid", $args['uid']);
252
        $this->update($manialink->getUserGroup());
253
    }
254
255
256
    public function callbackChatCommand(
257
        $manialink,
258
        $login,
259
        $entries,
260
        $args
261
    ) {
262
        $this->callChatCommand($login, $args['action']);
263
    }
264
265
266
    public function updateData(
267
        $manialink
268
    ) {
269
        $maps = $this->mapStorage->getMaps();
270
        $data = [];
271 View Code Duplication
        foreach ($maps as $map) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
272
            $data[] = [
273
                "uid" => $map->uId,
274
                "author" => $map->author,
275
                "name" => TMString::trimLinks($map->name),
276
                "length" => $this->time->timeToText($map->goldTime, false),
277
            ];
278
        }
279
        $this->setData($manialink, $data);
280
    }
281
282
283
    /**
284
     * @param $login
285
     * @param $command
286
     */
287
    public function callChatCommand(
288
        $login,
289
        $command
290
    ) {
291
        $this->chatCommandDataProvider->onPlayerChat($login, $login, $command, true);
292
    }
293
294
}