Completed
Push — master ( 647314...e3d872 )
by Kamil
20:49
created

CustomerOrderAddressesSaver::addAddress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
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
declare(strict_types=1);
13
14
namespace Sylius\Component\Core\Customer;
15
16
use Sylius\Component\Core\Model\AddressInterface;
17
use Sylius\Component\Core\Model\CustomerInterface;
18
use Sylius\Component\Core\Model\OrderInterface;
19
20
final class CustomerOrderAddressesSaver implements OrderAddressesSaverInterface
21
{
22
    /**
23
     * @var CustomerAddressAdderInterface
24
     */
25
    private $addressAdder;
26
27
    /**
28
     * @param CustomerAddressAdderInterface $addressAdder
29
     */
30
    public function __construct(CustomerAddressAdderInterface $addressAdder)
31
    {
32
        $this->addressAdder = $addressAdder;
33
    }
34
35
    /**
36
     * @param OrderInterface $order
37
     */
38
    public function saveAddresses(OrderInterface $order): void
39
    {
40
        /** @var CustomerInterface $customer */
41
        $customer = $order->getCustomer();
42
        if (null === $customer->getUser()) {
43
            return;
44
        }
45
46
        $this->addAddress($customer, $order->getBillingAddress());
47
        $this->addAddress($customer, $order->getShippingAddress());
48
    }
49
50
    /**
51
     * @param CustomerInterface $customer
52
     * @param AddressInterface|null $address
53
     */
54
    private function addAddress(CustomerInterface $customer, ?AddressInterface $address): void
55
    {
56
        if (null !== $address) {
57
            $this->addressAdder->add($customer, clone $address);
58
        }
59
    }
60
}
61