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