Test Setup Failed
Pull Request — master (#190)
by
unknown
17:12
created

schema::resolve_field_options()   D

Complexity

Conditions 18
Paths 1

Size

Total Lines 99
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 18

Importance

Changes 0
Metric Value
cc 18
eloc 68
nc 1
nop 2
dl 0
loc 99
rs 4.7996
c 0
b 0
f 0
ccs 28
cts 28
cp 1
crap 18

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
 * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
4
 */
5
6
namespace midcom\datamanager;
7
8
use midcom_error;
9
use Symfony\Component\Form\FormBuilderInterface;
10
use Symfony\Component\OptionsResolver\Options;
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
use Symfony\Component\Validator\Constraints\NotBlank;
13
use midcom;
14
use midcom_core_context;
15
use midcom\datamanager\extension\compat;
16
17
/**
18
 * Experimental schema class
19
 */
20
class schema
21
{
22
    private $defaults = array(
23
        'operations' => array('save' => '', 'cancel' => '')
24
    );
25
26
    private $config = array();
27
28
    /**
29
     *
30
     * @var string
31
     */
32
    private $name = 'default';
33 5
34
    public function __construct(array $config)
35 5
    {
36 5
        $this->config = array_merge($this->defaults, $config);
37 5
        $this->complete_fields();
38
    }
39
40
    /**
41
     *
42
     * @param string $name
43 1
     */
44
    public function set_name($name)
45 1
    {
46 1
        $this->name = $name;
47
    }
48
49
    /**
50
     *
51
     * @return string
52 1
     */
53
    public function get_name()
54 1
    {
55
        return $this->name;
56
    }
57
58
    /**
59
     *
60
     * @param FormBuilderInterface $builder
61
     * @return \Symfony\Component\Form\Form
62 1
     */
63
    public function build_form(FormBuilderInterface $builder)
64 1
    {
65
        foreach ($this->config['fields'] as $name => $config) {
66
            $options = array(
67
                'label' => $config['title'],
68
                'widget_config' => $config['widget_config'],
69
                'type_config' => $config['type_config'],
70
                'required' => $config['required'],
71
                'constraints' => $config['required'] ? array(new NotBlank()) : null,
72
                'dm2_type' => $config['type'],
73
                'start_fieldset' => $config['start_fieldset'],
74
                'end_fieldset' => $config['end_fieldset'],
75
                'index_method' => $config['index_method']
76
            );
77
78
            // Symfony < 2.8 compat
79
            if (compat::is_legacy()) {
80
                $options['read_only'] = $config['readonly'];
81
            } else {
82
                $options['attr']['readonly'] = $config['readonly'];
83
            }
84
85
            $builder->add($name, compat::get_type_name($config['widget']), $options);
86
        }
87 1
88 1
        $builder->add('form_toolbar', compat::get_type_name('toolbar'), array('operations' => $this->config['operations']));
89
        return $builder->getForm();
90
    }
91
92
    /**
93
     *
94
     * @return array
95 5
     */
96
    public function get_fields()
97 5
    {
98
        return $this->config['fields'];
99
    }
100
101
    /**
102
     *
103
     * @return \midcom_services_i18n_l10n
104 1
     */
105
    public function get_l10n()
106
    {
107 1
        // Populate the l10n_schema member
108
        if (array_key_exists('l10n_db', $this->config)) {
109
            $l10n_name = $this->config['l10n_db'];
110 1
        } else {
111
            $l10n_name = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_COMPONENT);
112 1
        }
113 1
        if (!midcom::get()->componentloader->is_installed($l10n_name)) {
114
            $l10n_name = 'midcom';
115 1
        }
116
        return midcom::get()->i18n->get_l10n($l10n_name);
117
    }
118
119 5
120
    private function complete_fields()
121 5
    {
122 4
        foreach ($this->config['fields'] as $name => &$config) {
123
            $config = $this->resolve_field_options($config, $name);
124 5
        }
125
    }
126 4
127
    private function resolve_field_options(array $config, $name)
128 4
    {
129
        $resolver = new OptionsResolver();
130 4
131 4
        $resolver->setDefaults(array(
132
            'title' => '',
133
            'type' => null,
134
            'type_config' => array(),
135
            'widget' => null,
136
            'widget_config' => array(),
137
            'required' => false,
138
            'readonly' => false,
139
            'default' => null,
140
            'storage' => '__UNSET__',
141
            'index_method' => 'auto',
142
            'start_fieldset' => null,
143
            'end_fieldset' => null,
144
            'validation' => array()
145
        ));
146 4
147 4
        $normalize_widget = function (Options $options, $value) {
148 1
            if (   $value == 'images'
149
                || $value == 'downloads') {
150 3
                return 'subform';
151 4
            }
152
            return $value;
153 4
        };
154
155 4
        $normalize_storage = function (Options $options, $value) use ($name) {
156 4
            $default = array(
157 4
                'location' => 'parameter',
158
                'domain' => 'midcom.helper.datamanager2',
159 4
                'name' => $name
160
            );
161
            if ($value === '__UNSET__') {
162 4
                return $default;
163 1
            }
164
            if ($value === null) {
165 3
                if ($options['type'] === 'privilege') {
166 1
                    return array(
167
                        'location' => 'privilege',
168
                        'name' => $name
169 1
                    );
170 1
                }
171
                return null;
172
            }
173
            if (is_string($value)) {
174 2
                if ($value === 'metadata') {
175 2
                    return array('location' => $value, 'name' => $name);
176 2
                }
177
                if ($value === 'parameter') {
178
                    return $default;
179
                }
180 2
                return array('location' => $value);
181
            }
182
            if (strtolower($value['location']) === 'parameter') {
183 2
                $value['location'] = strtolower($value['location']);
184 4
                if (!array_key_exists('domain', $value)) {
185
                    $value['domain'] = 'midcom.helper.datamanager2';
186 4
                }
187 4
            }
188
            if (strtolower($value['location']) === 'configuration') {
189 4
                $value['location'] = 'parameter';
190
            }
191
            return $value;
192
        };
193
194
        $normalize_validation = function (Options $options, $config) {
195
            $validation = array();
196
            if (array_key_exists('validation', (array) $config)) {
197
                $validation = (array) $config['validation'];
198
            }
199
200
            foreach ($validation as $key => $rule) {
201
                if (!is_array($rule)) {
202
                    $rule = array('type' => $rule);
203
                } elseif (!array_key_exists('type', $rule)) {
204
                    throw new midcom_error("Missing validation rule type for rule {$key} on field {$config['name']}, this is a required option.");
205
                } elseif (   $rule['type'] == 'compare'
206
                          && !array_key_exists('compare_with', $rule)) {
207
                    throw new midcom_error("Missing compare_with option for compare type rule {$key} on field {$config['name']}, this is a required option.");
208
                }
209
210
                $defaults = array(
211
                    'message' => "validation failed: {$rule['type']}",
212
                    'format' => ''
213
                );
214
215
                $validation[$key] = array_merge($defaults, $rule);
216
            }
217
            return $validation;
218
        };
219
220
        $resolver->setNormalizer('storage', $normalize_storage);
221
        $resolver->setNormalizer('widget', $normalize_widget);
222
        $resolver->setNormalizer('validation', $normalize_validation);
223
224
        return $resolver->resolve($config);
225
    }
226
}
227