Completed
Pull Request — master (#124)
by De Cramer
02:35
created

LineFactory::createInputColumn()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 26
c 0
b 0
f 0
ccs 0
cts 17
cp 0
rs 8.8571
cc 3
eloc 18
nc 3
nop 3
crap 12
1
<?php
2
3
namespace eXpansion\Framework\Core\Model\Gui\Factory;
4
5
use eXpansion\Framework\Gui\Components\uiCheckbox;
6
use eXpansion\Framework\Gui\Components\uiInput;
7
use eXpansion\Framework\Gui\Components\uiTooltip;
8
use FML\Controls\Control;
9
use FML\Controls\Frame;
10
use FML\Controls\Label;
11
use oliverde8\AssociativeArraySimplified\AssociativeArray;
12
13
/**
14
 * Class LineBuilder
15
 *
16
 * @package eXpansion\Framework\Core\Model\Gui\Builders;
17
 * @author  oliver de Cramer <[email protected]>
18
 */
19
class LineFactory
20
{
21
    /** @var BackGroundFactory */
22
    protected $backGroundFactory;
23
24
    /** @var LabelFactory */
25
    protected $labelFactory;
26
27
    /** @var string */
28
    protected $type;
29
30
    /**
31
     * TitleLineFactory constructor.
32
     *
33
     * @param BackGroundFactory $backGroundFactory
34
     * @param LabelFactory $labelFactory
35
     * @param string $type
36
     */
37 9
    public function __construct(
38
        BackGroundFactory $backGroundFactory,
39
        LabelFactory $labelFactory,
40
        $type = LabelFactory::TYPE_NORMAL
41
    ) {
42 9
        $this->backGroundFactory = $backGroundFactory;
43 9
        $this->labelFactory = $labelFactory;
44 9
        $this->type = $type;
45 9
    }
46
47
    /**
48
     * Create a multi column line.
49
     *
50
     * @param float $totalWidth
51
     * @param array $columns
52
     * @param int $index
53
     * @param float $height
54
     * @param bool $autoNewLine
55
     * @param int $maxLines
56
     *
57
     * @return Frame
58
     *
59
     * @throws \Exception
60
     */
61
    public function create($totalWidth, $columns, $index = 0, $height = 5.0, $autoNewLine = false, $maxLines = 1)
62
    {
63
        $totalCoef
64 1
            = ($totalWidth - 1) / array_reduce($columns, function ($carry, $item) {
65 1
                return $carry + $item['width'];
66 1
            });
67
68 1
        $frame = new Frame();
69
        //$frame->setHeight($height);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
70
        //$frame->setWidth($totalWidth);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71
72 1
        $postX = 1;
73 1
        foreach ($columns as $columnData) {
74 1
            if (isset($columnData['text'])) {
75 1
                $element = $this->createTextColumn($totalCoef, $columnData, $postX, $height, $autoNewLine, $maxLines);
76 1
            } elseif (isset($columnData['input'])) {
77
                $element = $this->createInputColumn($totalCoef, $columnData, $postX);
78 1
            } elseif (isset($columnData['renderer'])) {
79 1
                $element = $this->createRendererColumn($columnData, $postX);
80
            }
81
82 1
            if (!isset($element)) {
83
                throw new \Exception('Element not found.');
84
            }
85
86 1
            if (isset($columnData['action'])) {
87 1
                $element->setAction($columnData['action']);
88
            }
89
90 1
            $frame->addChild($element);
91 1
            $postX += $columnData["width"] * $totalCoef;
92
        }
93
94 1
        $frame->addChild($this->backGroundFactory->create($totalWidth, $height, $index));
95
96 1
        return $frame;
97
    }
98
99
    /**
100
     * @param float $totalCoef
101
     * @param array $columnData
102
     * @param float $postX
103
     * @param float $height
104
     * @param bool $autoNewLine
105
     * @param int $maxLines
106
     *
107
     * @return Label
108
     */
109 1
    protected function createTextColumn($totalCoef, $columnData, $postX, $height, $autoNewLine = false, $maxLines = 1)
110
    {
111 1
        $label = $this->labelFactory->create(
112 1
            $columnData['text'],
113 1
            AssociativeArray::getFromKey($columnData, 'translatable', false),
114 1
            $this->type
115
        );
116 1
        $label->setHeight($height - 1);
117 1
        $label->setWidth(($columnData["width"] * $totalCoef) - 0.5);
118 1
        $label->setPosition($postX, -0.5);
119 1
        $label->setAutoNewLine($autoNewLine);
120 1
        $label->setMaxLines($maxLines);
121
122 1
        return $label;
123
    }
124
125
126
    protected function createInputColumn($totalCoef, $columnData, $postX)
127
    {
128
        /** @var uiTooltip $tooltip */
129
        $tooltip = $columnData['tooltip'];
130
        $value = $columnData['input'];
131
        $i = $columnData['index'];
132
        $type = gettype($value);
133
134
        if ($type == "boolean") {
135
            $element = new uiCheckbox("", "entry_".$i."_boolean", true);
136
            if ($value === false) {
137
                $element = new uiCheckbox("", "entry_".$i."_boolean", false);
138
            }
139
            $element->setY(3);
140
        } else {
141
            $element = new uiInput("entry_".$i."_".$type);
142
            $element->setDefault($value);
143
        }
144
        $element->setPosition($postX, -0.5);
145
        $element->setWidth(($columnData["width"] * $totalCoef) - 0.5);
146
        $element->setHeight(4);
147
148
        $tooltip->addTooltip($element, $type);
149
150
        return $element;
151
    }
152
153
154
    /**
155
     * @param $columnData
156
     * @param $postX
157
     *
158
     * @return Control
159
     */
160 1
    protected function createRendererColumn($columnData, $postX)
161
    {
162
        /** @var Control $element */
163 1
        $element = $columnData['renderer'];
164 1
        $element->setPosition($postX, -0.5);
165 1
        $element->setHeight(4);
166
167 1
        return $element;
168
    }
169
}
170