Passed
Push — bugfix/ps-9925-punchout-connec... ( 3e09d6...914c03 )
by
unknown
04:46
created

createGlobalTotalsModeValidationConstraint()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 15
rs 9.9
1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\PunchoutCatalogs\Communication\Form;
9
10
use Generated\Shared\Transfer\PunchoutCatalogConnectionTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...talogConnectionTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Spryker\Zed\Gui\Communication\Form\Type\SelectType;
12
use Spryker\Zed\Kernel\Communication\Form\AbstractType;
13
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
14
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
15
use Symfony\Component\Form\Extension\Core\Type\TextType;
16
use Symfony\Component\Form\FormBuilderInterface;
17
use Symfony\Component\Form\FormEvent;
18
use Symfony\Component\Form\FormEvents;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
use Symfony\Component\Validator\Constraints\Length;
21
use Symfony\Component\Validator\Constraints\NotBlank;
22
use Symfony\Component\Validator\Constraints\Valid;
23
24
/**
25
 * @method \SprykerEco\Zed\PunchoutCatalogs\PunchoutCatalogsConfig getConfig()
26
 * @method \SprykerEco\Zed\PunchoutCatalogs\Persistence\PunchoutCatalogsRepositoryInterface getRepository()
27
 * @method \SprykerEco\Zed\PunchoutCatalogs\Business\PunchoutCatalogsFacadeInterface getFacade()
28
 * @method \SprykerEco\Zed\PunchoutCatalogs\Communication\PunchoutCatalogsCommunicationFactory getFactory()
29
 */
