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

ScriptSettingsWindowFactory::callbackApply()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
ccs 0
cts 11
cp 0
rs 9.4285
cc 3
eloc 11
nc 4
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"], ["grid" => $manialink->getData('grid')]));
111
112
        $manialink->addChild($apply);
113
114
    }
115
116
    /** Callback for apply button
117
     *
118
     * @param $login
119
     * @param $entries
120
     * @param $args
121
     */
122
    public function callbackApply($login, $entries, $args)
123
    {
124
        /** @var GridBuilder $grid */
125
        $grid = $args['grid'];
126
127
        $grid->updateDataCollection($entries); // update datacollection
128
129
        // build settings array from datacollection
130
        $settings = [];
131
        foreach ($grid->getDataCollection()->getAll() as $key => $value) {
132
            $settings[$value['name']] = $value['value'];
133
        }
134
135
        try {
136
            $this->connection->setModeScriptSettings($settings);
137
        } catch (\Exception $ex) {
138
            $this->connection->chatSendServerMessage("error: ".$ex->getMessage());
139
            $this->console->writeln('$f00Error: $fff'.$ex->getMessage());
140
        }
141
    }
142
143
    /**
144
     * helper function to fetch script settings
145
     */
146
    public function fetchScriptSettings()
147
    {
148
        $this->genericData = [];
149
150
        $scriptSettings = $this->connection->getModeScriptSettings();
151
152
        /**
153
         * @var string $i
154
         */
155
        $i = 1;
156
        foreach ($scriptSettings as $name => $value) {
157
            $this->genericData[] = [
158
                'index' => $i++,
159
                'name' => $name,
160
                'value' => $value,
161
            ];
162
        }
163
    }
164
}
165