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

AbstractOnlineTransferSubForm   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 264
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 17
lcom 0
cbo 4
dl 264
loc 264
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 4 4 1
A getTemplatePath() 4 4 1
A configureOptions() 6 6 1
A setDefaultOptions() 4 4 1
A buildForm() 5 5 1
A addBankAccount() 15 15 1
A addBankCode() 15 15 1
A addBankBranchCode() 15 15 1
A addBankCheckDigit() 15 15 1
A addIBAN() 15 15 1
B addBankCountry() 30 30 2
A addBIC() 15 15 1
A checkBankAccount() 21 21 4
addOnlineBankTransferType() 1 1 ?

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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