Completed
Push — master ( 3ff7e6...cf5edc )
by Kamil
96:30 queued 63:14
created

CustomerUniqueAddressAdder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A add() 0 16 4
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\Component\Core\Customer;
13
14
use Sylius\Component\Addressing\Comparator\AddressComparatorInterface;
15
use Sylius\Component\Core\Model\AddressInterface;
16
use Sylius\Component\Core\Model\CustomerInterface;
17
use Sylius\Component\Customer\Context\CustomerContextInterface;
18
19
/**
20
 * @author Jan Góralski <[email protected]>
21
 */
22
final class CustomerUniqueAddressAdder implements AddressAdderInterface
23
{
24
    /**
25
     * @var AddressComparatorInterface
26
     */
27
    private $addressComparator;
28
29
    /**
30
     * @var CustomerContextInterface
31
     */
32
    private $customerContext;
33
34
    /**
35
     * @param AddressComparatorInterface $addressComparator
36
     * @param CustomerContextInterface $customerContext
37
     */
38
    public function __construct(
39
        AddressComparatorInterface $addressComparator,
40
        CustomerContextInterface $customerContext
41
    ) {
42
        $this->addressComparator = $addressComparator;
43
        $this->customerContext = $customerContext;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function add(AddressInterface $address)
50
    {
51
        /** @var CustomerInterface $customer */
52
        $customer = $this->customerContext->getCustomer();
53
        if (null === $customer) {
54
            return;
55
        }
56
57
        foreach ($customer->getAddresses() as $customerAddress) {
58
            if ($this->addressComparator->same($customerAddress, $address)) {
59
                return;
60
            }
61
        }
62
63
        $customer->addAddress($address);
64
    }
65
}
66