Completed
Pull Request — master (#233)
by De Cramer
08:53
created

ConfigWindowFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
        $tooltip->addTooltip($descriptionButton, $config->getDescription());
140
141
        $sizeX -= 4 + 6;
142
143
        $rowLayout =  $this->uiFactory->createLayoutLine(
144
            0,
145
            0,
146
            [
147
                $this->uiFactory
148
                    ->createLabel($config->getName())
149
                    ->setWidth($sizeX * 0.35)
150
                    ->setHorizontalAlign(Control::RIGHT),
151
                $this->configUiManager->getUiHandler($config)->build($config, $sizeX * 0.65),
152
                $descriptionButton
153
            ],
154
            2
155
        );
156
157
        return $rowLayout;
158
    }
159
160
161
    /**
162
     * @param ManialinkInterface $manialink
163
     * @param $login
164
     * @param $entries
165
     * @param $args
166
     */
167
    public function saveCallback(ManialinkInterface $manialink, $login, $entries, $args)
168
    {
169
        $configs = $this->configManager->getConfigDefinitionTree();
170
171
        $error = false;
172
        // First check all.
173
        foreach ($entries as $configPath => $configValue) {
174
            $config = $configs->get($configPath);
175
176
            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...
177
                try {
178
                    $config->validate($configValue);
179
                } catch (InvalidConfigException $invalidConfigException) {
180
                    $this->chatNotification->sendMessage(
181
                        $invalidConfigException->getTranslatableMessage(),
182
                        $manialink->getUserGroup(),
183
                        $invalidConfigException->getTranslationParameters()
184
                    );
185
186
                    $error = true;
187
                }
188
            }
189
        }
190
191
        if ($error) {
192
            // Don't set values and dont refresh window.
193
            return;
194
        }
195
196
        // Then save all if all is ok.
197
        foreach ($entries as $configPath => $configValue) {
198
            $config = $configs->get($configPath);
199
200
            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...
201
                $config->setRawValue($configValue);
202
            }
203
        }
204
205
        $this->setCurrentPath($args['path']);
206
        $this->update($manialink->getUserGroup());
207
208
209
        $this->chatNotification->sendMessage('eXpansion.config.action.saved', $manialink->getUserGroup());
210
    }
211
212
    /**
213
     * Set path to display in.
214
     *
215
     * @param string $currentPath
216
     */
217
    public function setCurrentPath(string $currentPath)
218
    {
219
        $this->currentPath = $currentPath;
220
    }
221
}
222