EasyCreditSubForm::createNotBlankConstraint()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Heidelpay\Form;
9
10
use Generated\Shared\Transfer\HeidelpayEasyCreditPaymentTransfer;
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\Heidelpay\HeidelpayConfig;
15
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
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
use Symfony\Component\Validator\Constraint;
21
use Symfony\Component\Validator\Constraints\NotBlank;
22
23
class EasyCreditSubForm extends AbstractSubFormType implements SubFormInterface, SubFormProviderNameInterface
24
{
25
    public const VARS_KEY_LEGAL_TEXT = 'legalText';
26
    public const VARS_KEY_LOGO_URL = 'logo_url';
27
    public const VARS_KEY_INFO_LINK = 'info_link';
28
29
    protected const FIELD_EASY_CREDIT_POLICY_AGREEMENT = 'isPolicyAgreementChecked';
30
    protected const PAYMENT_METHOD_TEMPLATE_PATH = 'easy-credit';
31
32
    /**
33
     * @return string
34
     */
35
    public function getProviderName(): string
36
    {
37
        return HeidelpayConfig::PROVIDER_NAME;
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function getName(): string
44
    {
45
        return HeidelpayConfig::PAYMENT_METHOD_EASY_CREDIT;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getPropertyPath(): string
52
    {
53
        return HeidelpayConfig::PAYMENT_METHOD_EASY_CREDIT;
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getTemplatePath(): string
60
    {
61
        return HeidelpayConfig::PROVIDER_NAME . DIRECTORY_SEPARATOR . static::PAYMENT_METHOD_TEMPLATE_PATH;
62
    }
63
64
    /**
65
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
66
     *
67
     * @return void
68
     */
69
    public function configureOptions(OptionsResolver $resolver): void
70
    {
71
        $resolver->setDefaults([
72
            'data_class' => HeidelpayEasyCreditPaymentTransfer::class,
73
        ])->setRequired(static::OPTIONS_FIELD_NAME);
74
    }
75
76
    /**
77
     * @param \Symfony\Component\Form\FormView $view
78
     * @param \Symfony\Component\Form\FormInterface $form
79
     * @param array $options
80
     *
81
     * @return void
82
     */
83
    public function buildView(FormView $view, FormInterface $form, array $options): void
84
    {
85
        parent::buildView($view, $form, $options);
86
87
        $view->vars[static::VARS_KEY_LOGO_URL] = $options[static::OPTIONS_FIELD_NAME][static::VARS_KEY_LOGO_URL] ?? '';
88
        $view->vars[static::VARS_KEY_INFO_LINK] = $options[static::OPTIONS_FIELD_NAME][static::VARS_KEY_INFO_LINK] ?? '';
89
    }
90
91
    /**
92
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
93
     * @param array $options
94
     *
95
     * @return void
96
     */
97
    public function buildForm(FormBuilderInterface $builder, array $options): void
98
    {
99
        $this->addEasyCreditPaymentAgreement($builder, $options);
100
    }
101
102
    /**
103
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
104
     * @param array $options
105
     *
106
     * @return $this
107
     */
108
    public function addEasyCreditPaymentAgreement(FormBuilderInterface $builder, array $options)
109
    {
110
        $builder->add(
111
            static::FIELD_EASY_CREDIT_POLICY_AGREEMENT,
112
            CheckboxType::class,
113
            [
114
                'label' => $options[static::OPTIONS_FIELD_NAME][static::VARS_KEY_LEGAL_TEXT] ?? '',
115
                'constraints' => [
116
                    $this->createNotBlankConstraint(),
117
                ],
118
            ]
119
        );
120
121
        return $this;
122
    }
123
124
    /**
125
     * @return \Symfony\Component\Validator\Constraint
126
     */
127
    protected function createNotBlankConstraint(): Constraint
128
    {
129
        return new NotBlank(['groups' => $this->getPropertyPath()]);
130
    }
131
}
132