Completed
Pull Request — master (#294)
by De Cramer
17:11
created

TextListField::getElementLine()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

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 25
ccs 0
cts 18
cp 0
rs 8.8571
cc 1
eloc 17
nc 1
nop 4
crap 2
1
<?php
2
3
namespace eXpansion\Framework\Config\Ui\Fields;
4
5
use eXpansion\Framework\Config\Model\ConfigInterface;
6
use eXpansion\Framework\Config\Model\TextListConfig;
7
use eXpansion\Framework\Config\Ui\Window\ConfigWindowFactory;
8
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
9
use eXpansion\Framework\Core\Plugins\Gui\ActionFactory;
10
use eXpansion\Framework\Gui\Ui\Factory;
11
use FML\Types\Renderable;
12
13
/**
14
 * Class TextListField
15
 *
16
 * @author    de Cramer Oliver<[email protected]>
17
 * @copyright 2018 eXpansion
18
 * @package eXpansion\Framework\Config\Ui\Fields
19
 */
20
class TextListField extends TextField
21
{
22
    /** @var ActionFactory */
23
    protected $actionFactory;
24
25
    /** @var ConfigWindowFactory */
26
    protected $configWindowFactory;
27
28
    /**
29
     * TextListField constructor.
30
     *
31
     * @param Factory $uiFactory
32
     * @param ActionFactory $actionFactory
33
     * @param ConfigWindowFactory $configWindowFactory
34
     */
35
    public function __construct(
36
        Factory $uiFactory,
37
        ActionFactory $actionFactory,
38
        ConfigWindowFactory $configWindowFactory
39
    ) {
40
        parent::__construct($uiFactory);
41
42
        $this->actionFactory = $actionFactory;
43
        $this->configWindowFactory = $configWindowFactory;
44
    }
45
46
47
    /**
48
     * Create interface for the config value.
49
     *
50
     * @param ConfigInterface $config
51
     *
52
     * @return Renderable
53
     */
54
    public function build(ConfigInterface $config, $width, ManialinkInterface $manialink): Renderable
55
    {
56
        $input = $this
57
            ->uiFactory
58
            ->createInput($config->getPath(), '')
59
            ->setWidth($width * 0.66);
60
        $addButton = $this->uiFactory
61
            ->createButton('Add')
62
            ->setWidth($width * 0.33)
63
            ->setAction(
64
                $this->actionFactory->createManialinkAction(
65
                    $manialink,
66
                    function (ManialinkInterface $manialink, $login, $entries, $args) {
67
                        /** @var TextListConfig $config */
68
                        $config = $args['config'];
69
70
                        if (!empty($entries[$config->getPath()])) {
71
                            $config->add(trim($entries[$config->getPath()]));
72
73
                            $this->configWindowFactory->update($manialink->getUserGroup());
74
                        }
75
                    },
76
                    ['config' => $config]
77
                )
78
            );
79
80
        $elements = [$this->uiFactory->createLayoutLine(0,0, [$input, $addButton])];
81
        foreach ($config->get() as $element) {
82
            $elements[] = $this->getElementLine($config, $manialink, $element, $width);
83
        }
84
85
        return $this->uiFactory->createLayoutRow(0, 0, $elements, 0.5);
86
    }
87
88
    /**
89
     * Get the display of a single line.
90
     *
91
     * @param $config
92
     * @param $manialink
93
     * @param $element
94
     * @param $width
95
     *
96
     * @return \eXpansion\Framework\Gui\Layouts\LayoutLine
97
     */
98
    protected function getElementLine($config, $manialink, $element, $width)
99
    {
100
        $label = $this->uiFactory
101
            ->createLabel($this->getElementName($element))
102
            ->setX(2)
103
            ->setWidth($width * 0.66);
104
        $delButton = $this->uiFactory
105
            ->createButton('Remove')
106
            ->setWidth($width * 0.33)
107
            ->setAction(
108
                $this->actionFactory->createManialinkAction(
109
                    $manialink,
110
                    function (ManialinkInterface $manialink, $login, $entries, $args) {
111
                        /** @var TextListConfig $config */
112
                        $config = $args['config'];
113
                        $config->remove($args['element']);
114
115
                        $this->configWindowFactory->update($manialink->getUserGroup());
116
                    },
117
                    ['config' => $config, 'element' => $element]
118
                )
119
            );
120
121
        return $this->uiFactory->createLayoutLine(0,0, [$label, $delButton]);
122
    }
123
124
    /**
125
     * Get the text to display for any element.
126
     *
127
     * @param $element
128
     *
129
     * @return string
130
     */
131
    protected function getElementName($element)
132
    {
133
        return $element;
134
    }
135
136
    /**
137
     * Check if Ui is compatible with the given config.
138
     *
139
     * @param ConfigInterface $config
140
     *
141
     * @return bool
142
     */
143
    public function isCompatible(ConfigInterface $config): bool
144
    {
145
        return $config instanceof TextListConfig;
0 ignored issues
show
Bug introduced by
The class eXpansion\Framework\Config\Model\TextListConfig 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...
146
    }
147
}
148