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

ScriptSettingsWindowFactory::fetchScriptSettings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
ccs 0
cts 10
cp 0
rs 9.4285
cc 2
eloc 9
nc 2
nop 0
crap 6
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 FML\Controls\Frame;
15
use FML\Script\Features\ScriptFeature;
16
use FML\Script\Script;
17
use FML\Script\ScriptLabel;
18
use Maniaplanet\DedicatedServer\Connection;
19
20
21
/**
22
 * Class Script settings factory
23
 *
24
 * @package eXpansion\Bundle\Menu\Plugins\Gui;
25
 * @author reaby
26
 */
27
class ScriptSettingsWindowFactory extends GridWindowFactory
28
{
29
    /** @var Console */
30
    protected $console;
31
32
    /** @var Connection */
33
    protected $connection;
34
35
    /** @var DataCollectionFactory */
36
    protected $dataCollectionFactory;
37
38
    /** @var GridBuilderFactory */
39
    protected $gridBuilderFactory;
40
41
    /** @var  AdminGroups */
42
    protected $adminGroupsHelper;
43
44
    /**
45
     * ScriptSettingsWindowFactory constructor.
46
     *
47
     * @param                       $name
48
     * @param                       $sizeX
49
     * @param                       $sizeY
50
     * @param null $posX
51
     * @param null $posY
52
     * @param WindowFactoryContext $context
53
     * @param GridBuilderFactory $gridBuilderFactory
54
     * @param DataCollectionFactory $dataCollectionFactory
55
     * @param AdminGroups $adminGroupsHelper
56
     * @param Connection $connection
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
        Connection $connection,
69
        Console $console
70
    ) {
71
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
72
        $this->adminGroupsHelper = $adminGroupsHelper;
73
        $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...
74
        $this->gridBuilderFactory = $gridBuilderFactory;
75
        $this->dataCollectionFactory = $dataCollectionFactory;
76
        $this->connection = $connection;
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->fetchScriptSettings();
87
88
        $collection = $this->dataCollectionFactory->create($this->getData());
89
        $collection->setPageSize(20);
90
91
        $gridBuilder = $this->gridBuilderFactory->create();
92
        $gridBuilder->setManialink($manialink)
93
            ->setDataCollection($collection)
94
            ->setManialinkFactory($this)
95
            ->addTextColumn(
96
                'name',
97
                'expansion_admin.gui.window.scriptsettings.column.name',
98
                5,
99
                true,
100
                false
101
            )->addInputColumn(
102
                'value',
103
                'expansion_admin.gui.window.scriptsettings.column.value',
104
                3);
105
106
        $manialink->setData('grid', $gridBuilder);
107
        $frame = $manialink->getContentFrame();
108
        $this->setGridSize($frame->getWidth(), $frame->getHeight() - 10);
109
110
111
        $apply = $this->uiFactory->createButton("Apply");
112
        $apply->setPosition(($frame->getWidth() - $apply->getWidth()), -($frame->getHeight() - $apply->getHeight()));
113
114
        $apply->setAction($this->actionFactory->createManialinkAction(
115
            $manialink, [$this, "callbackApply"], [
116
            "grid" => $manialink->getData('grid'),
117
            "manialink" => $manialink,
118
        ]));
119
120
121
        $manialink->addChild($apply);
122
123
124
125
    }
126
127
    /** Callback for apply button
128
     *
129
     * @param $login
130
     * @param $entries
131
     * @param $args
132
     */
133
    public function callbackApply($login, $entries, $args)
134
    {
135
        /** @var GridBuilder $grid */
136
        $grid = $args['grid'];
137
138
        $grid->updateDataCollection($entries); // update datacollection
139
140
        // build settings array from datacollection
141
        $settings = [];
142
        foreach ($grid->getDataCollection()->getAll() as $key => $value) {
143
            $settings[$value['name']] = $value['value'];
144
        }
145
146
        try {
147
            $this->connection->setModeScriptSettings($settings);
148
            $this->closeManialink($login, $entries, $args);
149
150
        } catch (\Exception $ex) {
151
            $this->connection->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
        $this->genericData = [];
162
163
        $scriptSettings = $this->connection->getModeScriptSettings();
164
165
        /**
166
         * @var string $i
167
         */
168
        $i = 1;
169
        foreach ($scriptSettings as $name => $value) {
170
            $this->genericData[] = [
171
                'index' => $i++,
172
                'name' => $name,
173
                'value' => $value,
174
            ];
175
        }
176
    }
177
}
178