addEncodingField()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 9
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 15
rs 9.9666
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 Generated\Shared\Transfer\PunchoutCatalogConnectionCartTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...gConnectionCartTransfer 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 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...
12
use Spryker\Zed\Kernel\Communication\Form\AbstractType;
13
use Symfony\Component\Form\CallbackTransformer;
14
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
15
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
16
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
17
use Symfony\Component\Form\Extension\Core\Type\TextType;
18
use Symfony\Component\Form\FormBuilderInterface;
19
use Symfony\Component\Form\FormInterface;
20
use Symfony\Component\Form\FormView;
21
use Symfony\Component\OptionsResolver\OptionsResolver;
22
use Symfony\Component\Validator\Constraint;
23
use Symfony\Component\Validator\Constraints\Callback;
24
use Symfony\Component\Validator\Constraints\Length;
25
use Symfony\Component\Validator\Constraints\NotBlank;
26
use Symfony\Component\Validator\Constraints\Range;
27
use Symfony\Component\Validator\Context\ExecutionContextInterface;
28
29
/**
30
 * @method \SprykerEco\Zed\PunchoutCatalogs\PunchoutCatalogsConfig getConfig()
31
 * @method \SprykerEco\Zed\PunchoutCatalogs\Persistence\PunchoutCatalogsRepositoryInterface getRepository()
32
 * @method \SprykerEco\Zed\PunchoutCatalogs\Business\PunchoutCatalogsFacadeInterface getFacade()
33
 * @method \SprykerEco\Zed\PunchoutCatalogs\Communication\PunchoutCatalogsCommunicationFactory getFactory()
34
 */
