configureOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 4
rs 10
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\ConnectionSetupSubForms;
9
10
use Closure;
11
use Generated\Shared\Transfer\PunchoutCatalogConnectionSetupTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ConnectionSetupTransfer 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...
12
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...
13
use Spryker\Zed\Gui\Communication\Form\Type\SelectType;
14
use Spryker\Zed\Kernel\Communication\Form\AbstractType;
15
use Symfony\Component\Form\CallbackTransformer;
16
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
17
use Symfony\Component\Form\FormBuilderInterface;
18
use Symfony\Component\Form\FormEvent;
19
use Symfony\Component\Form\FormEvents;
20
use Symfony\Component\Form\FormInterface;
21
use Symfony\Component\OptionsResolver\OptionsResolver;
22
use Symfony\Component\Validator\Constraints\NotBlank;
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 PunchoutCatalogConnectionSetupForm extends AbstractType
31
{
32
    protected const TOGGLE_GROUP_LOGIN_MODE = 'login-mode';
33
34
    protected const LOGIN_MODE_SINGLE_USER = 'single_user';
35
    protected const LOGIN_MODE_DYNAMIC_USER_CREATION = 'dynamic_user_creation';
36
37
    /**
38
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
39
     * @param array $options
40
     *
41
     * @return void
42
     */
43
    public function buildForm(FormBuilderInterface $builder, array $options): void
44
    {
45
        $this->addLoginModeField($builder)
46
            ->addCompanyBusinessUnitField($builder)
47
            ->addCompanyBusinessUnitFieldListeners($builder)
48
            ->addCompanyUserField($builder)
49
            ->addCompanyUserFieldListeners($builder);
50
    }
51
52
    /**
53
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
54
     *
55
     * @return void
56
     */
57
    public function configureOptions(OptionsResolver $resolver): void
58
    {
59
        $resolver->setDefaults([
60
            'data_class' => PunchoutCatalogConnectionSetupTransfer::class,
61
        ]);
62
    }
63
64
    /**
65
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
66
     *
67
     * @return $this
68
     */
69
    protected function addLoginModeField(FormBuilderInterface $builder)
70
    {
71
        $builder->add(PunchoutCatalogConnectionSetupTransfer::LOGIN_MODE, ChoiceType::class, [
72
            'label' => 'Login Mode',
73
            'choices' => [
74
                'Single User' => static::LOGIN_MODE_SINGLE_USER,
75
                'Dynamic User Creation' => static::LOGIN_MODE_DYNAMIC_USER_CREATION,
76
            ],
77
            'attr' => [
78
                'class' => 'toggle-trigger',
79
                'data-toggle-group' => static::TOGGLE_GROUP_LOGIN_MODE,
80
            ],
81
            'constraints' => [
82
                new NotBlank(),
83
            ],
84
        ]);
85
86
        $builder->addModelTransformer($this->createLoginModeDependentFieldsModelTransformer());
87
88
        return $this;
89
    }
90
91
    /**
92
     * @return \Symfony\Component\Form\CallbackTransformer
93
     */
94
    protected function createLoginModeDependentFieldsModelTransformer(): CallbackTransformer
95
    {
96
        return new CallbackTransformer(
97
            function (?PunchoutCatalogConnectionSetupTransfer $punchoutCatalogConnectionSetupTransfer = null) {
98
                if (!$punchoutCatalogConnectionSetupTransfer) {
99
                    return null;
100
                }
101
102
                if ($punchoutCatalogConnectionSetupTransfer->getLoginMode() === static::LOGIN_MODE_SINGLE_USER) {
103
                    $punchoutCatalogConnectionSetupTransfer->setFkCompanyBusinessUnit(null);
104
105
                    return $punchoutCatalogConnectionSetupTransfer;
106
                }
107
108
                $punchoutCatalogConnectionSetupTransfer->setFkCompanyUser(null);
109
110
                return $punchoutCatalogConnectionSetupTransfer;
111
            },
112
            function (PunchoutCatalogConnectionSetupTransfer $punchoutCatalogConnectionSetupTransfer) {
113
                return $punchoutCatalogConnectionSetupTransfer;
114
            }
115
        );
116
    }
117
118
    /**
119
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
120
     *
121
     * @return $this
122
     */
123
    protected function addCompanyBusinessUnitField(FormBuilderInterface $builder)
124
    {
125
        $builder->add(PunchoutCatalogConnectionSetupTransfer::FK_COMPANY_BUSINESS_UNIT, SelectType::class, [
126
            'label' => 'Default Business Unit',
127
            'attr' => [
128
                'class' => 'toggle-inner-item',
129
                'data-toggle-group' => static::TOGGLE_GROUP_LOGIN_MODE,
130
                'data-toggle-type' => static::LOGIN_MODE_DYNAMIC_USER_CREATION,
131
            ],
132
        ]);
133
134
        return $this;
135
    }
136
137
    /**
138
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
139
     *
140
     * @return $this
141
     */
142
    protected function addCompanyUserField(FormBuilderInterface $builder)
143
    {
144
        $builder->add(PunchoutCatalogConnectionSetupTransfer::FK_COMPANY_USER, SelectType::class, [
145
            'label' => 'Single User',
146
            'attr' => [
147
                'class' => 'toggle-inner-item',
148
                'data-toggle-group' => static::TOGGLE_GROUP_LOGIN_MODE,
149
                'data-toggle-type' => static::LOGIN_MODE_SINGLE_USER,
150
            ],
151
        ]);
152
153
        return $this;
154
    }
155
156
    /**
157
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
158
     *
159
     * @return $this
160
     */
161
    protected function addCompanyBusinessUnitFieldListeners(FormBuilderInterface $builder)
162
    {
163
        $builder->addEventListener(FormEvents::PRE_SUBMIT, $this->createCompanyBusinessUnitFormEventListener());
164
        $builder->addEventListener(FormEvents::PRE_SET_DATA, $this->createCompanyBusinessUnitFormEventListener());
165
166
        return $this;
167
    }
168
169
    /**
170
     * @return \Closure
171
     */
172
    protected function createCompanyBusinessUnitFormEventListener(): Closure
173
    {
174
        return function (FormEvent $event) {
175
            $form = $event->getForm();
176
177
            $parentCompanyBusinessUnitId = $form
178
                ->getParent()
179
                ->getParent()
180
                ->get(PunchoutCatalogConnectionTransfer::FK_COMPANY_BUSINESS_UNIT)
181
                ->getData();
182
183
            if (!$parentCompanyBusinessUnitId) {
184
                return;
185
            }
186
187
            $this->updateCompanyBusinessUnitFieldChoices($form, $parentCompanyBusinessUnitId);
188
        };
189
    }
190
191
    /**
192
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
193
     *
194
     * @return $this
195
     */
196
    protected function addCompanyUserFieldListeners(FormBuilderInterface $builder)
197
    {
198
        $companyUserEventListenerCallback = $this->createCompanyUserFormEventListener();
199
200
        $builder->addEventListener(FormEvents::PRE_SUBMIT, $companyUserEventListenerCallback);
201
        $builder->addEventListener(FormEvents::PRE_SET_DATA, $companyUserEventListenerCallback);
202
203
        return $this;
204
    }
205
206
    /**
207
     * @return \Closure
208
     */
209
    protected function createCompanyUserFormEventListener(): Closure
210
    {
211
        return function (FormEvent $event) {
212
            $form = $event->getForm();
213
214
            $parentCompanyBusinessUnitId = $form
215
                ->getParent()
216
                ->getParent()
217
                ->get(PunchoutCatalogConnectionTransfer::FK_COMPANY_BUSINESS_UNIT)
218
                ->getData();
219
220
            if (!$parentCompanyBusinessUnitId) {
221
                return;
222
            }
223
224
            $this->updateCompanyUserFieldChoices($form, $parentCompanyBusinessUnitId);
225
        };
226
    }
227
228
    /**
229
     * @param \Symfony\Component\Form\FormInterface $form
230
     * @param int $parentCompanyBusinessUnitId
231
     *
232
     * @return void
233
     */
234
    protected function updateCompanyBusinessUnitFieldChoices(FormInterface $form, int $parentCompanyBusinessUnitId): void
235
    {
236
        $companyBusinessUnitChoices = $this->getFactory()
237
            ->createPunchoutCatalogSetupRequestConnectionTypeFormDataProvider()
238
            ->getCompanyBusinessUnitChoices($parentCompanyBusinessUnitId);
239
240
        $existingOptions = $form->get(PunchoutCatalogConnectionSetupTransfer::FK_COMPANY_BUSINESS_UNIT)
241
            ->getConfig()
242
            ->getOptions();
243
244
        $form->add(PunchoutCatalogConnectionSetupTransfer::FK_COMPANY_BUSINESS_UNIT, SelectType::class, array_merge(
245
            $existingOptions,
246
            [
247
                'choices' => $companyBusinessUnitChoices,
248
            ]
249
        ));
250
    }
251
252
    /**
253
     * @param \Symfony\Component\Form\FormInterface $form
254
     * @param int $parentCompanyBusinessUnitId
255
     *
256
     * @return void
257
     */
258
    protected function updateCompanyUserFieldChoices(FormInterface $form, int $parentCompanyBusinessUnitId): void
259
    {
260
        $companyUserChoices = $this->getFactory()
261
            ->createPunchoutCatalogSetupRequestConnectionTypeFormDataProvider()
262
            ->getCompanyUserChoices($parentCompanyBusinessUnitId);
263
264
        $existingOptions = $form->get(PunchoutCatalogConnectionSetupTransfer::FK_COMPANY_USER)
265
            ->getConfig()
266
            ->getOptions();
267
268
        $form->add(PunchoutCatalogConnectionSetupTransfer::FK_COMPANY_USER, SelectType::class, array_merge(
269
            $existingOptions,
270
            [
271
                'choices' => $companyUserChoices,
272
            ]
273
        ));
274
    }
275
}
276