Completed
Pull Request — master (#233)
by De Cramer
11:00
created

ConfigWindowFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 9
dl 0
loc 84
rs 10
c 0
b 0
f 0
ccs 0
cts 49
cp 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
B createContent() 0 31 4
A saveCallback() 0 14 3
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\Model\Gui\ManialinkInterface;
11
use eXpansion\Framework\Core\Model\Gui\WindowFactoryContext;
12
use eXpansion\Framework\Core\Plugins\Gui\WindowFactory;
13
use FML\Controls\Frame;
14
15
/**
16
 * Class ConfigWindowFactory
17
 *
18
 * @author    de Cramer Oliver<[email protected]>
19
 * @copyright 2018 eXpansion
20
 * @package eXpansion\Framework\Config\Ui\Window
21
 */
22
class ConfigWindowFactory extends WindowFactory
23
{
24
    /** @var string */
25
    protected $currentPath;
26
27
    /** @var ConfigManagerInterface */
28
    protected $configManager;
29
30
    /** @var ConfigUiManager */
31
    protected $configUiManager;
32
33
    public function __construct(
34
        $name,
35
        $sizeX,
36
        $sizeY,
37
        $posX = null,
38
        $posY = null,
39
        WindowFactoryContext $context,
40
        ConfigManagerInterface $configManager,
41
        ConfigUiManager $configUiManager
42
    ) {
43
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
44
45
        $this->configManager = $configManager;
46
        $this->configUiManager = $configUiManager;
47
    }
48
49
50
    /**
51
     * @inheritdoc
52
     */
53
    protected function createContent(ManialinkInterface $manialink)
54
    {
55
        parent::createContent($manialink);
56
        /** @var Frame $contentFrame */
57
        $contentFrame = $manialink->getContentFrame();
58
59
        $manialink->setData('current_path', $this->currentPath);
60
61
        // TODO should use scrollbars here as we can't use grid.
62
        // see how grid is built to fix this.
63
        $configs = $this->configManager->getConfigDefinitionTree()->get($this->currentPath);
64
        $elements = [];
65
        foreach ($configs as $config) {
66
            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...
67
                throw new PlayerException("{$this->currentPath} is not valid configuration path");
68
            }
69
70
            $elements[] = $this->configUiManager->getUiHandler($config)->build($config, $this->sizeX - 8);
71
        }
72
        $contentFrame->addChild($this->uiFactory->createLayoutLine(0, 0, $elements));
73
74
        $saveButton = $this->uiFactory->createButton('expansion_config.save');
75
        $saveButton->setAction(
76
            $this->actionFactory->createManialinkAction($manialink, [$this, 'saveCallback'], [], true)
77
        );
78
        $saveButton->setPosition(
79
            $this->sizeX - $saveButton->getWidth() - 5,
80
            $this->sizeY - $saveButton->getHeight() - 4
81
        );
82
        $contentFrame->addChild($saveButton);
83
    }
84
85
    /**
86
     * @param ManialinkInterface $manialink
87
     * @param $login
88
     * @param $entries
89
     * @param $args
90
     */
91
    public function saveCallback(ManialinkInterface $manialink, $login, $entries, $args)
92
    {
93
        $configs = $this->configManager->getConfigDefinitionTree();
94
95
        foreach ($entries as $configPath => $configValue) {
96
            $config = $configs->get($configPath);
97
98
            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...
99
                $config->setRawValue($configValue);
100
            }
101
        }
102
103
        $this->update($manialink->getUserGroup());
104
    }
105
}
106