InvoiceSecuredB2cSubForm::getPropertyPath()   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
c 0
b 0
f 0
dl 0
loc 3
rs 10
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 DateTime;
11
use Generated\Shared\Transfer\HeidelpayPaymentTransfer;
12
use Spryker\Yves\StepEngine\Dependency\Form\AbstractSubFormType;
13
use Spryker\Yves\StepEngine\Dependency\Form\SubFormInterface;
14
use Spryker\Yves\StepEngine\Dependency\Form\SubFormProviderNameInterface;
15
use SprykerEco\Shared\Heidelpay\HeidelpayConfig;
16
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
17
use Symfony\Component\Form\FormBuilderInterface;
18
use Symfony\Component\OptionsResolver\OptionsResolver;
19
use Symfony\Component\Validator\Constraint;
20
use Symfony\Component\Validator\Constraints\Callback;
21
use Symfony\Component\Validator\Constraints\NotBlank;
22
use Symfony\Component\Validator\Context\ExecutionContextInterface;
23
24
class InvoiceSecuredB2cSubForm extends AbstractSubFormType implements SubFormInterface, SubFormProviderNameInterface
25
{
26
    protected const PAYMENT_METHOD_TEMPLATE_PATH = 'invoice-secured-b2c';
27
    protected const FORM_FIELD_DATE_OF_BIRTH = 'dateOfBirth';
28
    protected const LABEL_DATE_OF_BIRTH = 'payment.heidelpay.date_of_birth_label';
29
    protected const FORMAT_DATE_OF_BIRTH = 'yyyy-MM-dd';
30
    protected const PLACEHOLDER_DATE_OF_BIRTH = 'customer.birth_date';
31
    protected const MIN_BIRTHDAY_DATE_STRING = '-18 years';
32
    protected const AGE_VIOLATION_MESSAGE = 'checkout.step.payment.must_be_older_than_18_years';
33
    protected const WIDGET_TYPE = 'single_text';
34
    protected const INPUT_TYPE = 'string';
35
36
    /**
37
     * @return string
38
     */
39
    public function getProviderName(): string
40
    {
41
        return HeidelpayConfig::PROVIDER_NAME;
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function getName(): string
48
    {
49
        return HeidelpayConfig::PAYMENT_METHOD_INVOICE_SECURED_B2C;
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function getPropertyPath(): string
56
    {
57
        return HeidelpayConfig::PAYMENT_METHOD_INVOICE_SECURED_B2C;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getTemplatePath(): string
64
    {
65
        return HeidelpayConfig::PROVIDER_NAME . DIRECTORY_SEPARATOR . static::PAYMENT_METHOD_TEMPLATE_PATH;
66
    }
67
68
    /**
69
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
70
     *
71
     * @return void
72
     */
73
    public function configureOptions(OptionsResolver $resolver): void
74
    {
75
        $resolver->setDefaults([
76
            'data_class' => HeidelpayPaymentTransfer::class,
77
        ])->setRequired(static::OPTIONS_FIELD_NAME);
78
    }
79
80
    /**
81
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
82
     * @param array $options
83
     *
84
     * @return void
85
     */
86
    public function buildForm(FormBuilderInterface $builder, array $options): void
87
    {
88
        $this->addDateOfBirth($builder);
89
    }
90
91
    /**
92
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
93
     *
94
     * @return $this
95
     */
96
    protected function addDateOfBirth(FormBuilderInterface $builder)
97
    {
98
        $builder->add(
99
            static::FORM_FIELD_DATE_OF_BIRTH,
100
            BirthdayType::class,
101
            [
102
                'label' => static::LABEL_DATE_OF_BIRTH,
103
                'required' => true,
104
                'widget' => static::WIDGET_TYPE,
105
                'format' => static::FORMAT_DATE_OF_BIRTH,
106
                'input' => static::INPUT_TYPE,
107
                'attr' => [
108
                    'placeholder' => static::PLACEHOLDER_DATE_OF_BIRTH,
109
                ],
110
111
                'constraints' => [
112
                    $this->createNotBlankConstraint(),
113
                    $this->createBirthdayConstraint(),
114
                ],
115
            ]
116
        );
117
118
        return $this;
119
    }
120
121
    /**
122
     * @return \Symfony\Component\Validator\Constraint
123
     */
124
    protected function createNotBlankConstraint(): Constraint
125
    {
126
        return new NotBlank(['groups' => $this->getPropertyPath()]);
127
    }
128
129
    /**
130
     * @return \Symfony\Component\Validator\Constraint
131
     */
132
    protected function createBirthdayConstraint(): Constraint
133
    {
134
        return new Callback([
135
            'callback' => function ($date, ExecutionContextInterface $context) {
136
                $inputDate = new DateTime($date);
137
                $minBirthDate = new DateTime(static::MIN_BIRTHDAY_DATE_STRING);
138
                if ($inputDate > $minBirthDate) {
139
                    $context->addViolation(static::AGE_VIOLATION_MESSAGE);
140
                }
141
            },
142
            'groups' => $this->getPropertyPath(),
143
        ]);
144
    }
145
}
146