EventType::buildForm()   F
last analyzed

Complexity

Conditions 17
Paths 392

Size

Total Lines 250
Code Lines 166

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 166
nc 392
nop 2
dl 0
loc 250
rs 1.5866
c 0
b 0
f 0

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
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\CampaignBundle\Form\Type;
13
14
use Mautic\CoreBundle\Form\EventListener\CleanFormSubscriber;
15
use Mautic\CoreBundle\Form\Type\ButtonGroupType;
16
use Mautic\CoreBundle\Form\Type\FormButtonsType;
17
use Mautic\CoreBundle\Form\Type\PropertiesTrait;
18
use Symfony\Component\Form\AbstractType;
19
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
20
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
21
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
22
use Symfony\Component\Form\Extension\Core\Type\NumberType;
23
use Symfony\Component\Form\Extension\Core\Type\TextType;
24
use Symfony\Component\Form\Extension\Core\Type\TimeType;
25
use Symfony\Component\Form\FormBuilderInterface;
26
use Symfony\Component\OptionsResolver\OptionsResolver;
27
28
/**
29
 * Class EventType.
30
 */
31
class EventType extends AbstractType
32
{
33
    use PropertiesTrait;
34
35
    public function buildForm(FormBuilderInterface $builder, array $options)
36
    {
37
        $masks = [];
38
39
        $builder->add(
40
            'name',
41
            TextType::class,
42
            [
43
                'label'      => 'mautic.core.name',
44
                'label_attr' => ['class' => 'control-label'],
45
                'attr'       => ['class' => 'form-control'],
46
                'required'   => false,
47
            ]
48
        );
49
50
        $builder->add(
51
            'anchor',
52
            HiddenType::class,
53
            [
54
                'label' => false,
55
            ]
56
        );
57
58
        if (in_array($options['data']['eventType'], ['action', 'condition'])) {
59
            $label = 'mautic.campaign.form.type';
60
61
            $choices = [
62
                'immediate' => 'mautic.campaign.form.type.immediate',
63
                'interval'  => 'mautic.campaign.form.type.interval',
64
                'date'      => 'mautic.campaign.form.type.date',
65
            ];
66
67
            if ('no' == $options['data']['anchor'] && 'condition' != $options['data']['anchorEventType']
68
                && 'condition' != $options['data']['eventType']
69
            ) {
70
                $label .= '_inaction';
71
72
                unset($choices['immediate']);
73
                $choices['interval'] = $choices['interval'].'_inaction';
74
                $choices['date']     = $choices['date'].'_inaction';
75
            }
76
77
            reset($choices);
78
            $default = key($choices);
79
80
            $triggerMode = (empty($options['data']['triggerMode'])) ? $default : $options['data']['triggerMode'];
81
            $builder->add(
82
                'triggerMode',
83
                ButtonGroupType::class,
84
                [
85
                    'choices'           => array_flip($choices),
86
                    'expanded'          => true,
87
                    'multiple'          => false,
88
                    'label_attr'        => ['class' => 'control-label'],
89
                    'label'             => $label,
90
                    'placeholder'       => false,
91
                    'required'          => false,
92
                    'attr'              => [
93
                        'onchange' => 'Mautic.campaignToggleTimeframes();',
94
                        'tooltip'  => 'mautic.campaign.form.type.help',
95
                    ],
96
                    'data'        => $triggerMode,
97
                ]
98
            );
99
100
            $builder->add(
101
                'triggerDate',
102
                DateTimeType::class,
103
                [
104
                    'label'  => false,
105
                    'attr'   => [
106
                        'class'       => 'form-control',
107
                        'preaddon'    => 'fa fa-calendar',
108
                        'data-toggle' => 'datetime',
109
                    ],
110
                    'widget' => 'single_text',
111
                    'format' => 'yyyy-MM-dd HH:mm',
112
                ]
113
            );
114
115
            $data = (!isset($options['data']['triggerInterval']) || '' === $options['data']['triggerInterval']
116
                || null === $options['data']['triggerInterval']) ? 1 : (int) $options['data']['triggerInterval'];
117
            $builder->add(
118
                'triggerInterval',
119
                NumberType::class,
120
                [
121
                    'label' => false,
122
                    'attr'  => [
123
                        'class'    => 'form-control',
124
                        'preaddon' => 'symbol-hashtag',
125
                    ],
126
                    'data'  => $data,
127
                ]
128
            );
129
130
            $data = (!empty($options['data']['triggerIntervalUnit'])) ? $options['data']['triggerIntervalUnit'] : 'd';
131
            $builder->add(
132
                'triggerIntervalUnit',
133
                ChoiceType::class,
134
                [
135
                    'choices'     => [
136
                        'mautic.campaign.event.intervalunit.choice.i' => 'i',
137
                        'mautic.campaign.event.intervalunit.choice.h' => 'h',
138
                        'mautic.campaign.event.intervalunit.choice.d' => 'd',
139
                        'mautic.campaign.event.intervalunit.choice.m' => 'm',
140
                        'mautic.campaign.event.intervalunit.choice.y' => 'y',
141
                    ],
142
                    'multiple'          => false,
143
                    'label_attr'        => ['class' => 'control-label'],
144
                    'label'             => false,
145
                    'attr'              => [
146
                        'class' => 'form-control',
147
                    ],
148
                    'placeholder' => false,
149
                    'required'    => false,
150
                    'data'        => $data,
151
                ]
152
            );
153
154
            // I could not get Doctrine TimeType does not play well with Symfony TimeType so hacking this workaround
155
            $data = $this->getTimeValue($options['data'], 'triggerHour');
156
            $builder->add(
157
                'triggerHour',
158
                TextType::class,
159
                [
160
                    'label' => false,
161
                    'attr'  => [
162
                        'class'        => 'form-control',
163
                        'data-toggle'  => 'time',
164
                        'data-format'  => 'H:i',
165
                        'autocomplete' => 'off',
166
                    ],
167
                    'data'  => ($data) ? $data->format('H:i') : $data,
168
                ]
169
            );
170
171
            $data = $this->getTimeValue($options['data'], 'triggerRestrictedStartHour');
172
            $builder->add(
173
                'triggerRestrictedStartHour',
174
                TextType::class,
175
                [
176
                    'label' => false,
177
                    'attr'  => [
178
                        'class'        => 'form-control',
179
                        'data-toggle'  => 'time',
180
                        'data-format'  => 'H:i',
181
                        'autocomplete' => 'off',
182
                    ],
183
                    'data'  => ($data) ? $data->format('H:i') : $data,
184
                ]
185
            );
186
187
            $data = $this->getTimeValue($options['data'], 'triggerRestrictedStopHour');
188
            $builder->add(
189
                'triggerRestrictedStopHour',
190
                TextType::class,
191
                [
192
                    'label' => false,
193
                    'attr'  => [
194
                        'class'        => 'form-control',
195
                        'data-toggle'  => 'time',
196
                        'data-format'  => 'H:i',
197
                        'autocomplete' => 'off',
198
                    ],
199
                    'data'  => ($data) ? $data->format('H:i') : $data,
200
                ]
201
            );
202
203
            $builder->add(
204
                'triggerRestrictedDaysOfWeek',
205
                ChoiceType::class,
206
                [
207
                    'label'    => true,
208
                    'attr'     => [
209
                        'data-toggle' => 'time',
210
                        'data-format' => 'H:i',
211
                    ],
212
                    'choices'  => [
213
                        'mautic.report.schedule.day.monday'     => 1,
214
                        'mautic.report.schedule.day.tuesday'    => 2,
215
                        'mautic.report.schedule.day.wednesday'  => 3,
216
                        'mautic.report.schedule.day.thursday'   => 4,
217
                        'mautic.report.schedule.day.friday'     => 5,
218
                        'mautic.report.schedule.day.saturday'   => 6,
219
                        'mautic.report.schedule.day.sunday'     => 0,
220
                        'mautic.report.schedule.day.week_days'  => -1,
221
                    ],
222
                    'expanded'          => true,
223
                    'multiple'          => true,
224
                    'required'          => false,
225
                ]
226
            );
227
        }
228
229
        if (!empty($options['settings']['formType'])) {
230
            $this->addPropertiesType($builder, $options, $masks);
231
        }
232
233
        $builder->add('type', HiddenType::class);
234
        $builder->add('eventType', HiddenType::class);
235
        $builder->add(
236
            'anchorEventType',
237
            HiddenType::class,
238
            [
239
                'mapped' => false,
240
                'data'   => (isset($options['data']['anchorEventType'])) ? $options['data']['anchorEventType'] : '',
241
            ]
242
        );
243
244
        $builder->add(
245
            'canvasSettings',
246
            EventCanvasSettingsType::class,
247
            [
248
                'label' => false,
249
            ]
250
        );
251
252
        $update = !empty($options['data']['properties']);
253
        if (!empty($update)) {
254
            $btnValue = 'mautic.core.form.update';
255
            $btnIcon  = 'fa fa-pencil';
256
        } else {
257
            $btnValue = 'mautic.core.form.add';
258
            $btnIcon  = 'fa fa-plus';
259
        }
260
261
        $builder->add(
262
            'buttons',
263
            FormButtonsType::class,
264
            [
265
                'save_text'       => $btnValue,
266
                'save_icon'       => $btnIcon,
267
                'save_onclick'    => 'Mautic.submitCampaignEvent(event)',
268
                'apply_text'      => false,
269
                'container_class' => 'bottom-form-buttons',
270
            ]
271
        );
272
273
        $builder->add(
274
            'campaignId',
275
            HiddenType::class,
276
            [
277
                'mapped' => false,
278
            ]
279
        );
280
281
        $builder->addEventSubscriber(new CleanFormSubscriber($masks));
282
283
        if (!empty($options['action'])) {
284
            $builder->setAction($options['action']);
285
        }
286
    }
287
288
    public function configureOptions(OptionsResolver $resolver)
289
    {
290
        $resolver->setRequired(['settings']);
291
    }
292
293
    /**
294
     * @param $name
295
     *
296
     * @return \DateTime|mixed|null
297
     */
298
    private function getTimeValue(array $data, $name)
299
    {
300
        if (empty($data[$name])) {
301
            return null;
302
        }
303
304
        if ($data[$name] instanceof \DateTime) {
305
            return $data[$name];
306
        }
307
308
        return new \DateTime($data[$name]);
309
    }
310
311
    public function getBlockPrefix()
312
    {
313
        return 'campaignevent';
314
    }
315
}
316