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