Completed
Pull Request — master (#4)
by Oleksandr
17:46 queued 08:55
created

CreditCardSubForm::buildView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 8
rs 10
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Yves\CrefoPay\Form;
9
10
use Generated\Shared\Transfer\CrefoPayPaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CrefoPayPaymentTransfer 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 Spryker\Yves\StepEngine\Dependency\Form\AbstractSubFormType;
12
use Spryker\Yves\StepEngine\Dependency\Form\SubFormInterface;
13
use Spryker\Yves\StepEngine\Dependency\Form\SubFormProviderNameInterface;
14
use SprykerEco\Shared\CrefoPay\CrefoPayConfig;
15
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
16
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
17
use Symfony\Component\Form\FormBuilderInterface;
18
use Symfony\Component\Form\FormInterface;
19
use Symfony\Component\Form\FormView;
20
use Symfony\Component\OptionsResolver\OptionsResolver;
21
22
class CreditCardSubForm extends AbstractSubFormType implements SubFormInterface, SubFormProviderNameInterface
23
{
24
    public const CREFO_PAY_SHOP_PUBLIC_KEY = 'shopPublicKey';
25
    public const CREFO_PAY_ORDER_ID = 'orderID';
26
    public const CREFO_PAY_SECURE_FIELDS_API_ENDPOINT = 'secureFieldsApiEndpoint';
27
    public const CREFO_PAY_SECURE_FIELDS_PLACEHOLDERS = 'secureFieldsPlaceholders';
28
29
    protected const PAYMENT_METHOD = 'credit-card';
30
    protected const FORM_FIELD_PAYMENT_METHOD = 'paymentMethod';
31
    protected const FORM_FIELD_PAYMENT_METHOD_DATA = 'CC';
32
    protected const FORM_FIELD_PAYMENT_INSTRUMENT_ID = 'paymentInstrumentId';
33
34
    /**
35
     * @return string
36
     */
37
    public function getProviderName(): string
38
    {
39
        return CrefoPayConfig::PROVIDER_NAME;
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    public function getName(): string
46
    {
47
        return CrefoPayConfig::CREFO_PAY_PAYMENT_METHOD_CREDIT_CARD;
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    public function getPropertyPath(): string
54
    {
55
        return CrefoPayConfig::CREFO_PAY_PAYMENT_METHOD_CREDIT_CARD;
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    public function getTemplatePath(): string
62
    {
63
        return CrefoPayConfig::PROVIDER_NAME . DIRECTORY_SEPARATOR . static::PAYMENT_METHOD;
64
    }
65
66
    /**
67
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
68
     *
69
     * @return void
70
     */
71
    public function configureOptions(OptionsResolver $resolver): void
72
    {
73
        $resolver->setDefaults([
74
            'data_class' => CrefoPayPaymentTransfer::class,
75
        ])->setRequired(static::OPTIONS_FIELD_NAME);
76
    }
77
78
    /**
79
     * @param \Symfony\Component\Form\FormView $view The view
80
     * @param \Symfony\Component\Form\FormInterface $form The form
81
     * @param array $options The options
82
     *
83
     * @return void
84
     */
85
    public function buildView(FormView $view, FormInterface $form, array $options): void
86
    {
87
        parent::buildView($view, $form, $options);
88
        $selectedOptions = $options[static::OPTIONS_FIELD_NAME];
89
        $view->vars[static::CREFO_PAY_SHOP_PUBLIC_KEY] = $selectedOptions[static::CREFO_PAY_SHOP_PUBLIC_KEY];
90
        $view->vars[static::CREFO_PAY_ORDER_ID] = $selectedOptions[static::CREFO_PAY_ORDER_ID];
91
        $view->vars[static::CREFO_PAY_SECURE_FIELDS_API_ENDPOINT] = $selectedOptions[static::CREFO_PAY_SECURE_FIELDS_API_ENDPOINT];
92
        $view->vars[static::CREFO_PAY_SECURE_FIELDS_PLACEHOLDERS] = $selectedOptions[static::CREFO_PAY_SECURE_FIELDS_PLACEHOLDERS];
93
    }
94
95
    /**
96
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
97
     * @param array $options
98
     *
99
     * @return void
100
     */
101
    public function buildForm(FormBuilderInterface $builder, array $options): void
102
    {
103
        $this->addPaymentMethod($builder)
104
            ->addPaymentInstrumentId($builder);
105
    }
106
107
    /**
108
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
109
     *
110
     * @return $this
111
     */
112
    protected function addPaymentMethod(FormBuilderInterface $builder)
113
    {
114
        $builder->add(
115
            static::FORM_FIELD_PAYMENT_METHOD,
116
            ChoiceType::class,
117
            [
118
                'choices' => [static::FORM_FIELD_PAYMENT_METHOD_DATA],
119
                'choices_as_values' => true,
120
                'expanded' => true,
121
            ]
122
        );
123
124
        return $this;
125
    }
126
127
    /**
128
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
129
     *
130
     * @return $this
131
     */
132
    protected function addPaymentInstrumentId(FormBuilderInterface $builder)
133
    {
134
        $builder->add(
135
            static::FORM_FIELD_PAYMENT_INSTRUMENT_ID,
136
            HiddenType::class,
137
            ['label' => false]
138
        );
139
140
        return $this;
141
    }
142
}
143