CreditCardSubForm::buildView()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 4
nop 3
dl 0
loc 22
rs 9.7333
c 0
b 0
f 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\Braintree\Form;
9
10
use Generated\Shared\Transfer\BraintreePaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\BraintreePaymentTransfer 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\Shared\Config\Config;
12
use SprykerEco\Shared\Braintree\BraintreeConfig;
13
use SprykerEco\Shared\Braintree\BraintreeConstants;
14
use Symfony\Component\Form\FormInterface;
15
use Symfony\Component\Form\FormView;
16
use Symfony\Component\OptionsResolver\OptionsResolver;
17
use Symfony\Component\String\UnicodeString;
18
19
class CreditCardSubForm extends AbstractSubForm
20
{
21
    public const PAYMENT_METHOD = 'credit-card';
22
23
    public const IS_3D_SECURE = 'is3dSecure';
24
    public const AMOUNT = 'amount';
25
    public const EMAIL = 'email';
26
27
    public const BILLING_ADDRESS = 'billingAddress';
28
    public const BILLING_ADDRESS_GIVEN_NAME = 'givenName';
29
    public const BILLING_ADDRESS_SURNAME = 'surname';
30
    public const BILLING_ADDRESS_PHONE_NUMBER = 'phoneNumber';
31
    public const BILLING_ADDRESS_STREET_ADDRESS = 'streetAddress';
32
    public const BILLING_ADDRESS_EXTENDED_ADDRESS = 'extendedAddress';
33
    public const BILLING_ADDRESS_LOCALITY = 'locality';
34
    public const BILLING_ADDRESS_REGION = 'region';
35
    public const BILLING_ADDRESS_POSTAL_CODE = 'postalCode';
36
    public const BILLING_ADDRESS_COUNTRY_CODE = 'countryCodeAlpha2';
37
38
    /**
39
     * @return string
40
     */
41
    public function getName()
42
    {
43
        return BraintreeConfig::PAYMENT_METHOD_CREDIT_CARD;
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function getPropertyPath()
50
    {
51
        return BraintreeConfig::PAYMENT_METHOD_CREDIT_CARD;
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getTemplatePath()
58
    {
59
        return BraintreeConfig::PROVIDER_NAME . '/' . static::PAYMENT_METHOD;
60
    }
61
62
    /**
63
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
64
     *
65
     * @return void
66
     */
67
    public function configureOptions(OptionsResolver $resolver): void
68
    {
69
        $resolver->setDefaults([
70
            'data_class' => BraintreePaymentTransfer::class,
71
        ])->setRequired(static::OPTIONS_FIELD_NAME);
72
    }
73
74
    /**
75
     * @param \Symfony\Component\Form\FormView $view The view
76
     * @param \Symfony\Component\Form\FormInterface $form The form
77
     * @param array $options The options
78
     *
79
     * @return void
80
     */
81
    public function buildView(FormView $view, FormInterface $form, array $options)
82
    {
83
        parent::buildView($view, $form, $options);
84
85
        $view->vars[static::CLIENT_TOKEN] = $this->generateClientToken();
86
        $view->vars[static::IS_3D_SECURE] = (string)Config::get(BraintreeConstants::IS_3D_SECURE);
87
88
        /** @var \Generated\Shared\Transfer\QuoteTransfer $quote */
89
        $quote = $form->getParent()->getViewData();
90
91
        $view->vars[static::EMAIL] = $quote->getCustomer()->getEmail();
92
        $view->vars[static::AMOUNT] = $quote->getTotals()->getGrandTotal();
93
        $view->vars[static::BILLING_ADDRESS] = [
94
            static::BILLING_ADDRESS_GIVEN_NAME => $this->convertToGermanAsciiFormat($quote->getBillingAddress()->getFirstName()),
95
            static::BILLING_ADDRESS_SURNAME => $this->convertToGermanAsciiFormat($quote->getBillingAddress()->getLastName()),
96
            static::BILLING_ADDRESS_PHONE_NUMBER => $quote->getBillingAddress()->getPhone(),
97
            static::BILLING_ADDRESS_STREET_ADDRESS => $quote->getBillingAddress()->getAddress1(),
98
            static::BILLING_ADDRESS_EXTENDED_ADDRESS => $quote->getBillingAddress()->getAddress2(),
99
            static::BILLING_ADDRESS_LOCALITY => $quote->getBillingAddress()->getCountry() ? $quote->getBillingAddress()->getCountry()->getName() : '',
100
            static::BILLING_ADDRESS_REGION => $quote->getBillingAddress()->getRegion(),
101
            static::BILLING_ADDRESS_POSTAL_CODE => $quote->getBillingAddress()->getZipCode(),
102
            static::BILLING_ADDRESS_COUNTRY_CODE => $quote->getBillingAddress()->getCountry() ? $quote->getBillingAddress()->getCountry()->getIso2Code() : '',
103
        ];
104
    }
105
106
    /**
107
     * @param string $string
108
     *
109
     * @return string
110
     */
111
    protected function convertToGermanAsciiFormat(string $string): string
112
    {
113
        return (new UnicodeString($string))
114
            ->ascii(['de-ascii'])
115
            ->toString();
116
    }
117
}
118