ScriptSettingsWindowFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 21
loc 21
ccs 0
cts 21
cp 0
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 11
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\Admin\Plugins\Gui;
4
5
use eXpansion\Framework\AdminGroups\Helpers\AdminGroups;
6
use eXpansion\Framework\Core\Model\Gui\Grid\DataCollectionFactory;
7
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilder;
8
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilderFactory;
9
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
10
use eXpansion\Framework\Core\Model\Gui\Window;
11
use eXpansion\Framework\Core\Model\Gui\WindowFactoryContext;
12
use eXpansion\Framework\Core\Plugins\Gui\GridWindowFactory;
13
use eXpansion\Framework\Core\Services\Console;
14
use eXpansion\Framework\Core\Services\DedicatedConnection\Factory;
15
use FML\Controls\Frame;
16
use FML\Script\Script;
17
use Maniaplanet\DedicatedServer\Connection;
18
19
20
/**
21
 * Class Script settings factory
22
 *
23
 * @package eXpansion\Bundle\Menu\Plugins\Gui;
24
 * @author reaby
25
 */
26
class ScriptSettingsWindowFactory extends GridWindowFactory
27
{
28
    /** @var Console */
29
    protected $console;
30
31
    /** @var Factory */
32
    protected $factory;
33
34
    /** @var DataCollectionFactory */
35
    protected $dataCollectionFactory;
36
37
    /** @var GridBuilderFactory */
38
    protected $gridBuilderFactory;
39
40
    /** @var  AdminGroups */
41
    protected $adminGroupsHelper;
42
43
    /**
44
     * ScriptSettingsWindowFactory constructor.
45
     *
46
     * @param $name
47
     * @param $sizeX
48
     * @param $sizeY
49
     * @param $posX
50
     * @param $posY
51
     * @param WindowFactoryContext $context
52
     * @param GridBuilderFactory $gridBuilderFactory
53
     * @param DataCollectionFactory $dataCollectionFactory
54
     * @param AdminGroups $adminGroupsHelper
55
     * @param Factory $factory
56
     * @param Console $console
57
     */
58 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
59
        $name,
60
        $sizeX,
61
        $sizeY,
62
        $posX,
63
        $posY,
64
        WindowFactoryContext $context,
65
        GridBuilderFactory $gridBuilderFactory,
66
        DataCollectionFactory $dataCollectionFactory,
67
        AdminGroups $adminGroupsHelper,
68
        Factory $factory,
69
        Console $console
70
    ) {
71
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
72
        $this->adminGroupsHelper = $adminGroupsHelper;
73
        $this->currentMenuView = Frame::create();
74
        $this->gridBuilderFactory = $gridBuilderFactory;
75
        $this->dataCollectionFactory = $dataCollectionFactory;
76
        $this->factory = $factory;
77
        $this->console = $console;
78
    }
79
80
    /**
81
     * @param ManialinkInterface|Window $manialink
82
     * @return void
83
     */
84
    protected function createGrid(ManialinkInterface $manialink)
85
    {
86
        $this->setdata($manialink, $this->fetchScriptSettings());
87
88
        $gridBuilder = $this->gridBuilderFactory->create();
89
        $gridBuilder->setManialink($manialink)
90
            ->setDataCollection($manialink->getData('dataCollection'))
91
            ->setManialinkFactory($this)
92
            ->addTextColumn(
93
                'name',
94
                'expansion_admin.gui.window.scriptsettings.column.name',
95
                5,
96
                true,
97
                false
98
            )->addInputColumn(
99
                'value',
100
                'expansion_admin.gui.window.scriptsettings.column.value',
101
                3);
102
103
        $manialink->setData('grid', $gridBuilder);
104
        $frame = $manialink->getContentFrame();
105
        $this->setGridSize($frame->getWidth(), $frame->getHeight() - 10);
106
107
108
        $apply = $this->uiFactory->createButton("expansion_admin.gui.window.scriptsettings.button.apply");
109
        $apply->setTranslate(true);
110
        $apply->setPosition(($frame->getWidth() - $apply->getWidth()), -($frame->getHeight() - $apply->getHeight() - 3));
111
112
        $apply->setAction(
113
            $this->actionFactory->createManialinkAction(
114
                $manialink,
115
                [$this, "callbackApply"],
116
                [],
117
                true
118
            )
119
        );
120
121
122
        $manialink->addChild($apply);
123
124
125
    }
126
127
    /** Callback for apply button
128
     *
129
     * @param $login
130
     * @param $entries
131
     * @param $args
132
     */
133
    public function callbackApply(ManialinkInterface $manialink, $login, $entries, $args)
134
    {
135
        /** @var GridBuilder $grid */
136
        $grid = $manialink->getData('grid');
137
        $grid->updateDataCollection($entries); // update datacollection
138
139
        // build settings array from datacollection
140
        $settings = [];
141
        foreach ($grid->getDataCollection()->getAll() as $key => $value) {
142
            $settings[$value['name']] = $value['value'];
143
        }
144
145
        try {
146
            $this->factory->getConnection()->setModeScriptSettings($settings);
147
            $this->closeManialink($manialink);
148
149
        } catch (\Exception $ex) {
150
            // TODO this should use chat notification.
151
            $this->factory->getConnection()->chatSendServerMessage("error: ".$ex->getMessage());
152
            $this->console->writeln('$f00Error: $fff'.$ex->getMessage());
153
        }
154
    }
155
156
    /**
157
     * helper function to fetch script settings
158
     */
159
    public function fetchScriptSettings()
160
    {
161
        $data = [];
162
163
        $scriptSettings = $this->factory->getConnection()->getModeScriptSettings();
164
165
        /**
166
         * @var string $i
167
         */
168
        $i = 1;
169
        foreach ($scriptSettings as $name => $value) {
170
            $data[] = [
171
                'index' => $i++,
172
                'name' => $name,
173
                'value' => $value,
174
            ];
175
        }
176
177
        return $data;
178
    }
179
}
180