Completed
Push — master ( a42644...fb7144 )
by Oleksandr
10s
created

OnlineTransferSubForm::getTemplatePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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;
11
use Generated\Shared\Transfer\PayoneBankAccountCheckTransfer;
12
use Generated\Shared\Transfer\PayonePaymentOnlinetransferTransfer;
13
use Spryker\Yves\StepEngine\Dependency\Form\SubFormInterface;
14
use SprykerEco\Shared\Payone\PayoneConstants;
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\Context\ExecutionContextInterface;
21
22 View Code Duplication
abstract class OnlineTransferSubForm extends AbstractPayoneSubForm
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
{
24
    const PAYMENT_METHOD = 'online_transfer';
25
    const FIELD_IBAN = 'iban';
26
    const FIELD_BIC = 'bic';
27
    const FIELD_BANK_COUNTRY = 'bankcountry';
28
    const FIELD_BANK_ACCOUNT = 'bankaccount';
29
    const FIELD_BANK_CODE = 'bankcode';
30
    const FIELD_BANK_BRANCH_CODE = 'bankbranchcode';
31
    const FIELD_BANK_CHECK_DIGIT = 'bankcheckdigit';
32
    const FIELD_ONLINE_BANK_TRANSFER_TYPE = 'onlinebanktransfertype';
33
    const FIELD_BANK_GROUP_TYPE = 'bankgrouptype';
34
    const OPTION_BANK_GROUP_TYPES = 'online bank transfer types';
35
    const OPTION_BANK_COUNTRIES = '';
36
37
    /**
38
     * @return string
39
     */
40
    public function getName()
41
    {
42
        return PaymentTransfer::PAYONE_ONLINE_TRANSFER;
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function getTemplatePath()
49
    {
50
        return PayoneConstants::PROVIDER_NAME . '/' . static::PAYMENT_METHOD;
51
    }
52
53
    /**
54
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
55
     *
56
     * @return void
57
     */
58
    public function configureOptions(OptionsResolver $resolver)
59
    {
60
        $resolver->setDefaults([
61
            'data_class' => PayonePaymentOnlinetransferTransfer::class,
62
            'constraints' => [
63
                // Add Callback constraint for bank account check in ancestor classes
64
                // new Callback(['methods' => [[$this, 'checkBankAccount']]])
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
65
            ],
66
        ])->setRequired(SubFormInterface::OPTIONS_FIELD_NAME);
67
    }
68
69
    /**
70
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
71
     *
72
     * @return void
73
     */
74
    public function setDefaultOptions(OptionsResolver $resolver)
75
    {
76
        $this->configureOptions($resolver);
77
    }
78
79
    /**
80
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
81
     * @param array $options
82
     *
83
     * @return void
84
     */
85
    public function buildForm(FormBuilderInterface $builder, array $options)
86
    {
87
        $this->addOnlineBankTransferType($builder, $options)
88
            ->addBankCountry($builder, $options);
89
    }
90
91
    /**
92
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
93
     *
94
     * @return \SprykerEco\Yves\Payone\Form\OnlineTransferSubForm
95
     */
96
    protected function addBankAccount(FormBuilderInterface $builder)
97
    {
98
        $builder->add(
99
            static::FIELD_BANK_ACCOUNT,
100
            TextType::class,
101
            [
102
                'label' => false,
103
                'required' => true,
104
                'constraints' => [
105
                ],
106
            ]
107
        );
108
109
        return $this;
110
    }
111
112
    /**
113
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
114
     *
115
     * @return \SprykerEco\Yves\Payone\Form\OnlineTransferSubForm
116
     */
117
    protected function addBankCode(FormBuilderInterface $builder)
118
    {
119
        $builder->add(
120
            static::FIELD_BANK_CODE,
121
            TextType::class,
122
            [
123
                'label' => false,
124
                'required' => true,
125
                'constraints' => [
126
                ],
127
            ]
128
        );
129
130
        return $this;
131
    }
132
133
    /**
134
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
135
     *
136
     * @return \SprykerEco\Yves\Payone\Form\OnlineTransferSubForm
137
     */
138
    protected function addBankBranchCode(FormBuilderInterface $builder)
139
    {
140
        $builder->add(
141
            static::FIELD_BANK_BRANCH_CODE,
142
            TextType::class,
143
            [
144
                'label' => false,
145
                'required' => true,
146
                'constraints' => [
147
                ],
148
            ]
149
        );
150
151
        return $this;
152
    }
153
154
    /**
155
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
156
     *
157
     * @return \SprykerEco\Yves\Payone\Form\OnlineTransferSubForm
158
     */
159
    protected function addBankCheckDigit(FormBuilderInterface $builder)
160
    {
161
        $builder->add(
162
            static::FIELD_BANK_CHECK_DIGIT,
163
            TextType::class,
164
            [
165
                'label' => false,
166
                'required' => true,
167
                'constraints' => [
168
                ],
169
            ]
170
        );
171
172
        return $this;
173
    }
174
175
    /**
176
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
177
     *
178
     * @return \SprykerEco\Yves\Payone\Form\OnlineTransferSubForm
179
     */
180
    protected function addIban(FormBuilderInterface $builder)
181
    {
182
        $builder->add(
183
            static::FIELD_IBAN,
184
            TextType::class,
185
            [
186
                'label' => false,
187
                'required' => true,
188
                'constraints' => [
189
                ],
190
            ]
191
        );
192
193
        return $this;
194
    }
195
196
    /**
197
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
198
     * @param array $options
199
     *
200
     * @return \SprykerEco\Yves\Payone\Form\OnlineTransferSubForm
201
     */
202
    protected function addBankCountry(FormBuilderInterface $builder, array $options)
203
    {
204
        if (count($options[static::OPTIONS_FIELD_NAME][static::OPTION_BANK_COUNTRIES]) == 1) {
205
            $builder->add(
206
                static::FIELD_BANK_COUNTRY,
207
                HiddenType::class,
208
                [
209
                    'label' => false,
210
                    'data' => array_keys($options[static::OPTIONS_FIELD_NAME][static::OPTION_BANK_COUNTRIES])[0],
211
                ]
212
            );
213
        } else {
214
            $builder->add(
215
                static::FIELD_BANK_COUNTRY,
216
                ChoiceType::class,
217
                [
218
                    'label' => false,
219
                    'required' => true,
220
                    'expanded' => false,
221
                    'multiple' => false,
222
                    'placeholder' => false,
223
                    'choices' => $options[static::OPTIONS_FIELD_NAME][static::OPTION_BANK_COUNTRIES],
224
                    'constraints' => [
225
                    ],
226
                ]
227
            );
228
        }
229
230
        return $this;
231
    }
232
233
    /**
234
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
235
     *
236
     * @return \SprykerEco\Yves\Payone\Form\OnlineTransferSubForm
237
     */
238
    protected function addBic(FormBuilderInterface $builder)
239
    {
240
        $builder->add(
241
            static::FIELD_BIC,
242
            TextType::class,
243
            [
244
                'label' => false,
245
                'required' => true,
246
                'constraints' => [
247
                ],
248
            ]
249
        );
250
251
        return $this;
252
    }
253
254
    /**
255
     * @param \Generated\Shared\Transfer\PayonePaymentOnlinetransferTransfer $data
256
     * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
257
     *
258
     * @return void
259
     */
260
    public function checkBankAccount(PayonePaymentOnlinetransferTransfer $data, ExecutionContextInterface $context)
261
    {
262
        $quoteTransfer = $context->getRoot()->getData();
263
        if ($quoteTransfer->getPayment()->getPaymentSelection() != $this->getPropertyPath()) {
264
            return;
265
        }
266
267
        $bankAccountCheckTransfer = new PayoneBankAccountCheckTransfer();
268
        $bankAccountCheckTransfer->setBankCountry($data->getBankcountry());
269
        $bankAccountCheckTransfer->setBankAccount($data->getBankaccount());
270
        $bankAccountCheckTransfer->setBankCode($data->getBankcode());
271
        $bankAccountCheckTransfer->setBankBranchCode($data->getBankbranchcode());
272
        $bankAccountCheckTransfer->setBankCheckDigit($data->getBankcheckdigit());
273
        $bankAccountCheckTransfer->setIban($data->getIban());
274
        $bankAccountCheckTransfer->setBic($data->getBic());
275
276
        $response = $this->getClient()->bankAccountCheck($bankAccountCheckTransfer);
277
        if ($response->getStatus() == 'ERROR' || $response->getStatus() == 'INVALID') {
278
            $context->addViolation($response->getCustomerErrorMessage());
279
        }
280
    }
281
282
    /**
283
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
284
     * @param array $options
285
     *
286
     * @return \SprykerEco\Yves\Payone\Form\OnlineTransferSubForm
287
     */
288
    abstract public function addOnlineBankTransferType(FormBuilderInterface $builder, array $options);
289
}
290