Completed
Pull Request — master (#294)
by De Cramer
03:48
created

ConfigWindowFactory::createContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace eXpansion\Framework\Config\Ui\Window;
4
5
use eXpansion\Framework\Config\Exception\InvalidConfigException;
6
use eXpansion\Framework\Config\Model\ConfigInterface;
7
use eXpansion\Framework\Config\Services\ConfigManagerInterface;
8
use eXpansion\Framework\Config\Services\ConfigUiManager;
9
use eXpansion\Framework\Core\Exceptions\PlayerException;
10
use eXpansion\Framework\Core\Helpers\ChatNotification;
11
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
12
use eXpansion\Framework\Core\Model\Gui\WindowFactoryContext;
13
use eXpansion\Framework\Core\Plugins\Gui\WindowFactory;
14
use eXpansion\Framework\Gui\Components\Button;
15
use eXpansion\Framework\Gui\Components\Label;
16
use eXpansion\Framework\Gui\Components\Tooltip;
17
use FML\Controls\Control;
18
use FML\Controls\Frame;
19
use FML\Controls\Quad;
20
21
/**
22
 * Class ConfigWindowFactory
23
 *
24
 * @author    de Cramer Oliver<[email protected]>
25
 * @copyright 2018 eXpansion
26
 * @package eXpansion\Framework\Config\Ui\Window
27
 */
28
class ConfigWindowFactory extends WindowFactory
29
{
30
    /** @var string */
31
    protected $currentPath;
32
33
    /** @var ConfigManagerInterface */
34
    protected $configManager;
35
36
    /** @var ConfigUiManager */
37
    protected $configUiManager;
38
39
    /** @var ChatNotification */
40
    protected $chatNotification;
41
42
    /**
43
     * ConfigWindowFactory constructor.
44
     *
45
     * @param                        $name
46
     * @param                        $sizeX
47
     * @param                        $sizeY
48
     * @param null                   $posX
49
     * @param null                   $posY
50
     * @param WindowFactoryContext   $context
51
     * @param ConfigManagerInterface $configManager
52
     * @param ConfigUiManager        $configUiManager
53
     * @param ChatNotification       $chatNotification
54
     */
55
    public function __construct(
56
        $name,
57
        $sizeX,
58
        $sizeY,
59
        $posX = null,
60
        $posY = null,
61
        WindowFactoryContext $context,
62
        ConfigManagerInterface $configManager,
63
        ConfigUiManager $configUiManager,
64
        ChatNotification $chatNotification
65
    ) {
66
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
67
68
        $this->configManager = $configManager;
69
        $this->configUiManager = $configUiManager;
70
        $this->chatNotification = $chatNotification;
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76
    protected function createContent(ManialinkInterface $manialink)
77
    {
78
        parent::createContent($manialink);
79
        $manialink->setData('current_path', $this->currentPath);
80
    }
81
82
83
    /**
84
     * @inheritdoc
85
     */
86
    protected function updateContent(ManialinkInterface $manialink)
87
    {
88
        parent::updateContent($manialink);
89
        $this->currentPath = $manialink->getData('current_path');
90
91
        /** @var Frame $contentFrame */
92
        $contentFrame = $manialink->getContentFrame();
93
        $contentFrame->removeAllChildren();
94
95
        $saveButton = $this->uiFactory->createConfirmButton('expansion_config.ui.save', Button::COLOR_SUCCESS);
96
        $saveButton->setAction(
97
            $this->actionFactory->createManialinkAction(
98
                $manialink,
99
                [$this, 'saveCallback'],
100
                ['path' => $this->currentPath]
101
            )
102
        )
103
            ->setPosition(
104
                $this->sizeX - $saveButton->getWidth() - 4,
105
                -$this->sizeY + $saveButton->getHeight() + 4
106
            )
107
            ->setTranslate(true);
108
109
        // see how grid is built to fix this.
110
        $configs = $this->configManager->getConfigDefinitionTree()->get($this->currentPath);
111
112
        $elements = [(new Quad())->setHeight(4)];
113
        foreach ($configs as $config) {
114
            if (!is_object($config) || !($config instanceof ConfigInterface)) {
0 ignored issues
show
Bug introduced by
The class eXpansion\Framework\Config\Model\ConfigInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
115
                throw new PlayerException("{$this->currentPath} is not valid configuration path");
116
            }
117
118
            $elements[] = $this->buildConfig($config, $this->sizeX - 8, $manialink);
119
        }
120
121
        $contentFrame->addChild(
122
            $this->uiFactory->createLayoutScrollable(
123
                $this->uiFactory->createLayoutRow(0, 0, $elements, 2),
124
                $this->sizeX,
125
                $this->sizeY - $saveButton->getHeight() - 4 - 4
126
            )->setAxis(false, true)
127
        );
128
129
        $contentFrame->addChild($saveButton);
130
    }
131
132
    /**
133
     * Build display for config.
134
     *
135
     * @param ConfigInterface    $config
136
     * @param float              $sizeX
137
     * @param ManialinkInterface $manialink
138
     *
139
     * @return \eXpansion\Framework\Gui\Layouts\LayoutRow
140
     */
141
    protected function buildConfig(ConfigInterface $config, $sizeX, ManialinkInterface $manialink)
142
    {
143
        $rowLayout = $this->uiFactory->createLayoutRow(2, 0, [], 1);
144
145
        $rowLayout->addChild(
146
            $name = $this->uiFactory
147
                ->createLabel($config->getName(), Label::TYPE_TITLE)
148
                ->setWidth($sizeX)
149
                ->setHorizontalAlign(Control::LEFT)
150
                ->setTranslate(true)
151
                ->setTextId($config->getName())
152
                ->setText(null));
153
154
        if ($config->getDescription()) {
155
            $rowLayout->addChild(
156
                $this->uiFactory
157
                    ->createLabel("", Label::TYPE_NORMAL)
158
                    ->setWidth($sizeX)
159
                    ->setHorizontalAlign(Control::LEFT)
160
                    ->setTranslate(true)
161
                    ->setTextId($config->getDescription())
162
                    ->setText(null)
163
                    ->setTextPrefix("  ")
164
                    ->setMaxLines(3)
165
                    ->setLineSpacing(1)
166
            );
167
        }
168
169
        $rowLayout->addChild(
170
            $this->configUiManager->getUiHandler($config)->build($config, $sizeX * 0.37, $manialink, $this)
171
        );
172
173
174
        return $rowLayout;
175
    }
176
177
178
    /**
179
     * @param ManialinkInterface $manialink
180
     * @param                    $login
181
     * @param                    $entries
182
     * @param                    $args
183
     */
184
    public function saveCallback(ManialinkInterface $manialink, $login, $entries, $args)
185
    {
186
        $configs = $this->configManager->getConfigDefinitionTree();
187
188
        $error = false;
189
        // First check all.
190
        foreach ($entries as $configPath => $configValue) {
191
            $config = $configs->get($configPath);
192
193
            if ($config instanceof ConfigInterface) {
0 ignored issues
show
Bug introduced by
The class eXpansion\Framework\Config\Model\ConfigInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
194
                try {
195
                    $config->validate($configValue);
196
                } catch (InvalidConfigException $invalidConfigException) {
197
                    $this->chatNotification->sendMessage(
198
                        $invalidConfigException->getTranslatableMessage(),
199
                        $manialink->getUserGroup(),
200
                        $invalidConfigException->getTranslationParameters()
201
                    );
202
203
                    $error = true;
204
                }
205
            }
206
        }
207
208
        if ($error) {
209
            // Don't set values and don't refresh window.
210
            return;
211
        }
212
213
        // Then save all if all is ok.
214
        foreach ($entries as $configPath => $configValue) {
215
            $config = $configs->get($configPath);
216
217
            if ($config instanceof ConfigInterface) {
0 ignored issues
show
Bug introduced by
The class eXpansion\Framework\Config\Model\ConfigInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
218
                $config->setRawValue(
219
                    $this->configUiManager->getUiHandler($config)->getRawValueFromEntry($config, $configValue)
220
                );
221
            }
222
        }
223
224
        $this->setCurrentPath($args['path']);
225
        $this->update($manialink->getUserGroup());
226
227
228
        $this->chatNotification->sendMessage('eXpansion.config.action.saved', $manialink->getUserGroup());
229
    }
230
231
    /**
232
     * Set path to display in.
233
     *
234
     * @param string $currentPath
235
     */
236
    public function setCurrentPath(string $currentPath)
237
    {
238
        $this->currentPath = $currentPath;
239
    }
240
}
241