Completed
Pull Request — master (#233)
by De Cramer
10:00
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\Model\ConfigInterface;
6
use eXpansion\Framework\Config\Services\ConfigManager;
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 FML\Controls\Frame;
15
16
/**
17
 * Class ConfigWindowFactory
18
 *
19
 * @author    de Cramer Oliver<[email protected]>
20
 * @copyright 2018 eXpansion
21
 * @package eXpansion\Framework\Config\Ui\Window
22
 */
23
class ConfigWindowFactory extends WindowFactory
24
{
25
    /** @var string */
26
    protected $currentPath;
27
28
    /** @var ConfigManagerInterface */
29
    protected $configManager;
30
31
    /** @var ConfigUiManager */
32
    protected $configUiManager;
33
34
    /** @var ChatNotification */
35
    protected $chatNotification;
36
37
    /**
38
     * ConfigWindowFactory constructor.
39
     *
40
     * @param                        $name
41
     * @param                        $sizeX
42
     * @param                        $sizeY
43
     * @param null                   $posX
44
     * @param null                   $posY
45
     * @param WindowFactoryContext   $context
46
     * @param ConfigManagerInterface $configManager
47
     * @param ConfigUiManager        $configUiManager
48
     * @param ChatNotification       $chatNotification
49
     */
50
    public function __construct(
51
        $name,
52
        $sizeX,
53
        $sizeY,
54
        $posX = null,
55
        $posY = null,
56
        WindowFactoryContext $context,
57
        ConfigManagerInterface $configManager,
58
        ConfigUiManager $configUiManager,
59
        ChatNotification $chatNotification
60
    ) {
61
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
62
63
        $this->configManager = $configManager;
64
        $this->configUiManager = $configUiManager;
65
        $this->chatNotification = $chatNotification;
66
    }
67
68
69
    /**
70
     * @inheritdoc
71
     */
72
    protected function updateContent(ManialinkInterface $manialink)
73
    {
74
        parent::updateContent($manialink);
75
        /** @var Frame $contentFrame */
76
        $contentFrame = $manialink->getContentFrame();
77
        $contentFrame->removeAllChildren();
78
79
        // TODO should use scrollbars here as we can't use grid.
80
        // see how grid is built to fix this.
81
        $configs = $this->configManager->getConfigDefinitionTree()->get($this->currentPath);
82
83
        $elements = [];
84
        foreach ($configs as $config) {
85
            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...
86
                throw new PlayerException("{$this->currentPath} is not valid configuration path");
87
            }
88
89
            $elements[] = $this->configUiManager->getUiHandler($config)->build($config, $this->sizeX - 8);
90
        }
91
        $contentFrame->addChild($this->uiFactory->createLayoutRow(0, 0, $elements));
92
93
        $saveButton = $this->uiFactory->createButton('expansion_config.save');
94
        $saveButton->setAction(
95
            $this->actionFactory->createManialinkAction(
96
                $manialink,
97
                [$this, 'saveCallback'],
98
                ['path' => $this->currentPath]
99
            )
100
        );
101
        $saveButton->setPosition(
102
            $this->sizeX - $saveButton->getWidth() - 8,
103
            -$this->sizeY + $saveButton->getHeight() + 8
104
        );
105
        $contentFrame->addChild($saveButton);
106
    }
107
108
    /**
109
     * @param ManialinkInterface $manialink
110
     * @param $login
111
     * @param $entries
112
     * @param $args
113
     */
114
    public function saveCallback(ManialinkInterface $manialink, $login, $entries, $args)
115
    {
116
        $configs = $this->configManager->getConfigDefinitionTree();
117
118
        foreach ($entries as $configPath => $configValue) {
119
            $config = $configs->get($configPath);
120
121
            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...
122
                $config->setRawValue($configValue);
123
            }
124
        }
125
126
        $this->setCurrentPath($args['path']);
127
        $this->update($manialink->getUserGroup());
128
129
130
        $this->chatNotification->sendMessage('eXpansion.config.action.saved', $manialink->getUserGroup());
131
    }
132
133
    /**
134
     * Set path to display in.
135
     *
136
     * @param string $currentPath
137
     */
138
    public function setCurrentPath(string $currentPath)
139
    {
140
        $this->currentPath = $currentPath;
141
    }
142
}
143