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