1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* NOTICE OF LICENSE |
5
|
|
|
* |
6
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
7
|
|
|
* that is available through the world-wide-web at this URL: |
8
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
9
|
|
|
* |
10
|
|
|
* @author Vadim Justus <[email protected]> |
11
|
|
|
* @author Harald Deiser <[email protected]> |
12
|
|
|
* @copyright 2018 TechDivision GmbH <[email protected]> |
13
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
14
|
|
|
* @link https://github.com/techdivision/import-customer-address |
15
|
|
|
* @link http://www.techdivision.com |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace TechDivision\Import\Customer\Address\Observers; |
19
|
|
|
|
20
|
|
|
use TechDivision\Import\Customer\Address\Repositories\CustomerAddressRepositoryInterface; |
21
|
|
|
use TechDivision\Import\Customer\Observers\AbstractCustomerImportObserver; |
22
|
|
|
use TechDivision\Import\Customer\Actions\CustomerActionInterface; |
23
|
|
|
use TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface; |
24
|
|
|
use TechDivision\Import\Customer\Utils\ColumnKeys; |
25
|
|
|
use TechDivision\Import\Customer\Utils\MemberNames as OriginalMemberNames; |
26
|
|
|
use TechDivision\Import\Utils\EntityStatus; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Abstract class to set shipping and billing address. |
30
|
|
|
* |
31
|
|
|
* @copyright Copyright (c) 2019 TechDivision GmbH (http://www.techdivision.com) |
32
|
|
|
* @author TechDivision Team Allstars <[email protected]> |
33
|
|
|
* @link http://www.techdivision.com/ |
34
|
|
|
*/ |
35
|
|
|
abstract class AbstractDefaultAddressImportObserver extends AbstractCustomerImportObserver |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Constants to define default types. |
39
|
|
|
*/ |
40
|
|
|
const TYPE_DEFAULT_SHIPPING = OriginalMemberNames::DEFAULT_SHIPPING; |
41
|
|
|
const TYPE_DEFAULT_BILLING = OriginalMemberNames::DEFAULT_BILLING; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var CustomerBunchProcessorInterface |
45
|
|
|
*/ |
46
|
|
|
protected $customerBunchProcessor; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var CustomerAddressRepositoryInterface |
50
|
|
|
*/ |
51
|
|
|
protected $customerAddressRepository; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var CustomerActionInterface |
55
|
|
|
*/ |
56
|
|
|
protected $customerAction; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* DefaultShippingObserver constructor. |
60
|
|
|
* |
61
|
|
|
* @param CustomerBunchProcessorInterface $customerBunchProcessor |
62
|
|
|
* @param CustomerAddressRepositoryInterface $customerAddressRepository |
63
|
|
|
* @param CustomerActionInterface $customerAction |
64
|
|
|
*/ |
65
|
|
|
public function __construct( |
66
|
|
|
CustomerBunchProcessorInterface $customerBunchProcessor, |
67
|
|
|
CustomerAddressRepositoryInterface $customerAddressRepository, |
68
|
|
|
CustomerActionInterface $customerAction |
69
|
|
|
) { |
70
|
|
|
$this->customerBunchProcessor = $customerBunchProcessor; |
71
|
|
|
$this->customerAddressRepository = $customerAddressRepository; |
72
|
|
|
$this->customerAction = $customerAction; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Save default address by type. |
77
|
|
|
* |
78
|
|
|
* @param string $type |
79
|
|
|
*/ |
80
|
|
|
protected function saveDefaultAddressByType($type) |
81
|
|
|
{ |
82
|
|
|
$defaultShipping = $this->getValue('_address_' . $type . '_'); |
83
|
|
|
|
84
|
|
|
if ($defaultShipping) { |
85
|
|
|
// load email and website code |
86
|
|
|
$email = $this->getValue('_' . ColumnKeys::EMAIL); |
87
|
|
|
$websiteCode = $this->getValue(ColumnKeys::WEBSITE); |
88
|
|
|
$websiteId = $this->getSubject()->getStoreWebsiteIdByCode($websiteCode); |
89
|
|
|
|
90
|
|
|
$customer = $this->customerBunchProcessor->loadCustomerByEmailAndWebsiteId($email, $websiteId); |
91
|
|
|
$addressId = $this->getSubject()->getLastEntityId(); |
92
|
|
|
|
93
|
|
|
if (isset($addressId)) { |
94
|
|
|
$customer = $this->prepareCustomerArray($type, $customer, $addressId); |
95
|
|
|
$this->customerAction->persist($customer); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Prepare customer array. |
102
|
|
|
* |
103
|
|
|
* @param string $type |
104
|
|
|
* @param array $customer |
105
|
|
|
* @param int $addressId |
106
|
|
|
* |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
protected function prepareCustomerArray($type, $customer, $addressId) |
110
|
|
|
{ |
111
|
|
|
unset($customer[OriginalMemberNames::CREATED_AT]); |
112
|
|
|
$customer[$type] = $addressId; |
113
|
|
|
$customer[EntityStatus::MEMBER_NAME] = EntityStatus::STATUS_UPDATE; |
114
|
|
|
|
115
|
|
|
return $customer; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|