Completed
Pull Request — master (#127)
by De Cramer
04:50
created

TitleLineFactory::create()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5.0214

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 19
cts 21
cp 0.9048
rs 8.439
c 0
b 0
f 0
cc 5
eloc 21
nc 9
nop 4
crap 5.0214
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\uiLabel;
8
use eXpansion\Framework\Gui\Components\uiTooltip;
9
use eXpansion\Framework\Gui\Ui\Factory;
10
use FML\Controls\Control;
11
use FML\Controls\Frame;
12
use FML\Controls\Label;
13
use oliverde8\AssociativeArraySimplified\AssociativeArray;
14
15
/**
16
 * Class LineBuilder
17
 *
18
 * @package eXpansion\Framework\Core\Model\Gui\Builders;
19
 * @author  oliver de Cramer <[email protected]>
20
 */
21
class TitleLineFactory
22
{
23
    /** @var Factory */
24
    protected $uiFactory;
25
26
    /**
27
     * TitleLineFactory constructor.
28
     *
29
     * @param Factory $uiFactory
30
     */
31 5
    public function __construct(
32
        Factory $uiFactory
33
    ) {
34 5
        $this->uiFactory = $uiFactory;
35 5
    }
36
37
    /**
38
     * Create a multi column line.
39
     *
40
     * @param float $totalWidth
41
     * @param array $columns
42
     * @param int $index
43
     * @param float $height
44
     * @param bool $autoNewLine
0 ignored issues
show
Bug introduced by
There is no parameter named $autoNewLine. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
45
     * @param int $maxLines
0 ignored issues
show
Bug introduced by
There is no parameter named $maxLines. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
46
     *
47
     * @return Frame
48
     *
49
     * @throws \Exception
50
     */
51 5
    public function create($totalWidth, $columns, $index = 0, $height = 5.0)
0 ignored issues
show
Unused Code introduced by
The parameter $index is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
    {
53
54 5
        $tooltip = $this->uiFactory->createTooltip();
55
56
        $totalCoef
57 5
            = ($totalWidth - 1) / array_reduce($columns, function ($carry, $item) {
58 5
                return $carry + $item['width'];
59 5
            });
60
61 5
        $frame = Frame::create();
62 5
        $postX = 1;
63 5
        foreach ($columns as $columnData) {
64 5
            $action = null;
65 5
            if (isset($columnData['action'])) {
66
                $action = $columnData['action'];
67
            }
68
69 5
            if (isset($columnData['title'])) {
70 5
                $element = $this->createTitleColumn($totalCoef, $columnData, $postX, $height, $action, $tooltip);
71
            }
72
73 5
            if (!isset($element)) {
74
                throw new \Exception('Element not found.');
75
            }
76
77 5
            $frame->addChild($element);
78 5
            $postX += $columnData["width"] * $totalCoef;
79
        }
80
81 5
        $line = $this->uiFactory->createLine(0, -$height);
82 5
        $line->setLength($totalWidth)->setStoke(0.33)->setColor("fff");
83
84 5
        $frame->addChild($line);
85
86 5
        return $frame;
87
    }
88
89
    /**
90
     * @param float $totalCoef
91
     * @param array $columnData
92
     * @param float $postX
93
     * @param float $height
94
     * @param string $action
95
     * @param uiTooltip $tooltip
96
     * @return \eXpansion\Framework\Gui\Layouts\layoutLine
97
     */
98 5
    protected function createTitleColumn($totalCoef, $columnData, $postX, $height, $action, uiTooltip $tooltip)
99
    {
100 5
        $sort = AssociativeArray::getFromKey($columnData, 'sort', "");
101
        switch ($sort) {
102 5
            case "ASC":
103
                $sortType = " ";
104
                break;
105 5
            case "DESC":
106
                $sortType = " ";
107
                break;
108
            default:
109 5
                $sortType = "";
110 5
                break;
111
        }
112
113 5
        $sortLabel = $this->uiFactory->createLabel($sortType, uiLabel::TYPE_HEADER);
114 5
        $sortLabel->setSize(3, $height - 1);
115
116 5
        $label = $this->uiFactory->createLabel($columnData['title'], uiLabel::TYPE_HEADER);
117
118 5
        $translate = AssociativeArray::getFromKey($columnData, 'translatable', false);
119 5
        if ($translate) {
120 5
            $label->setTextId($columnData['title']);
121 5
            $label->setTranslate(true);
122
        }
123
124 5
        $label->setHeight($height - 1)
125 5
            ->setWidth(($columnData["width"] * $totalCoef) - 3.5)
126 5
            ->setPosition($postX, -0.5)
127 5
            ->setAutoNewLine(false)
128 5
            ->setMaxLines(1);
129
130 5
        if (!empty($action)) {
131
            $label->setAction($action);
132
            $tooltip->addTooltip($label, "click to sort");
133
        }
134
135 5
        return $this->uiFactory->createLayoutLine($postX, -0.5, [$label, $sortLabel]);
136
    }
137
}
138