Passed
Push — develop ( 6f0b3c...59c8d7 )
by Paul
14:05
created

Transformer   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 42
c 1
b 0
f 0
dl 0
loc 74
ccs 0
cts 45
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A transformCheckbox() 0 12 4
A transformNumber() 0 4 1
A transformSelect() 0 24 6
A control() 0 3 1
A transformText() 0 4 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\WPBakery;
4
5
use GeminiLabs\SiteReviews\Helper;
6
use GeminiLabs\SiteReviews\Helpers\Arr;
7
use GeminiLabs\SiteReviews\Integrations\WPBakery\Defaults\ControlDefaults;
8
9
class Transformer
10
{
11
    public string $name;
12
    public string $shortcode;
13
14
    protected array $control;
15
16
    public function __construct(string $name, array $control, string $shortcode = '')
17
    {
18
        $this->name = $name;
19
        $this->shortcode = $shortcode;
20
        $control = wp_parse_args(compact('name'), $control);
21
        $control = glsr(ControlDefaults::class)->merge($control);
22
        $method = Helper::buildMethodName('transform', $control['type']);
23
        $this->control = method_exists($this, $method)
24
            ? call_user_func([$this, $method], $control)
25
            : $control;
26
    }
27
28
    public function control(): array
29
    {
30
        return $this->control;
31
    }
32
33
    protected function transformCheckbox(array $control): array
34
    {
35
        if ('hide' === $this->name) {
36
            $control['heading'] = _x('Hide', 'admin-text', 'site-reviews');
37
        }
38
        if (!empty($control['options'])) {
39
            $control['value'] = array_flip($control['options']);
40
        } elseif (empty($control['value'])) {
41
            $control['value'] = [esc_html_x('Yes', 'admin-text', 'site-reviews') => 'true'];
42
        }
43
        unset($control['options']);
44
        return $control;
45
    }
46
47
    protected function transformNumber(array $control): array
48
    {
49
        $control['type'] = 'glsr_type_range';
50
        return $control;
51
    }
52
53
    protected function transformSelect(array $control): array
54
    {
55
        $control['type'] = 'dropdown';
56
        if (!isset($values['options']) && !isset($values['value'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $values seems to never exist and therefore isset should always be false.
Loading history...
57
            $control['type'] = 'autocomplete';
58
            $control['settings'] = [
59
                'multiple' => true,
60
                'sortable' => true,
61
            ];
62
            return $control;
63
        }
64
        if ($options = Arr::consolidate($control['value'] ?? [])) {
0 ignored issues
show
Unused Code introduced by
The assignment to $options is dead and can be removed.
Loading history...
65
            unset($control['options']);
66
            return $control;
67
        }
68
        if ($options = Arr::consolidate($control['options'] ?? [])) {
69
            if (!empty($control['placeholder'])) {
70
                $options = ['' => $control['placeholder']] + $options;
71
            }
72
            $control['value'] = array_flip($options);
73
            unset($control['options']);
74
            return $control;
75
        }
76
        return []; // invalid control
77
    }
78
79
    protected function transformText(array $control): array
80
    {
81
        $control['type'] = 'textfield';
82
        return $control;
83
    }
84
}
85