Passed
Push — master ( 26c354...92479f )
by Richard
05:49 queued 10s
created

VueFieldGenerator::generateHTML()   C

Complexity

Conditions 16
Paths 28

Size

Total Lines 60
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 60
rs 5.5666
cc 16
nc 28
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PWWEB\Artomator\Utils;
4
5
use InfyOm\Generator\Common\GeneratorField;
6
use InfyOm\Generator\Utils\GeneratorFieldsInputUtil;
7
8
class VueFieldGenerator
9
{
10
    /**
11
     * Generate HTML.
12
     *
13
     * @param GeneratorField $field        Field.
14
     * @param string         $templateType Template type.
15
     * @param bool           $localized    Localized.
16
     *
17
     * @return string
18
     */
19
    public static function generateHTML(GeneratorField $field, $templateType, $localized = false)
20
    {
21
        $fieldTemplate = '';
22
23
        $localized = (true === $localized) ? '_locale' : '';
24
        switch ($field->htmlType) {
25
            case 'text':
26
            case 'textarea':
27
            case 'date':
28
            case 'file':
29
            case 'email':
30
            case 'password':
31
            case 'number':
32
                $fieldTemplate = get_template('scaffold.vues.fields.'.$field->htmlType.$localized, $templateType);
33
34
                break;
35
            case 'select':
36
            case 'enum':
37
                $fieldTemplate = get_template('scaffold.vues.fields.select'.$localized, $templateType);
38
                $radioLabels = GeneratorFieldsInputUtil::prepareKeyValueArrFromLabelValueStr($field->htmlValues);
39
40
                $fieldTemplate = str_replace(
41
                    '$INPUT_ARR$',
42
                    GeneratorFieldsInputUtil::prepareKeyValueArrayStr($radioLabels),
43
                    $fieldTemplate
44
                );
45
46
                break;
47
            case 'checkbox':
48
                $fieldTemplate = get_template('scaffold.vues.fields.checkbox'.$localized, $templateType);
49
                if (count($field->htmlValues) > 0) {
50
                    $checkboxValue = $field->htmlValues[0];
51
                } else {
52
                    $checkboxValue = 1;
53
                }
54
                $fieldTemplate = str_replace('$CHECKBOX_VALUE$', $checkboxValue, $fieldTemplate);
55
56
                break;
57
            case 'radio':
58
                $fieldTemplate = get_template('scaffold.vues.fields.radio_group'.$localized, $templateType);
59
                $radioTemplate = get_template('scaffold.vues.fields.radio'.$localized, $templateType);
60
61
                $radioLabels = GeneratorFieldsInputUtil::prepareKeyValueArrFromLabelValueStr($field->htmlValues);
62
63
                $radioButtons = [];
64
                foreach ($radioLabels as $label => $value) {
65
                    $radioButtonTemplate = str_replace('$LABEL$', $label, $radioTemplate);
66
                    $radioButtonTemplate = str_replace('$VALUE$', $value, $radioButtonTemplate);
67
                    $radioButtons[] = $radioButtonTemplate;
68
                }
69
                $fieldTemplate = str_replace('$RADIO_BUTTONS$', implode("\n", $radioButtons), $fieldTemplate);
70
71
                break;
72
            case 'toggle-switch':
73
                $fieldTemplate = get_template('scaffold.vues.fields.toggle-switch'.$localized, $templateType);
74
75
                break;
76
        }
77
78
        return $fieldTemplate;
79
    }
80
}
81