Completed
Pull Request — master (#233)
by De Cramer
07:18
created

ConfigWindowFactory::buildConfig()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 3
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\ConfigManager;
8
use eXpansion\Framework\Config\Services\ConfigManagerInterface;
9
use eXpansion\Framework\Config\Services\ConfigUiManager;
10
use eXpansion\Framework\Core\Exceptions\PlayerException;
11
use eXpansion\Framework\Core\Helpers\ChatNotification;
12
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
13
use eXpansion\Framework\Core\Model\Gui\WindowFactoryContext;
14
use eXpansion\Framework\Core\Plugins\Gui\WindowFactory;
15
use eXpansion\Framework\Gui\Components\uiButton;
16
use eXpansion\Framework\Gui\Components\uiTooltip;
17
use FML\Controls\Control;
18
use FML\Controls\Frame;
19
use FML\Controls\Quad;
20
use FML\Controls\Quads\Quad_Icons64x64_1;
21
22
/**
23
 * Class ConfigWindowFactory
24
 *
25
 * @author    de Cramer Oliver<[email protected]>
26
 * @copyright 2018 eXpansion
27
 * @package eXpansion\Framework\Config\Ui\Window
28
 */
29
class ConfigWindowFactory extends WindowFactory
30
{
31
    /** @var string */
32
    protected $currentPath;
33
34
    /** @var ConfigManagerInterface */
35
    protected $configManager;
36
37
    /** @var ConfigUiManager */
38
    protected $configUiManager;
39
40
    /** @var ChatNotification */
41
    protected $chatNotification;
42
43
    /**
44
     * ConfigWindowFactory constructor.
45
     *
46
     * @param                        $name
47
     * @param                        $sizeX
48
     * @param                        $sizeY
49
     * @param null                   $posX
50
     * @param null                   $posY
51
     * @param WindowFactoryContext   $context
52
     * @param ConfigManagerInterface $configManager
53
     * @param ConfigUiManager        $configUiManager
54
     * @param ChatNotification       $chatNotification
55
     */
56
    public function __construct(
57
        $name,
58
        $sizeX,
59
        $sizeY,
60
        $posX = null,
61
        $posY = null,
62
        WindowFactoryContext $context,
63
        ConfigManagerInterface $configManager,
64
        ConfigUiManager $configUiManager,
65
        ChatNotification $chatNotification
66
    ) {
67
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
68
69
        $this->configManager = $configManager;
70
        $this->configUiManager = $configUiManager;
71
        $this->chatNotification = $chatNotification;
72
    }
73
74
75
    /**
76
     * @inheritdoc
77
     */
78
    protected function updateContent(ManialinkInterface $manialink)
79
    {
80
        parent::updateContent($manialink);
81
        /** @var Frame $contentFrame */
82
        $contentFrame = $manialink->getContentFrame();
83
        $contentFrame->removeAllChildren();
84
85
        $tooltip = $this->uiFactory->createTooltip();
86
        $manialink->addChild($tooltip);
87
88
        $saveButton = $this->uiFactory->createConfirmButton('expansion_config.ui.save', uiButton::COLOR_SUCCESS);
89
        $saveButton->setAction(
90
                $this->actionFactory->createManialinkAction(
91
                    $manialink,
92
                    [$this, 'saveCallback'],
93
                    ['path' => $this->currentPath]
94
                )
95
            )
96
            ->setPosition(
97
                $this->sizeX - $saveButton->getWidth() - 4,
98
                -$this->sizeY + $saveButton->getHeight() + 4
99
            )
100
            ->setTranslate(true);
101
102
        // see how grid is built to fix this.
103
        $configs = $this->configManager->getConfigDefinitionTree()->get($this->currentPath);
104
105
        $elements = [(new Quad())->setHeight(4)];
106
        foreach ($configs as $config) {
107
            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...
108
                throw new PlayerException("{$this->currentPath} is not valid configuration path");
109
            }
110
111
            $elements[] = $this->buildConfig($config, $this->sizeX - 8, $tooltip);
112
        }
113
114
        $contentFrame->addChild(
115
            $this->uiFactory->createLayoutScrollable(
116
                $this->uiFactory->createLayoutRow(0, 0, $elements),
117
                $this->sizeX,
118
                $this->sizeY - $saveButton->getHeight() - 4 - 4
119
            )->setAxis(false, true)
120
        );
121
122
        $contentFrame->addChild($saveButton);
123
    }
124
125
    /**
126
     * Build display for config.
127
     *
128
     * @param ConfigInterface $config
129
     * @param                 $sizeX
130
     * @param uiTooltip       $tooltip
131
     *
132
     * @return \eXpansion\Framework\Gui\Layouts\layoutLine
133
     */
134
    protected function buildConfig(ConfigInterface $config, $sizeX, uiTooltip $tooltip)
135
    {
136
        $descriptionButton = new Quad_Icons64x64_1();
137
        $descriptionButton->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_TrackInfo)
138
            ->setSize(4, 4);
139
        // Temporary to get en, tooltip don't support translations.
140
        $tooltip->addTooltip($descriptionButton, $this->chatNotification->getMessage($config->getDescription()));
141
142
        $sizeX -= 4 + 6;
143
144
        $rowLayout =  $this->uiFactory->createLayoutLine(
145
            0,
146
            0,
147
            [
148
                $this->uiFactory
149
                    ->createLabel($config->getName())
150
                    ->setWidth($sizeX * 0.35)
151
                    ->setHorizontalAlign(Control::RIGHT)
152
                    ->setTranslate(true)
153
                    ->setTextId($config->getName())
154
                    ->setText(null),
155
                $this->configUiManager->getUiHandler($config)->build($config, $sizeX * 0.65),
156
                $descriptionButton
157
            ],
158
            2
159
        );
160
161
        return $rowLayout;
162
    }
163
164
165
    /**
166
     * @param ManialinkInterface $manialink
167
     * @param $login
168
     * @param $entries
169
     * @param $args
170
     */
171
    public function saveCallback(ManialinkInterface $manialink, $login, $entries, $args)
172
    {
173
        $configs = $this->configManager->getConfigDefinitionTree();
174
175
        $error = false;
176
        // First check all.
177
        foreach ($entries as $configPath => $configValue) {
178
            $config = $configs->get($configPath);
179
180
            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...
181
                try {
182
                    $config->validate($configValue);
183
                } catch (InvalidConfigException $invalidConfigException) {
184
                    $this->chatNotification->sendMessage(
185
                        $invalidConfigException->getTranslatableMessage(),
186
                        $manialink->getUserGroup(),
187
                        $invalidConfigException->getTranslationParameters()
188
                    );
189
190
                    $error = true;
191
                }
192
            }
193
        }
194
195
        if ($error) {
196
            // Don't set values and dont refresh window.
197
            return;
198
        }
199
200
        // Then save all if all is ok.
201
        foreach ($entries as $configPath => $configValue) {
202
            $config = $configs->get($configPath);
203
204
            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...
205
                $config->setRawValue($configValue);
206
            }
207
        }
208
209
        $this->setCurrentPath($args['path']);
210
        $this->update($manialink->getUserGroup());
211
212
213
        $this->chatNotification->sendMessage('eXpansion.config.action.saved', $manialink->getUserGroup());
214
    }
215
216
    /**
217
     * Set path to display in.
218
     *
219
     * @param string $currentPath
220
     */
221
    public function setCurrentPath(string $currentPath)
222
    {
223
        $this->currentPath = $currentPath;
224
    }
225
}
226