Passed
Pull Request — master (#74)
by oleksandr
04:26
created

DirectDebitSubForm::getTemplatePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Yves\Payone\Form;
9
10
use Generated\Shared\Transfer\PaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaymentTransfer 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\PayonePaymentDirectDebitTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...mentDirectDebitTransfer 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\Shared\Kernel\Store;
13
use Spryker\Yves\StepEngine\Dependency\Form\SubFormInterface;
14
use SprykerEco\Yves\Payone\Form\Constraint\ManageMandate;
15
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
16
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
17
use Symfony\Component\Form\Extension\Core\Type\TextType;
18
use Symfony\Component\Form\FormBuilderInterface;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
use Symfony\Component\Validator\Constraint;
21
use Symfony\Component\Validator\Constraints\NotBlank;
22
23
/**
24
 * @method \SprykerEco\Yves\Payone\PayoneConfig getConfig()
25
 */
26
class DirectDebitSubForm extends AbstractPayoneSubForm
27
{
28
    /**
29
     * @var string
30
     */
31
    public const PAYMENT_METHOD = 'direct_debit';
32
33
    /**
34
     * @var string
35
     */
36
    public const FIELD_IBAN = 'iban';
37
38
    /**
39
     * @var string
40
     */
41
    public const FIELD_BIC = 'bic';
42
43
    /**
44
     * @var string
45
     */
46
    public const FIELD_BANK_COUNTRY = 'bankcountry';
47
48
    /**
49
     * @var string
50
     */
51
    public const FIELD_BANK_ACCOUNT = 'bankaccount';
52
53
    /**
54
     * @var string
55
     */
56
    public const FIELD_BANK_ACCOUNT_MODE = 'bankaccountmode';
57
58
    /**
59
     * @var string
60
     */
61
    public const FIELD_BANK_CODE = 'bankcode';
62
63
    /**
64
     * @var string
65
     */
66
    public const OPTION_BANK_COUNTRIES = 'direct debit bank countries';
67
68
    /**
69
     * @var string
70
     */
71
    public const OPTION_BANK_ACCOUNT_MODE = 'direct debit bank account mode';
72
73
    /**
74
     * @return string
75
     */
76
    public function getName(): string
77
    {
78
        return PaymentTransfer::PAYONE_DIRECT_DEBIT;
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getPropertyPath(): string
85
    {
86
        return PaymentTransfer::PAYONE_DIRECT_DEBIT;
87
    }
88
89
    /**
90
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
91
     *
92
     * @return void
93
     */
94
    public function configureOptions(OptionsResolver $resolver): void
95
    {
96
        $resolver->setDefaults([
97
            'data_class' => PayonePaymentDirectDebitTransfer::class,
98
        ])->setRequired(SubFormInterface::OPTIONS_FIELD_NAME);
99
    }
100
101
    /**
102
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
103
     *
104
     * @return void
105
     */
106
    public function setDefaultOptions(OptionsResolver $resolver): void
107
    {
108
        $this->configureOptions($resolver);
109
    }
110
111
    /**
112
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
113
     * @param array $options
114
     *
115
     * @return void
116
     */
117
    public function buildForm(FormBuilderInterface $builder, array $options): void
118
    {
119
        $this->addIBAN($builder)
120
            ->addBIC($builder);
121
122
        if (Store::getInstance()->getCurrentCountry() === 'DE') {
123
            $this->addBankAccount($builder)
124
                ->addBankCode($builder)
125
                ->addBankCountry($builder, $options)
126
                ->addModeSwitch($builder, $options);
127
        }
128
    }
129
130
    /**
131
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
132
     * @param array $options
133
     *
134
     * @return $this
135
     */
136
    protected function addModeSwitch(FormBuilderInterface $builder, array $options)
137
    {
138
        $builder->add(
139
            static::FIELD_BANK_ACCOUNT_MODE,
140
            ChoiceType::class,
141
            [
142
                'label' => false,
143
                'required' => true,
144
                'expanded' => true,
145
                'multiple' => false,
146
                'placeholder' => false,
147
                'choices' => $options[static::OPTIONS_FIELD_NAME][static::OPTION_BANK_ACCOUNT_MODE],
148
                'constraints' => [
149
                ],
150
            ],
151
        );
152
153
        return $this;
154
    }
155
156
    /**
157
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
158
     *
159
     * @return $this
160
     */
161
    protected function addBankAccount(FormBuilderInterface $builder)
162
    {
163
        $builder->add(
164
            static::FIELD_BANK_ACCOUNT,
165
            TextType::class,
166
            [
167
                'label' => false,
168
                'required' => true,
169
                'constraints' => [
170
                ],
171
            ],
172
        );
173
174
        return $this;
175
    }
176
177
    /**
178
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
179
     *
180
     * @return $this
181
     */
182
    protected function addBankCode(FormBuilderInterface $builder)
183
    {
184
        $builder->add(
185
            static::FIELD_BANK_CODE,
186
            TextType::class,
187
            [
188
                'label' => false,
189
                'required' => true,
190
                'constraints' => [
191
                ],
192
            ],
193
        );
194
195
        return $this;
196
    }
197
198
    /**
199
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
200
     * @param array $options
201
     *
202
     * @return $this
203
     */
204
    protected function addBankCountry(FormBuilderInterface $builder, array $options)
205
    {
206
        if (count($options[static::OPTIONS_FIELD_NAME][static::OPTION_BANK_COUNTRIES]) == 1) {
207
            $builder->add(
208
                static::FIELD_BANK_COUNTRY,
209
                HiddenType::class,
210
                [
211
                    'label' => false,
212
                    'data' => array_keys($options[static::OPTIONS_FIELD_NAME][static::OPTION_BANK_COUNTRIES])[0],
213
                ],
214
            );
215
        } else {
216
            $builder->add(
217
                static::FIELD_BANK_COUNTRY,
218
                ChoiceType::class,
219
                [
220
                    'label' => false,
221
                    'required' => true,
222
                    'expanded' => false,
223
                    'multiple' => false,
224
                    'placeholder' => false,
225
                    'choices' => $options[static::OPTIONS_FIELD_NAME][static::OPTION_BANK_COUNTRIES],
226
                    'constraints' => [
227
                    ],
228
                ],
229
            );
230
        }
231
232
        return $this;
233
    }
234
235
    /**
236
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
237
     *
238
     * @return $this
239
     */
240
    protected function addIBAN(FormBuilderInterface $builder)
241
    {
242
        $builder->add(
243
            static::FIELD_IBAN,
244
            TextType::class,
245
            [
246
                'label' => false,
247
                'required' => true,
248
                'constraints' => [
249
                ],
250
            ],
251
        );
252
253
        return $this;
254
    }
255
256
    /**
257
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
258
     *
259
     * @return $this
260
     */
261
    protected function addBIC(FormBuilderInterface $builder)
262
    {
263
        $builder->add(
264
            static::FIELD_BIC,
265
            TextType::class,
266
            [
267
                'label' => false,
268
                'required' => true,
269
                'constraints' => [
270
                    $this->createManageMandateConstraint(),
271
                ],
272
            ],
273
        );
274
275
        return $this;
276
    }
277
278
    /**
279
     * @return \Symfony\Component\Validator\Constraint
280
     */
281
    protected function createNotBlankConstraint(): Constraint
282
    {
283
        return new NotBlank(['groups' => $this->getPropertyPath()]);
284
    }
285
286
    /**
287
     * @return \Symfony\Component\Validator\Constraint
288
     */
289
    protected function createManageMandateConstraint(): Constraint
290
    {
291
        return new ManageMandate(['payoneClient' => $this->getClient(), 'groups' => $this->getPropertyPath()]);
292
    }
293
}
294