Test Failed
Push — master ( e97986...2933e0 )
by Bruno
19:41 queued 09:43
created

VuetifyElement   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 36
c 1
b 0
f 0
dl 0
loc 48
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A color() 0 38 5
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Frontend\Vuetify\Element;
4
5
use Formularium\Element as Element;
6
use Formularium\Exception\Exception;
7
use Formularium\HTMLNode;
8
9
abstract class VuetifyElement extends Element
10
{
11
    const COLOR = 'color';
12
    const COLOR_PRIMARY = 'primary';
13
    const COLOR_LINK = 'link';
14
    const COLOR_INFO = 'info';
15
    const COLOR_SUCCESS = 'success';
16
    const COLOR_WARNING = 'warning';
17
    const COLOR_ERROR = 'error';
18
19
    public function color(array $parameters, HTMLNode $node): HTMLNode
20
    {
21
        $color = $parameters[self::COLOR] ?? '';
22
        if (!$color) {
23
            return $node;
24
        }
25
26
        $colorMap = [
27
            self::COLOR_PRIMARY => 'primary',
28
            self::COLOR_LINK => 'anchor',
29
            self::COLOR_INFO => 'info',
30
            self::COLOR_SUCCESS => 'success',
31
            self::COLOR_WARNING => 'warning',
32
            self::COLOR_ERROR => 'error',
33
        ];
34
35
        if (array_key_exists($color, $colorMap)) {
36
            $color = $parameters[self::COLOR];
37
        } else {
38
            $colors = [
39
                'primary',
40
                'secondary',
41
                'helper',
42
                'accent',
43
                'anchor',
44
                'error',
45
                'info',
46
                'success',
47
                'warning',
48
            ];
49
            if (!in_array($color, $colors)) {
50
                throw new Exception('Invalid button color: ' . $color);
51
            }
52
        }
53
        if ($color) {
54
            $node->addAttribute('color', $color);
55
        }
56
        return $node;
57
    }
58
}
59