Issues (8)

Plugin/AddInputMaskCheckoutPlugin.php (1 issue)

Severity
1
<?php
2
/**
3
 * Copyright © O2TI. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See COPYING.txt for license details.
7
 */
8
9
declare(strict_types=1);
10
11
namespace O2TI\InputMasking\Plugin;
12
13
use Magento\Checkout\Block\Checkout\LayoutProcessor;
14
use O2TI\InputMasking\Helper\Config;
15
use O2TI\InputMasking\Model\Masking;
16
17
/**
18
 *  AddInputMaskCheckoutPlugin - Change Components.
19
 */
20
class AddInputMaskCheckoutPlugin
21
{
22
    /**
23
     * @var Config
24
     */
25
    private $config;
26
27
    /**
28
     * @var Masking
29
     */
30
    private $masking;
31
32
    /**
33
     * @param Config  $config
34
     * @param Masking $masking
35
     */
36
    public function __construct(
37
        Config $config,
38
        Masking $masking
39
    ) {
40
        $this->config = $config;
41
        $this->masking = $masking;
42
    }
43
44
    /**
45
     * Change Components in Create Account.
46
     *
47
     * @param array $jsLayout
48
     *
49
     * @return array
50
     */
51
    public function changeCreateAccount(array $jsLayout): ?array
52
    {
53
        // phpcs:ignore
54
        if (isset($jsLayout['components']['checkout']['children']['steps']['children']['identification-step']['children']['identification']['children']['createAccount']['children']['create-account-fieldset']['children'])) {
55
            // phpcs:ignore
56
            $createAccountFields = &$jsLayout['components']['checkout']['children']['steps']['children']['identification-step']['children']['identification']['children']['createAccount']['children']['create-account-fieldset']['children'];
57
            $createAccountFields = $this->masking->createMaskToAddressFields($createAccountFields);
58
            $createAccountFields = $this->masking->createMaskToAccountFields($createAccountFields);
59
            $createAccountFields = $this->masking->changeComponentFields($createAccountFields);
60
        }
61
62
        return $jsLayout;
63
    }
64
65
    /**
66
     * Change Components in Shipping.
67
     *
68
     * @param array $jsLayout
69
     *
70
     * @return array
71
     */
72
    public function changeShippingFields(array $jsLayout): ?array
73
    {
74
        // phpcs:ignore
75
        if (isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step'])) {
76
            // phpcs:ignore
77
            $shippingFields = &$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children'];
78
            $shippingFields = $this->masking->createMaskToAddressFields($shippingFields);
79
            $shippingFields = $this->masking->changeComponentFields($shippingFields);
80
        }
81
82
        return $jsLayout;
83
    }
84
85
    /**
86
     * Change Components in Billing.
87
     *
88
     * @param array $jsLayout
89
     *
90
     * @return array
91
     */
92
    public function changeBillingFields(array $jsLayout): array
93
    {
94
        // phpcs:ignore
95
        foreach ($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['payments-list']['children'] as &$payment) {
96
            if (isset($payment['children']['form-fields'])) {
97
                $billingFields = &$payment['children']['form-fields']['children'];
98
                $billingFields = $this->masking->createMaskToAddressFields($billingFields);
99
                $billingFields = $this->masking->changeComponentFields($billingFields);
100
            }
101
        }
102
        // phpcs:ignore
103
        if (isset($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['afterMethods']['children']['billing-address-form'])) {
104
            // phpcs:ignore
105
            $billingAddressOnPage = &$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['afterMethods']['children']['billing-address-form']['children']['form-fields']['children'];
106
            $billingAddressOnPage = $this->masking->createMaskToAddressFields($billingAddressOnPage);
107
            $billingAddressOnPage = $this->masking->changeComponentFields($billingAddressOnPage);
108
        }
109
        // phpcs:ignore
110
        if (isset($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['beforeMethods']['children']['billing-address-form'])) {
111
            // phpcs:ignore
112
            $billingAddressOnPage = &$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['beforeMethods']['children']['billing-address-form']['children']['form-fields']['children'];
113
            $billingAddressOnPage = $this->masking->createMaskToAddressFields($billingAddressOnPage);
114
            $billingAddressOnPage = $this->masking->changeComponentFields($billingAddressOnPage);
115
        }
116
117
        return $jsLayout;
118
    }
119
120
    /**
121
     * Select Components for Change.
122
     *
123
     * @param LayoutProcessor $layoutProcessor
124
     * @param callable        $proceed
125
     * @param array           $args
126
     *
127
     * @return array
128
     */
129
    public function aroundProcess(LayoutProcessor $layoutProcessor, callable $proceed, array $args): array
130
    {
131
        $jsLayout = $proceed($args);
132
        if ($this->config->isEnabled()) {
133
            $jsLayout = $this->changeCreateAccount($jsLayout);
134
            $jsLayout = $this->changeShippingFields($jsLayout);
135
            $jsLayout = $this->changeBillingFields($jsLayout);
136
            $layoutProcessor = $layoutProcessor;
0 ignored issues
show
The assignment to $layoutProcessor is dead and can be removed.
Loading history...
137
        }
138
139
        return $jsLayout;
140
    }
141
}
142