30
class PunchoutCatalogConnectionForm extends AbstractType
31
{
32
    public const OPTION_BUSINESS_UNIT_CHOICES = 'OPTION_BUSINESS_UNIT_CHOICES';
33
    public const OPTION_CONNECTION_FORMAT_SUB_FORM_TYPES = 'OPTION_CONNECTION_FORMAT_FORMS';
34
    public const OPTION_CONNECTION_TYPE_SUB_FORM_TYPES = 'OPTION_CONNECTION_TYPE_SUB_FORM_TYPES';
35
36
    protected const VALIDATION_GROUP_DISABLED = 'disabled';
37
38
    protected const TOGGLE_GROUP_CONNECTION_TYPE = 'type';
39
    protected const TOGGLE_GROUP_CONNECTION_FORMAT = 'format';
40
41
    /**
42
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
43
     * @param array $options
44
     *
45
     * @return void
46
     */
47
    public function buildForm(FormBuilderInterface $builder, array $options): void
48
    {
49
        $this->addNameField($builder)
50
            ->addBusinessUnitField($builder, $options)
51
            ->addAddMappingField($builder)
52
            ->addConnectionFormatField($builder, $options)
53
            ->addConnectionTypeField($builder, $options)
54
            ->addConnectionFormatSubForms($builder, $options)
55
            ->addConnectionTypeSubForms($builder, $options);
56
    }
57
58
    /**
59
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
60
     *
61
     * @return void
62
     */
63
    public function configureOptions(OptionsResolver $resolver): void
64
    {
65
        $resolver->setRequired([
66
            static::OPTION_BUSINESS_UNIT_CHOICES,
67
            static::OPTION_CONNECTION_FORMAT_SUB_FORM_TYPES,
68
            static::OPTION_CONNECTION_TYPE_SUB_FORM_TYPES,
69
        ]);
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getBlockPrefix(): string
76
    {
77
        return 'punchoutCatalogConnection';
78
    }
79
80
    /**
81
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
82
     *
83
     * @return $this
84
     */
85
    protected function addNameField(FormBuilderInterface $builder)
86
    {
87
        $builder->add(PunchoutCatalogConnectionTransfer::NAME, TextType::class, [
88
            'label' => 'Name',
89
            'constraints' => [
90
                new NotBlank(),
91
                new Length(['max' => 255]),
92
            ],
93
        ]);
94
95
        return $this;
96
    }
97
98
    /**
99
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
100
     * @param array $options
101
     *
102
     * @return $this
103
     */
104
    protected function addConnectionFormatField(FormBuilderInterface $builder, array $options)
105
    {
106
        $formats = array_keys($options[static::OPTION_CONNECTION_FORMAT_SUB_FORM_TYPES]);
107
108
        $builder->add(PunchoutCatalogConnectionTransfer::FORMAT, ChoiceType::class, [
109
            'label' => 'Format',
110
            'choices' => array_combine($formats, $formats),
111
            'constraints' => [
112
                new NotBlank(),
113
            ],
114
            'attr' => [
115
                'class' => 'toggle-trigger',
116
                'data-toggle-group' => static::TOGGLE_GROUP_CONNECTION_FORMAT,
117
            ],
118
        ]);
119
120
        return $this;
121
    }
122
123
    /**
124
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
125
     * @param array $options
126
     *
127
     * @return $this
128
     */
129
    protected function addConnectionTypeField(FormBuilderInterface $builder, array $options)
130
    {
131
        $types = array_keys($options[static::OPTION_CONNECTION_TYPE_SUB_FORM_TYPES]);
132
133
        $builder->add(PunchoutCatalogConnectionTransfer::TYPE, ChoiceType::class, [
134
            'label' => 'Type',
135
            'choices' => array_combine($types, $types),
136
            'constraints' => [
137
                new NotBlank(),
138
            ],
139
            'attr' => [
140
                'class' => 'toggle-trigger',
141
                'data-toggle-group' => static::TOGGLE_GROUP_CONNECTION_TYPE,
142
            ],
143
        ]);
144
145
        return $this;
146
    }
147
148
    /**
149
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
150
     * @param array $options
151
     *
152
     * @return $this
153
     */
154
    protected function addConnectionFormatSubForms(FormBuilderInterface $builder, array $options)
155
    {
156
        foreach ($options[static::OPTION_CONNECTION_FORMAT_SUB_FORM_TYPES] as $connectionFormat => $connectionFormatSubFormType) {
157
            $builder->add($connectionFormat, $connectionFormatSubFormType, [
158
                'mapped' => false,
159
                'validation_groups' => static::VALIDATION_GROUP_DISABLED,
160
                'label' => false,
161
                'attr' => [
162
                    'class' => 'toggle-inner-item',
163
                    'data-toggle-type' => $connectionFormat,
164
                    'data-toggle-group' => static::TOGGLE_GROUP_CONNECTION_FORMAT,
165
                ],
166
            ]);
167
        }
168
169
        $this->addConnectionFormatDynamicSubFormListener($builder);
170
171
        return $this;
172
    }
173
174
    /**
175
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
176
     * @param array $options
177
     *
178
     * @return $this
179
     */
180
    protected function addConnectionTypeSubForms(FormBuilderInterface $builder, array $options)
181
    {
182
        foreach ($options[static::OPTION_CONNECTION_TYPE_SUB_FORM_TYPES] as $connectionType => $connectionTypeSubFormType) {
183
            $builder->add($connectionType, $connectionTypeSubFormType, [
184
                'mapped' => false,
185
                'validation_groups' => static::VALIDATION_GROUP_DISABLED,
186
                'label' => false,
187
                'constraints' => [
188
                    new Valid(),
189
                ],
190
                'attr' => [
191
                    'class' => 'toggle-inner-item',
192
                    'data-toggle-type' => $connectionType,
193
                    'data-toggle-group' => static::TOGGLE_GROUP_CONNECTION_TYPE,
194
                ],
195
            ]);
196
        }
197
198
        $this->addConnectionTypeDynamicSubFormListener($builder);
199
200
        return $this;
201
    }
202
203
    /**
204
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
205
     *
206
     * @return void
207
     */
208
    protected function addConnectionFormatDynamicSubFormListener(FormBuilderInterface $builder): void
209
    {
210
        $connectionFormatSubFormTypes = static::OPTION_CONNECTION_FORMAT_SUB_FORM_TYPES;
211
        $formModificationCallback = function (FormEvent $event) use ($connectionFormatSubFormTypes) {
212
            $format = $event->getData()[PunchoutCatalogConnectionTransfer::FORMAT] ?? null;
213
214
            if (!$format) {
215
                return;
216
            }
217
218
            $this->addActiveDependentFieldSubFormToConnectionForm(
219
                $event,
220
                $connectionFormatSubFormTypes,
221
                $format
222
            );
223
        };
224
225
        $builder->addEventListener(FormEvents::PRE_SET_DATA, $formModificationCallback);
226
        $builder->addEventListener(FormEvents::PRE_SUBMIT, $formModificationCallback);
227
    }
228
229
    /**
230
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
231
     *
232
     * @return void
233
     */
234
    protected function addConnectionTypeDynamicSubFormListener(FormBuilderInterface $builder): void
235
    {
236
        $connectionTypeSubFormTypes = static::OPTION_CONNECTION_TYPE_SUB_FORM_TYPES;
237
        $formModificationCallback = function (FormEvent $event) use ($connectionTypeSubFormTypes) {
238
            $type = $event->getData()[PunchoutCatalogConnectionTransfer::TYPE] ?? null;
239
240
            if (!$type) {
241
                return;
242
            }
243
244
            $this->addActiveDependentFieldSubFormToConnectionForm(
245
                $event,
246
                $connectionTypeSubFormTypes,
247
                $type
248
            );
249
        };
250
251
        $builder->addEventListener(FormEvents::PRE_SET_DATA, $formModificationCallback)
252
            ->addEventListener(FormEvents::PRE_SUBMIT, $formModificationCallback);
253
    }
254
255
    /**
256
     * @param \Symfony\Component\Form\FormEvent $event
257
     * @param string $subFormsOptionName
258
     * @param string $selectedSubFormName
259
     *
260
     * @return void
261
     */
262
    protected function addActiveDependentFieldSubFormToConnectionForm(FormEvent $event, string $subFormsOptionName, string $selectedSubFormName): void
263
    {
264
        $form = $event->getForm();
265
        $formOptions = $form->getConfig()->getOptions();
266
        $subForms = $formOptions[$subFormsOptionName];
267
268
        foreach ($subForms as $subFormName => $subFormType) {
269
            $existingFieldOptions = $form->get($subFormName)
270
                ->getConfig()
271
                ->getOptions();
272
273
            $isActiveSubForm = $subFormName === $selectedSubFormName;
274
275
            $form->add($subFormName, $subFormType, array_merge(
276
                $existingFieldOptions,
277
                [
278
                    'inherit_data' => $isActiveSubForm,
279
                    'validation_groups' => $isActiveSubForm ? null : static::VALIDATION_GROUP_DISABLED,
280
                ]
281
            ));
282
        }
283
    }
284
285
    /**
286
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
287
     *
288
     * @return $this
289
     */
290
    protected function addAddMappingField(FormBuilderInterface $builder)
291
    {
292
        $builder->add(PunchoutCatalogConnectionTransfer::MAPPING, TextareaType::class, [
293
            'label' => 'Request Mapping',
294
            'required' => false,
295
        ]);
296
297
        return $this;
298
    }
299
300
    /**
301
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
302
     * @param array $options
303
     *
304
     * @return $this
305
     */
306
    protected function addBusinessUnitField(FormBuilderInterface $builder, array $options)
307
    {
308
        $builder->add(
309
            PunchoutCatalogConnectionTransfer::FK_COMPANY_BUSINESS_UNIT,
310
            SelectType::class,
311
            [
312
                'choices' => $options[static::OPTION_BUSINESS_UNIT_CHOICES],
313
                'placeholder' => 'Choose business unit',
314
                'label' => 'Business Unit',
315
                'constraints' => [
316
                    new NotBlank(),
317
                ],
318
            ]
319
        );
320
321
        return $this;
322
    }
323
}
324