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

ScriptSettingsWindowFactory::callbackApply()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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