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

CreditCardSubForm   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 235
Duplicated Lines 11.06 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 26
loc 235
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A getPropertyPath() 0 4 1
A getTemplatePath() 0 4 1
A configureOptions() 0 6 1
A setDefaultOptions() 0 4 1
A buildForm() 0 6 1
A addCardType() 0 20 1
A addCardNumber() 13 13 1
A addNameOnCard() 0 16 1
A addCardExpiresMonth() 0 17 1
A addCardExpiresYear() 0 20 1
A addCardSecurityCode() 13 13 1
A addHiddenInputs() 0 14 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\PayonePaymentCreditCardTransfer;
12
use Spryker\Yves\StepEngine\Dependency\Form\SubFormInterface;
13
use SprykerEco\Shared\Payone\PayoneConstants;
14
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
15
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
16
use Symfony\Component\Form\Extension\Core\Type\TextType;
17
use Symfony\Component\Form\FormBuilderInterface;
18
use Symfony\Component\OptionsResolver\OptionsResolver;
19
20
class CreditCardSubForm extends AbstractPayoneSubForm
21
{
22
    const PAYMENT_METHOD = 'credit_card';
23
24
    const FIELD_CARD_TYPE = 'cardtype';
25
    const FIELD_CARD_NUMBER = 'cardpan';
26
    const FIELD_NAME_ON_CARD = 'cardholder';
27
    const FIELD_CARD_EXPIRES_MONTH = 'cardexpiredate_month';
28
    const FIELD_CARD_EXPIRES_YEAR = 'cardexpiredate_year';
29
    const FIELD_CARD_SECURITY_CODE = 'cardcvc2';
30
    const FIELD_PSEUDO_CARD_NUMBER = 'pseudocardpan';
31
32
    const OPTION_CARD_EXPIRES_CHOICES_MONTH = 'month choices';
33
    const OPTION_CARD_EXPIRES_CHOICES_YEAR = 'year choices';
34
    const OPTION_CARD_TYPES = 'card types';
35
36
    const OPTION_PAYONE_SETTINGS = 'payone settings';
37
38
    /**
39
     * @return string
40
     */
41
    public function getName()
42
    {
43
        return PaymentTransfer::PAYONE_CREDIT_CARD;
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function getPropertyPath()
50
    {
51
        return PaymentTransfer::PAYONE_CREDIT_CARD;
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getTemplatePath()
58
    {
59
        return PayoneConstants::PROVIDER_NAME . '/' . self::PAYMENT_METHOD;
60
    }
61
62
    /**
63
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
64
     *
65
     * @return void
66
     */
67
    public function configureOptions(OptionsResolver $resolver)
68
    {
69
        $resolver->setDefaults([
70
            'data_class' => PayonePaymentCreditCardTransfer::class,
71
        ])->setRequired(SubFormInterface::OPTIONS_FIELD_NAME);
72
    }
73
74
    /**
75
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
76
     *
77
     * @return void
78
     */
79
    public function setDefaultOptions(OptionsResolver $resolver)
80
    {
81
        $this->configureOptions($resolver);
82
    }
83
84
    /**
85
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
86
     * @param array $options
87
     *
88
     * @return void
89
     */
90
    public function buildForm(FormBuilderInterface $builder, array $options)
91
    {
92
        $this->addCardType($builder, $options)
93
            ->addNameOnCard($builder)
94
            ->addHiddenInputs($builder);
95
    }
96
97
    /**
98
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
99
     * @param array $options
100
     *
101
     * @return \SprykerEco\Yves\Payone\Form\CreditCardSubForm
102
     */
103
    public function addCardType(FormBuilderInterface $builder, array $options)
104
    {
105
        $builder->add(
106
            self::FIELD_CARD_TYPE,
107
            ChoiceType::class,
108
            [
109
                'choices' => $options[self::OPTIONS_FIELD_NAME][self::OPTION_CARD_TYPES],
110
                'label' => false,
111
                'required' => true,
112
                'expanded' => false,
113
                'multiple' => false,
114
                'placeholder' => false,
115
                'constraints' => [
116
                    $this->createNotBlankConstraint(),
117
                ],
118
            ]
119
        );
120
121
        return $this;
122
    }
123
124
    /**
125
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
126
     *
127
     * @return \SprykerEco\Yves\Payone\Form\CreditCardSubForm
128
     */
129 View Code Duplication
    protected function addCardNumber(FormBuilderInterface $builder)
0 ignored issues
show
Duplication introduced by
This method 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...
130
    {
131
        $builder->add(
132
            self::FIELD_CARD_NUMBER,
133
            TextType::class,
134
            [
135
                'label' => false,
136
                'required' => false,
137
            ]
138
        );
139
140
        return $this;
141
    }
142
143
    /**
144
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
145
     *
146
     * @return \SprykerEco\Yves\Payone\Form\CreditCardSubForm
147
     */
148
    protected function addNameOnCard(FormBuilderInterface $builder)
149
    {
150
        $builder->add(
151
            self::FIELD_NAME_ON_CARD,
152
            TextType::class,
153
            [
154
                'label' => false,
155
                'required' => true,
156
                'constraints' => [
157
                    $this->createNotBlankConstraint(),
158
                ],
159
            ]
160
        );
161
162
        return $this;
163
    }
164
165
    /**
166
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
167
     * @param array $options
168
     *
169
     * @return \SprykerEco\Yves\Payone\Form\CreditCardSubForm
170
     */
171
    protected function addCardExpiresMonth(FormBuilderInterface $builder, array $options)
172
    {
173
        $builder->add(
174
            self::FIELD_CARD_EXPIRES_MONTH,
175
            ChoiceType::class,
176
            [
177
                'label' => false,
178
                'choices' => $options[self::OPTIONS_FIELD_NAME][self::OPTION_CARD_EXPIRES_CHOICES_MONTH],
179
                'required' => true,
180
                'constraints' => [
181
                    $this->createNotBlankConstraint(),
182
                ],
183
            ]
184
        );
185
186
        return $this;
187
    }
188
189
    /**
190
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
191
     * @param array $options
192
     *
193
     * @return \SprykerEco\Yves\Payone\Form\CreditCardSubForm
194
     */
195
    protected function addCardExpiresYear(FormBuilderInterface $builder, array $options)
196
    {
197
        $builder->add(
198
            self::FIELD_CARD_EXPIRES_YEAR,
199
            ChoiceType::class,
200
            [
201
                'label' => false,
202
                'choices' => $options[self::OPTIONS_FIELD_NAME][self::OPTION_CARD_EXPIRES_CHOICES_YEAR],
203
                'required' => true,
204
                'attr' => [
205
                    'placeholder' => 'Expires year',
206
                ],
207
                'constraints' => [
208
                    $this->createNotBlankConstraint(),
209
                ],
210
            ]
211
        );
212
213
        return $this;
214
    }
215
216
    /**
217
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
218
     *
219
     * @return \SprykerEco\Yves\Payone\Form\CreditCardSubForm
220
     */
221 View Code Duplication
    protected function addCardSecurityCode(FormBuilderInterface $builder)
0 ignored issues
show
Duplication introduced by
This method 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...
222
    {
223
        $builder->add(
224
            self::FIELD_CARD_SECURITY_CODE,
225
            TextType::class,
226
            [
227
                'label' => false,
228
                'required' => false,
229
            ]
230
        );
231
232
        return $this;
233
    }
234
235
    /**
236
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
237
     *
238
     * @return $this
239
     */
240
    protected function addHiddenInputs(FormBuilderInterface $builder)
241
    {
242
        parent::addHiddenInputs($builder);
243
244
        $builder->add(
245
            self::FIELD_PSEUDO_CARD_NUMBER,
246
            HiddenType::class,
247
            [
248
                'label' => false,
249
            ]
250
        );
251
252
        return $this;
253
    }
254
}
255