ConfigForm::__construct()   C
last analyzed

Complexity

Conditions 16
Paths 80

Size

Total Lines 73
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 0
Metric Value
cc 16
eloc 52
c 0
b 0
f 0
nc 80
nop 4
dl 0
loc 73
ccs 0
cts 58
cp 0
crap 272
rs 5.5666

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 PSFS\base\config;
4
5
use PSFS\base\types\Form;
6
7
/**
8
 * Class ConfigForm
9
 * @package PSFS\base\config
10
 */
11
class ConfigForm extends Form
12
{
13
14
    /**
15
     * Constructor por defecto
16
     * @param string $route
17
     * @param array $required
18
     * @param array $optional
19
     * @param array $data
20
     * @throws \PSFS\base\exception\FormException
21
     * @throws \PSFS\base\exception\RouterException
22
     */
23
    public function __construct($route, array $required, array $optional = [], array $data = [])
24
    {
25
        parent::__construct();
26
        $this->setAction($route);
27
        //Añadimos los campos obligatorios
28
        foreach ($required as $field) {
29
            $type = in_array($field, Config::$encrypted) ? 'password' : 'text';
30
            $value = isset(Config::$defaults[$field]) ? Config::$defaults[$field] : null;
31
            $this->add($field, array(
32
                'label' => t($field),
33
                'class' => 'col-md-6',
34
                'required' => true,
35
                'type' => $type,
36
                'value' => $value,
37
            ));
38
        }
39
        $this->add(Form::SEPARATOR);
40
        if (!empty($optional) && !empty($data)) {
41
            foreach ($optional as $field) {
42
                if (array_key_exists($field, $data) && strlen($data[$field] ?? '') > 0) {
43
                    $type = preg_match('/(password|secret)/i', $field) ? 'password' : 'text';
44
                    $this->add($field, array(
45
                        'label' => t($field),
46
                        'class' => 'col-md-6',
47
                        'required' => false,
48
                        'value' => $data[$field],
49
                        'type' => $type,
50
                    ));
51
                }
52
            }
53
        }
54
        $extra = array();
55
        $extraKeys = array();
56
        if (!empty($data)) {
57
            $extraKeys = array_keys($data);
58
            $extra = array_diff($extraKeys, array_merge($required, $optional));
59
        }
60
        if (!empty($extra)) {
61
            foreach ($extra as $key => $field) {
62
                if (strlen($data[$field]) > 0) {
63
                    $type = preg_match('/(password|secret)/i', $field) ? 'password' : 'text';
64
                    $this->add($extraKeys[$key], array(
65
                        'label' => $field,
66
                        'class' => 'col-md-6',
67
                        'required' => false,
68
                        'value' => $data[$field],
69
                        'type' => $type,
70
                    ));
71
                }
72
            }
73
        }
74
        $this->add(Form::SEPARATOR);
75
        //Aplicamos estilo al formulario
76
        $this->setAttrs(array(
77
            'class' => 'form-horizontal',
78
        ));
79
        //Hidratamos el formulario
80
        $this->setData($data);
81
        //Añadimos las acciones del formulario
82
        $add = [
83
            'class' => 'btn-warning md-default',
84
            'icon' => 'fa-plus',
85
        ];
86
        if (Config::getParam('admin.version', 'v1') === 'v1') {
87
            $add['onclick'] = 'javascript:addNewField(document.getElementById("' . $this->getName() . '"));';
88
        } else {
89
            $add['ng-click'] = 'addNewField()';
90
        }
91
        $this->addButton('submit', t('Guardar configuración'), 'submit', array(
92
            'class' => 'btn-success col-md-offset-2 md-primary',
93
            'icon' => 'fa-save',
94
        ))
95
            ->addButton('add_field', t('Añadir nuevo parámetro'), 'button', $add);
96
    }
97
98
    /**
99
     * Nombre del formulario
100
     * @return string
101
     */
102
    public function getName()
103
    {
104
        return 'config';
105
    }
106
107
    /**
108
     * Nombre del título del formulario
109
     * @return string
110
     */
111
    public function getTitle()
112
    {
113
        return t('Parámetros necesarios para la ejecución de PSFS');
114
    }
115
}
116