Completed
Push — master ( 4bc269...bc2ce1 )
by Paweł
35:58 queued 24:11
created

setCustomerAddressing()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\CoreBundle\EventListener;
13
14
use Sylius\Component\Core\Model\CustomerInterface;
15
use Sylius\Component\Core\Model\OrderInterface;
16
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
17
use Symfony\Component\EventDispatcher\GenericEvent;
18
19
/**
20
 * Automatic set customer's default addressing
21
 *
22
 * @author Liverbool <[email protected]>
23
 */
24
final class CheckoutAddressingListener
25
{
26
    /**
27
     * @param GenericEvent $event
28
     */
29
    public function setCustomerAddressing(GenericEvent $event)
30
    {
31
        $order = $event->getSubject();
32
33
        if (!$order instanceof OrderInterface) {
34
            throw new UnexpectedTypeException(
35
                $order,
36
                OrderInterface::class
37
            );
38
        }
39
40
        /** @var CustomerInterface $customer */
41
        if (null === $customer = $order->getCustomer()) {
42
            return;
43
        }
44
45
        if (null === $customer->getDefaultAddress()) {
46
            $customer->setDefaultAddress(clone $order->getShippingAddress());
47
        }
48
    }
49
}
50