Passed
Pull Request — master (#809)
by Stanislav
06:02
created

addSameAsShippingCheckboxField()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 27
rs 9.7666
c 1
b 0
f 0
cc 4
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of the Spryker Commerce OS.
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types = 1);
9
10
namespace Pyz\Yves\CustomerPage\Form;
11
12
use Generated\Shared\Transfer\QuoteTransfer;
13
use SprykerShop\Yves\CustomerPage\Dependency\Service\CustomerPageToShipmentServiceInterface;
14
use SprykerShop\Yves\CustomerPage\Form\CheckoutAddressCollectionForm as SprykerCheckoutAddressCollectionForm;
15
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
16
use Symfony\Component\Form\FormBuilderInterface;
17
use Symfony\Component\Form\FormEvent;
18
use Symfony\Component\Form\FormInterface;
19
20
/**
21
 * @method \Pyz\Yves\CustomerPage\CustomerPageConfig getConfig()
22
 * @method \SprykerShop\Yves\CustomerPage\CustomerPageFactory getFactory()
23
 */
24
class CheckoutAddressCollectionForm extends SprykerCheckoutAddressCollectionForm
25
{
26
    /**
27
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
28
     *
29
     * @return $this
30
     */
31
    protected function addSameAsShippingCheckboxField(FormBuilderInterface $builder)
32
    {
33
        $builder->add(
34
            static::FIELD_BILLING_SAME_AS_SHIPPING,
35
            CheckboxType::class,
36
            [
37
                'required' => false,
38
                'constraints' => [],
39
                'validation_groups' => function (FormInterface $form) {
40
                    $shippingAddressForm = $form->getParent()
41
                        ? $form->getParent()->get(static::FIELD_SHIPPING_ADDRESS)
42
                        : null;
43
44
                    if (!$shippingAddressForm) {
45
                        return false;
46
                    }
47
48
                    if (!$this->isDeliverToMultipleAddressesEnabled($shippingAddressForm)) {
49
                        return false;
50
                    }
51
52
                    return [static::GROUP_BILLING_SAME_AS_SHIPPING];
53
                },
54
            ],
55
        );
56
57
        return $this;
58
    }
59
60
    protected function hydrateShippingAddressSubFormDataFromItemLevelShippingAddresses(
61
        FormEvent $event,
62
        CustomerPageToShipmentServiceInterface $shipmentService,
63
    ): void {
64
        $quoteTransfer = $event->getData();
65
66
        if (!($quoteTransfer instanceof QuoteTransfer)) {
67
            return;
68
        }
69
70
        $quoteTransfer = $this->executeCheckoutAddressStepPreGroupItemsByShipmentPlugins($quoteTransfer);
71
72
        $shipmentGroupCollection = $this->mergeShipmentGroupsByShipmentHash(
73
            $shipmentService->groupItemsByShipment($quoteTransfer->getItems()),
74
            $shipmentService->groupItemsByShipment($quoteTransfer->getBundleItems()),
75
        );
76
77
        $shippingAddressForm = $event->getForm()->get(static::FIELD_SHIPPING_ADDRESS);
78
79
        if ($quoteTransfer->getItems()->count() || $quoteTransfer->getBundleItems()->count()) {
80
            $this->setDeliverToMultipleAddressesEnabled($shippingAddressForm);
81
        }
82
83
        if ($this->isDeliverToMultipleAddressesEnabled($shippingAddressForm) || $shipmentGroupCollection->count() < 1) {
84
            return;
85
        }
86
87
        $shipmentGroupTransfer = $shipmentGroupCollection->getIterator()->current();
88
89
        if (!$shipmentGroupTransfer->getShipment() || !$shipmentGroupTransfer->getShipment()->getShippingAddress()) {
90
            return;
91
        }
92
93
        $shippingAddressForm->setData(clone $shipmentGroupTransfer->getShipment()->getShippingAddress());
94
    }
95
}
96