Passed
Pull Request — master (#67)
by
unknown
09:59
created

KlarnaSubForm   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 10
eloc 42
c 2
b 0
f 1
dl 0
loc 133
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A addPayMethodField() 0 19 2
A getTemplatePath() 0 3 1
A configureOptions() 0 5 1
A setDefaultOptions() 0 3 1
A getPropertyPath() 0 3 1
A buildForm() 0 7 1
A getName() 0 3 1
A buildView() 0 7 1
A addPayMethodTokenField() 0 13 1
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\PayoneKlarnaTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PayoneKlarnaTransfer 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\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\FormBuilderInterface;
17
use Symfony\Component\Form\FormInterface;
18
use Symfony\Component\Form\FormView;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
21
class KlarnaSubForm extends AbstractPayoneSubForm
22
{
23
    public const PAY_METHOD_CHOICES = 'pay_methods';
24
    public const WIDGET_PAY_METHODS = 'widget_pay_methods';
25
    public const BILLING_ADDRESS_DATA = 'billing_address_data';
26
    public const CUSTOMER_DATA = 'customer_data';
27
    protected const PAYMENT_METHOD = 'klarna';
28
    protected const FIELD_PAY_METHOD_TYPE = 'payMethod';
29
    protected const FIELD_PAY_METHOD_TOKEN = 'payMethodToken';
30
    protected const FORM_TEMPLATE_PATH = '%s/%s';
31
32
    /**
33
     * @return string
34
     */
35
    public function getName(): string
36
    {
37
        return PaymentTransfer::PAYONE_KLARNA;
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function getPropertyPath(): string
44
    {
45
        return PaymentTransfer::PAYONE_KLARNA;
46
    }
47
48
    /**
49
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
50
     * @param array $options
51
     *
52
     * @return void
53
     */
54
    public function buildForm(FormBuilderInterface $builder, array $options): void
55
    {
56
        $this->addPayMethodField(
57
            $builder,
58
            $options[SubFormInterface::OPTIONS_FIELD_NAME][static::PAY_METHOD_CHOICES]
59
        );
60
        $this->addPayMethodTokenField($builder);
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public function getTemplatePath(): string
67
    {
68
        return sprintf(self::FORM_TEMPLATE_PATH, PayoneConstants::PROVIDER_NAME, self::PAYMENT_METHOD);
69
    }
70
71
    /**
72
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
73
     *
74
     * @return void
75
     */
76
    public function configureOptions(OptionsResolver $resolver): void
77
    {
78
        $resolver->setDefaults([
79
            'data_class' => PayoneKlarnaTransfer::class,
80
        ])->setRequired(SubFormInterface::OPTIONS_FIELD_NAME);
81
    }
82
83
    /**
84
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
85
     *
86
     * @return void
87
     */
88
    public function setDefaultOptions(OptionsResolver $resolver): void
89
    {
90
        $this->configureOptions($resolver);
91
    }
92
93
    /**
94
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
95
     * @param array $choices
96
     *
97
     * @return $this
98
     */
99
    protected function addPayMethodField(FormBuilderInterface $builder, array $choices)
100
    {
101
        $choices = ['Choose payment category' => ''] + $choices;
102
103
        $builder->add(
104
            static::FIELD_PAY_METHOD_TYPE,
105
            ChoiceType::class,
106
            [
107
                'label' => false,
108
                'required' => true,
109
                'choices' => $choices,
110
                'data' => '',
111
                'choice_attr' => function ($val) {
112
                    return $val === '' ? ['disabled' => 'disabled', 'selected' => 'selected'] : ['disabled' => 'disabled'];
113
                },
114
            ]
115
        );
116
117
        return $this;
118
    }
119
120
    /**
121
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
122
     *
123
     * @return $this
124
     */
125
    protected function addPayMethodTokenField(FormBuilderInterface $builder)
126
    {
127
        $builder->add(
128
            static::FIELD_PAY_METHOD_TOKEN,
129
            HiddenType::class,
130
            [
131
                'label' => false,
132
                'required' => true,
133
                'data' => [],
134
            ]
135
        );
136
137
        return $this;
138
    }
139
140
    /**
141
     * @param \Symfony\Component\Form\FormView $view
142
     * @param \Symfony\Component\Form\FormInterface $form
143
     * @param array $options
144
     *
145
     * @return void
146
     */
147
    public function buildView(FormView $view, FormInterface $form, array $options): void
148
    {
149
        parent::buildView($view, $form, $options);
150
151
        $view->vars[static::BILLING_ADDRESS_DATA] = $options[SubFormInterface::OPTIONS_FIELD_NAME][static::BILLING_ADDRESS_DATA];
152
        $view->vars[static::CUSTOMER_DATA] = $options[SubFormInterface::OPTIONS_FIELD_NAME][static::CUSTOMER_DATA];
153
        $view->vars[static::WIDGET_PAY_METHODS] = $options[SubFormInterface::OPTIONS_FIELD_NAME][static::WIDGET_PAY_METHODS];
154
    }
155
}
156