35
class PunchoutCatalogConnectionCartForm extends AbstractType
36
{
37
    protected const MIN_DESCRIPTION_LENGTH = 16;
38
    protected const MAX_DESCRIPTION_LENGTH = 99999;
39
40
    protected const MESSAGE_PARAM_TOTALS_MODE = '%totals_mode%';
41
    protected const MESSAGE_PARAM_CONNECTION_FORMAT = '%connection_format%';
42
43
    protected const MESSAGE_INCOMPATIBLE_TOTALS_MODE_AND_CONNECTION_FORMAT = 'You can’t use "%s" for "%s" connection - please choose another one.';
44
45
    protected const TEMPLATE_PATH_MAX_DESCRIPTION_LENGTH_FIELD = '@PunchoutCatalogs/Form/Connection/Setup/max_description_length.twig';
46
47
    protected const ALLOWED_CONNECTION_FORMATS_FOR_TOTALS_MODES = [
48
        'line' => ['cxml', 'oci'],
49
        'header' => ['cxml'],
50
    ];
51
52
    /**
53
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
54
     * @param array $options
55
     *
56
     * @return void
57
     */
58
    public function buildForm(FormBuilderInterface $builder, array $options): void
59
    {
60
        $this->addMaxDescriptionLengthField($builder)
61
            ->addEncodingField($builder)
62
            ->addTotalsModeField($builder)
63
            ->addMappingField($builder)
64
            ->addMaxDescriptionLengthField($builder)
65
            ->addDefaultSupplierIdField($builder);
66
    }
67
68
    /**
69
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
70
     *
71
     * @return void
72
     */
73
    public function configureOptions(OptionsResolver $resolver): void
74
    {
75
        $resolver->setDefaults([
76
            'data_class' => PunchoutCatalogConnectionCartTransfer::class,
77
        ]);
78
    }
79
80
    /**
81
     * @param \Symfony\Component\Form\FormView $view
82
     * @param \Symfony\Component\Form\FormInterface $form
83
     * @param array $options
84
     *
85
     * @return void
86
     */
87
    public function buildView(FormView $view, FormInterface $form, array $options)
88
    {
89
        $view->vars['min_description_length'] = static::MIN_DESCRIPTION_LENGTH;
90
        $view->vars['max_description_length'] = static::MAX_DESCRIPTION_LENGTH;
91
    }
92
93
    /**
94
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
95
     *
96
     * @return $this
97
     */
98
    protected function addMaxDescriptionLengthField(FormBuilderInterface $builder)
99
    {
100
        $builder->add(PunchoutCatalogConnectionCartTransfer::MAX_DESCRIPTION_LENGTH, IntegerType::class, [
101
            'label' => 'Set Description length on "Transfer to Requisition"',
102
            'required' => false,
103
            'constraints' => [
104
                new Range([
105
                    'min' => static::MIN_DESCRIPTION_LENGTH,
106
                    'max' => static::MAX_DESCRIPTION_LENGTH,
107
                ]),
108
            ],
109
            'attr' => [
110
                'template_path' => static::TEMPLATE_PATH_MAX_DESCRIPTION_LENGTH_FIELD,
111
            ],
112
        ]);
113
114
        $builder->get(PunchoutCatalogConnectionCartTransfer::MAX_DESCRIPTION_LENGTH)
115
            ->addViewTransformer($this->createMaxDescriptionLengthViewTransformer());
116
117
        return $this;
118
    }
119
120
    /**
121
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
122
     *
123
     * @return $this
124
     */
125
    protected function addTotalsModeField(FormBuilderInterface $builder)
126
    {
127
        $builder->add(PunchoutCatalogConnectionCartTransfer::TOTALS_MODE, ChoiceType::class, [
128
            'label' => 'Totals Mode',
129
            'choices' => [
130
                'Line' => 'line',
131
                'Header (does not work with OCI)' => 'header',
132
            ],
133
            'constraints' => [
134
                $this->createTotalsModeValidationConstraint(),
135
            ],
136
        ]);
137
138
        return $this;
139
    }
140
141
    /**
142
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
143
     *
144
     * @return $this
145
     */
146
    protected function addEncodingField(FormBuilderInterface $builder)
147
    {
148
        $choices = $this->getFactory()
149
            ->createPunchoutCatalogSetupRequestConnectionTypeFormDataProvider()
150
            ->getCartEncodingChoices();
151
152
        $builder->add(PunchoutCatalogConnectionCartTransfer::ENCODING, ChoiceType::class, [
153
            'label' => 'Cart Encoding',
154
            'choices' => $choices,
155
            'constraints' => [
156
                new NotBlank(),
157
            ],
158
        ]);
159
160
        return $this;
161
    }
162
163
    /**
164
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
165
     *
166
     * @return $this
167
     */
168
    protected function addMappingField(FormBuilderInterface $builder)
169
    {
170
        $builder->add(PunchoutCatalogConnectionCartTransfer::MAPPING, TextareaType::class, [
171
            'label' => 'Cart Mapping',
172
            'required' => false,
173
        ]);
174
175
        return $this;
176
    }
177
178
    /**
179
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
180
     *
181
     * @return $this
182
     */
183
    protected function addDefaultSupplierIdField(FormBuilderInterface $builder)
184
    {
185
        $builder->add(PunchoutCatalogConnectionCartTransfer::DEFAULT_SUPPLIER_ID, TextType::class, [
186
            'label' => 'Default Supplier ID',
187
            'constraints' => [
188
                new NotBlank(),
189
                new Length(['max' => 64]),
190
            ],
191
        ]);
192
193
        return $this;
194
    }
195
196
    /**
197
     * @return \Symfony\Component\Form\CallbackTransformer
198
     */
199
    protected function createMaxDescriptionLengthViewTransformer(): CallbackTransformer
200
    {
201
        return new CallbackTransformer(
202
            function (string $maxDescriptionLength) {
203
                return $maxDescriptionLength;
204
            },
205
            function (string $maxDescriptionLength) {
206
                if (!$maxDescriptionLength) {
207
                    return (string)static::MAX_DESCRIPTION_LENGTH;
208
                }
209
210
                return $maxDescriptionLength;
211
            }
212
        );
213
    }
214
215
    /**
216
     * @return \Symfony\Component\Validator\Constraint
217
     */
218
    protected function createTotalsModeValidationConstraint(): Constraint
219
    {
220
        return new Callback([
221
            'callback' => function (string $totalsMode, ExecutionContextInterface $context) {
222
                $connectionFormat = $context->getRoot()
223
                     ->get(PunchoutCatalogConnectionTransfer::FORMAT)
224
                     ->getData();
225
226
                $allowedConnectionFormats = static::ALLOWED_CONNECTION_FORMATS_FOR_TOTALS_MODES[$totalsMode] ?? [];
227
228
                if (!in_array($connectionFormat, $allowedConnectionFormats)) {
229
                    $context->addViolation(
230
                        sprintf(
231
                            static::MESSAGE_INCOMPATIBLE_TOTALS_MODE_AND_CONNECTION_FORMAT,
232
                            static::MESSAGE_PARAM_TOTALS_MODE,
233
                            static::MESSAGE_PARAM_CONNECTION_FORMAT
234
                        ),
235
                        [
236
                            static::MESSAGE_PARAM_TOTALS_MODE => $totalsMode,
237
                            static::MESSAGE_PARAM_CONNECTION_FORMAT => $connectionFormat,
238
                        ]
239
                    );
240
                }
241
            },
242
        ]);
243
    }
244
}
245