Completed
Pull Request — master (#124)
by De Cramer
02:35
created

ServerSettingsWindowFactory::createContent()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 147
Code Lines 112

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 147
c 0
b 0
f 0
ccs 0
cts 113
cp 0
rs 8.2857
cc 1
eloc 112
nc 1
nop 1
crap 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace eXpansion\Bundle\Admin\Plugins\Gui;
4
5
use eXpansion\Framework\AdminGroups\Helpers\AdminGroups;
6
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceMpLegacyManialink;
7
use eXpansion\Framework\Core\Model\Gui\Grid\DataCollectionFactory;
8
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilder;
9
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilderFactory;
10
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
11
use eXpansion\Framework\Core\Model\Gui\WindowFactoryContext;
12
use eXpansion\Framework\Core\Model\UserGroups\Group;
13
use eXpansion\Framework\Core\Plugins\Gui\GridWindowFactory;
14
use eXpansion\Framework\Core\Plugins\Gui\WindowFactory;
15
use eXpansion\Framework\Core\Services\Console;
16
use eXpansion\Framework\Gui\Components\uiAnimation;
17
use eXpansion\Framework\Gui\Components\uiButton;
18
use eXpansion\Framework\Gui\Components\uiCheckbox;
19
use eXpansion\Framework\Gui\Components\uiDropdown;
20
use eXpansion\Framework\Gui\Components\uiInput;
21
use eXpansion\Framework\Gui\Components\uiInputMasked;
22
use eXpansion\Framework\Gui\Components\uiLabel;
23
use eXpansion\Framework\Gui\Components\uiTextbox;
24
use FML\Controls\Frame;
25
use FML\Controls\Label;
26
use FML\Controls\Quad;
27
use Maniaplanet\DedicatedServer\Connection;
28
use Maniaplanet\DedicatedServer\Structures\ServerOptions;
29
30
31
/**
32
 * Class Script settings factory
33
 *
34
 * @package eXpansion\Bundle\Menu\Plugins\Gui;
35
 * @author reaby
36
 */
37
class ServerSettingsWindowFactory extends WindowFactory
38
{
39
    /** @var  uiCheckbox */
40
    public $allowMapDownload;
41
    /** @var  uiCheckbox */
42
    public $autoSaveReplays;
43
    /** @var  uiCheckbox */
44
    public $autoSaveValidationReplays;
45
    /** @var  uiCheckbox */
46
    public $disableHorns;
47
    /** @var  uiCheckbox */
48
    public $disableServiceAnnounces;
49
    /** @var  uiCheckbox */
50
    public $nextUseChangingValidationSeed;
51
52
    /** @var  uiInput */
53
    public $nextMaxPlayers;
54
    /** @var  uiInput */
55
    public $nextMaxSpectators;
56
    /** @var  uiCheckbox */
57
    public $keepPlayerSlots;
58
    /** @var  uiInput */
59
    public $ladderServerLimitMax;
60
    /** @var  uiInput */
61
    public $ladderServerLimitMin;
62
    /** @var  uiDropdown */
63
    public $nextLadderMode;
64
    /** @var  uiDropdown */
65
    public $refereeMode;
66
    /** @var  uiCheckbox */
67
    public $isP2PUpload;
68
    /** @var  uiCheckbox */
69
    public $isP2PDownload;
70
71
    /** @var Console */
72
    protected $console;
73
74
    /** @var Connection */
75
    protected $connection;
76
77
    /** @var  AdminGroups */
78
    protected $adminGroupsHelper;
79
80
    /** @var  uiInput */
81
    protected $serverName;
82
83
    /** @var  uiTextbox */
84
    protected $comment;
85
86
    /** @var  uiInputMasked */
87
    protected $password;
88
89
    /** @var  uiInputMasked */
90
    protected $specpassword;
91
92
    /** @var  uiInputMasked */
93
    protected $refpassword;
94
95
96
    public function __construct(
97
        $name,
98
        $sizeX,
99
        $sizeY,
100
        $posX,
101
        $posY,
102
        WindowFactoryContext $context,
103
        AdminGroups $adminGroupsHelper,
104
        Connection $connection,
105
        Console $console
106
    ) {
107
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
108
        $this->adminGroupsHelper = $adminGroupsHelper;
109
        $this->currentMenuView = Frame::create();
0 ignored issues
show
Bug introduced by
The property currentMenuView does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
110
        $this->connection = $connection;
111
        $this->console = $console;
112
    }
113
114
    /**
115
     * @param ManialinkInterface $manialink
116
     */
117
    protected function createContent(ManialinkInterface $manialink)
118
    {
119
120
        $options = $this->connection->getServerOptions();
121
        $manialink->setData("options", $options);
122
123
        $tooltip = $this->uiFactory->createTooltip();
124
125
        $frame = Frame::create();
126
        $firstColumn = $this->uiFactory->createLayoutRow(0, 0, [], 0);
127
        $secondColumn = $this->uiFactory->createLayoutRow(90, 0, [], 0);
128
        $thirdColumn = $this->uiFactory->createLayoutRow(160, 0, [], 0);
0 ignored issues
show
Unused Code introduced by
$thirdColumn 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...
129
130
        $label = $this->uiFactory->createLabel("Name", uiLabel::TYPE_HEADER);
131
        $this->serverName = $this->uiFactory->createInput("name_string", "", 60);
132
        $firstColumn->addChild($this->uiFactory->createLayoutRow(0, 0, [$label, $this->serverName]));
133
134
        $label = $this->uiFactory->createLabel("Comment", uiLabel::TYPE_HEADER);
135
        $this->comment = $this->uiFactory->createTextbox("comment_string", "", 7, 60);
136
        $firstColumn->addChild($this->uiFactory->createLayoutRow(0, 0, [$label, $this->comment]));
137
138
        $label = $this->uiFactory->createLabel("Password for Players", uiLabel::TYPE_HEADER);
139
        $label->setWidth(60);
140
        $this->password = $this->uiFactory->createInputMasked("password_string", "", 60);
141
        $firstColumn->addChild($this->uiFactory->createLayoutRow(0, 0, [$label, $this->password]));
142
143
        $label = $this->uiFactory->createLabel("Password for Spectators", uiLabel::TYPE_HEADER);
144
        $label->setWidth(60);
145
        $this->specpassword = $this->uiFactory->createInputMasked("passwordForSpectator_string", "", 60);
146
        $firstColumn->addChild($this->uiFactory->createLayoutRow(0, 0, [$label, $this->specpassword]));
147
148
        $label = $this->uiFactory->createLabel("Referee Mode", uiLabel::TYPE_HEADER);
149
        $label->setWidth(60);
150
        $this->refereeMode = $this->uiFactory->createDropdown("refereeMode_integer",
151
            ["Validate top 3" => "0", "Validate all" => "1"]);
152
        $this->refereeMode->setWidth(60);
153
        $firstColumn->addChild($this->uiFactory->createLayoutRow(0, 0, [$label, $this->refereeMode]));
154
155
        $label = $this->uiFactory->createLabel("Password for Referee", uiLabel::TYPE_HEADER);
156
        $label->setWidth(60);
157
        $this->refpassword = $this->uiFactory->createInputMasked("refereePassword_string", "", 60);
158
        $firstColumn->addChild($this->uiFactory->createLayoutRow(0, 0, [$label, $this->refpassword]));
159
160
        $frame->addChild($firstColumn);
161
162
// @region second column
163
        $label = $this->uiFactory->createLabel("Ladder Server", uiLabel::TYPE_HEADER);
164
        $label->setWidth(60);
165
        $this->nextLadderMode = $this->uiFactory->createDropdown("nextLadderMode_integer",
166
            ["Disable Ladder" => "0", "Use Ladder" => "1"]);
167
        $this->nextLadderMode->setWidth(60);
168
        $secondColumn->addChild($this->uiFactory->createLayoutRow(0, 0, [$label, $this->nextLadderMode]));
169
170
        $label = $this->uiFactory->createLabel("Ladder Limits ", uiLabel::TYPE_HEADER);
171
        $label->setWidth(60);
172
        $separator = $this->uiFactory->createLabel("   to ", uiLabel::TYPE_HEADER);
173
        $separator->setSize(10, 5);
174
        $this->ladderServerLimitMax = $this->uiFactory->createInput("ladderServerLimitMax_integer", "", 25);
175
        $this->ladderServerLimitMin = $this->uiFactory->createInput("ladderServerLimitMin_integer", "", 25);
176
177
        $line = $this->uiFactory->createLayoutLine(0, 0,
178
            [$this->ladderServerLimitMin, $separator, $this->ladderServerLimitMax]);
179
        $secondColumn->addChild($this->uiFactory->createLayoutRow(0, 0, [$label, $line]));
180
181
182
        $label = $this->uiFactory->createLabel("Server Max Players", uiLabel::TYPE_HEADER);
183
        $label->setWidth(60);
184
        $this->nextMaxPlayers = $this->uiFactory->createInput("nextMaxPlayers_integer", "", 60);
185
        $secondColumn->addChild($this->uiFactory->createLayoutRow(0, 0, [$label, $this->nextMaxPlayers]));
186
187
        $label = $this->uiFactory->createLabel("Server Max Spectators", uiLabel::TYPE_HEADER);
188
        $label->setWidth(60);
189
        $this->nextMaxSpectators = $this->uiFactory->createInput("nextMaxSpectators_integer", "", 60);
190
        $secondColumn->addChild($this->uiFactory->createLayoutRow(0, 0, [$label, $this->nextMaxSpectators]));
191
192
193
        $box = $this->uiFactory->createCheckbox('Autosave Validation Replays',
194
            'autoSaveValidationReplays_boolean');
195
        $box->setWidth(60);
196
        $secondColumn->addChild($box);
197
        $this->autoSaveValidationReplays = $box;
198
199
        $box = $this->uiFactory->createCheckbox('Autosave Replays', 'autoSaveReplays_boolean');
200
        $box->setWidth(60);
201
        $secondColumn->addChild($box);
202
        $this->autoSaveReplays = $box;
203
204
        $box = $this->uiFactory->createCheckbox('Allow Map Download', 'allowMapDownload_boolean');
205
        $box->setWidth(60);
206
        $secondColumn->addChild($box);
207
        $this->allowMapDownload = $box;
208
209
        $box = $this->uiFactory->createCheckbox('Disable Horns', 'disableHorns_boolean');
210
        $box->setWidth(60);
211
        $secondColumn->addChild($box);
212
        $this->disableHorns = $box;
213
214
        $box = $this->uiFactory->createCheckbox('Disable Service Announces',
215
            'disableServiceAnnounces_boolean');
216
        $box->setWidth(60);
217
        $secondColumn->addChild($box);
218
        $this->disableServiceAnnounces = $box;
219
220
        $box = $this->uiFactory->createCheckbox('Keep Player Slots',
221
            'keepPlayerSlots_boolean');
222
        $box->setWidth(60);
223
        $secondColumn->addChild($box);
224
        $this->keepPlayerSlots = $box;
225
226
        $box = $this->uiFactory->createCheckbox('Enable P2P Upload',
227
            'isP2PUpload_boolean');
228
        $box->setWidth(60);
229
        $secondColumn->addChild($box);
230
        $this->isP2PUpload = $box;
231
232
        $box = $this->uiFactory->createCheckbox('Enable P2P Download',
233
            'isP2PDownload_boolean');
234
        $box->setWidth(60);
235
        $secondColumn->addChild($box);
236
        $this->isP2PDownload = $box;
237
238
        $box = $this->uiFactory->createCheckbox('Keep Player Slots',
239
            'keepPlayerSlots_boolean');
240
        $box->setWidth(60);
241
        $secondColumn->addChild($box);
242
        $this->keepPlayerSlots = $box;
243
244
        $box = $this->uiFactory->createCheckbox('Use Changing Validation Seed',
245
            'nextUseChangingValidationSeed_boolean');
246
        $box->setWidth(60);
247
        $secondColumn->addChild($box);
248
        $this->nextUseChangingValidationSeed = $box;
249
// @endregion
250
        $frame->addChild($secondColumn);
251
        $frame->addChild($tooltip);
252
253
        $apply = $this->uiFactory->createButton("Apply", uiButton::TYPE_DECORATED);
254
        $apply->setAction($this->actionFactory->createManialinkAction($manialink, [$this, "callbackApply"],
255
            ["options" => $manialink->getData('options')]));
256
        $content = $manialink->getContentFrame();
257
        $apply->setPosition($content->getWidth() - $apply->getWidth(), -($content->getHeight() - $apply->getHeight()));
258
        $frame->addChild($apply);
259
260
        $manialink->addChild($frame);
261
262
263
    }
264
265
    public function callbackApply($login, $entries, $args)
266
    {
267
        /** @var ServerOptions $options */
268
        $options = $args['options'];
269
270
        foreach ($entries as $key => $value) {
271
            $array = explode("_", $key);
272
            settype($value, $array[1]);
273
            $options->{$array[0]} = $value;
274
        }
275
        try {
276
            $this->connection->setServerOptions($options);
277
278
            $this->connection->chatSendServerMessage("Done.");
279
280
        } catch (\Exception $ex) {
281
            $this->console->write('$f00Error while setting server options: $fff'.$ex->getMessage());
282
        }
283
284
    }
285
286
287
    protected function updateContent(ManialinkInterface $manialink)
288
    {
289
        /** @var ServerOptions $options */
290
        $options = $manialink->getData('options');
291
292
        $this->serverName->setDefault($options->name);
293
        $this->comment->setDefault($options->comment);
294
        $this->password->setDefault($options->password);
295
        $this->specpassword->setDefault($options->passwordForSpectator);
296
        $this->refpassword->setDefault($options->refereePassword);
297
        $this->refereeMode->setSelectedIndex($options->refereeMode);
298
        $this->nextMaxSpectators->setDefault($options->nextMaxSpectators);
299
        $this->nextMaxPlayers->setDefault($options->nextMaxPlayers);
300
        $this->ladderServerLimitMax->setDefault($options->ladderServerLimitMax);
301
        $this->ladderServerLimitMin->setDefault($options->ladderServerLimitMin);
302
        $this->nextLadderMode->setSelectedIndex($options->nextLadderMode);
303
        $this->allowMapDownload->setChecked($options->allowMapDownload);
304
        $this->autoSaveValidationReplays->setChecked($options->autoSaveValidationReplays);
305
        $this->autoSaveReplays->setChecked($options->autoSaveReplays);
306
        $this->disableHorns->setChecked($options->disableHorns);
307
        $this->disableServiceAnnounces->setChecked($options->disableServiceAnnounces);
308
        $this->keepPlayerSlots->setChecked($options->keepPlayerSlots);
309
        $this->isP2PDownload->setChecked($options->isP2PDownload);
310
        $this->isP2PUpload->setChecked($options->isP2PUpload);
311
312
313
    }
314
315
316
}
317