1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Customer\Address\Observers\AbstractDefaultAddressImportObserver |
5
|
|
|
* |
6
|
|
|
* PHP version 7 |
7
|
|
|
* |
8
|
|
|
* @author Vadim Justus <[email protected]> |
9
|
|
|
* @author Harald Deiser <[email protected]> |
10
|
|
|
* @author Tim Wagner <[email protected]> |
11
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
12
|
|
|
* @license https://opensource.org/licenses/MIT |
13
|
|
|
* @link https://github.com/techdivision/import-customer-address |
14
|
|
|
* @link http://www.techdivision.com |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace TechDivision\Import\Customer\Address\Observers; |
18
|
|
|
|
19
|
|
|
use TechDivision\Import\Customer\Utils\MemberNames; |
20
|
|
|
use TechDivision\Import\Customer\Address\Utils\ColumnKeys; |
21
|
|
|
use TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface; |
22
|
|
|
use TechDivision\Import\Customer\Observers\AbstractCustomerImportObserver; |
23
|
|
|
use TechDivision\Import\Observers\StateDetectorInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Abstract class that provides the functionality to update a customers default |
27
|
|
|
* shipping and billing address. |
28
|
|
|
* |
29
|
|
|
* @author Vadim Justus <[email protected]> |
30
|
|
|
* @author Harald Deiser <[email protected]> |
31
|
|
|
* @author Tim Wagner <[email protected]> |
32
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
33
|
|
|
* @license https://opensource.org/licenses/MIT |
34
|
|
|
* @link https://github.com/techdivision/import-customer-address |
35
|
|
|
* @link http://www.techdivision.com |
36
|
|
|
*/ |
37
|
|
|
abstract class AbstractDefaultAddressImportObserver extends AbstractCustomerImportObserver |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The customer bunch processor instance. |
42
|
|
|
* |
43
|
|
|
* @var \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface |
44
|
|
|
*/ |
45
|
|
|
protected $customerBunchProcessor; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The mapping for the default billing/shipping address column => member name. |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $defaultAddressMapping = array( |
53
|
|
|
ColumnKeys::ADDRESS_DEFAULT_BILLING => MemberNames::DEFAULT_BILLING, |
54
|
|
|
ColumnKeys::ADDRESS_DEFAULT_SHIPPING => MemberNames::DEFAULT_SHIPPING |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* DefaultShippingObserver constructor. |
59
|
|
|
* |
60
|
|
|
* @param \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface $customerBunchProcessor The processor instance |
61
|
|
|
* @param \TechDivision\Import\Observers\StateDetectorInterface $stateDetector The state detector instance |
62
|
|
|
*/ |
63
|
|
|
public function __construct( |
64
|
|
|
CustomerBunchProcessorInterface $customerBunchProcessor, |
65
|
|
|
StateDetectorInterface $stateDetector = null |
66
|
|
|
) { |
67
|
|
|
parent::__construct($stateDetector); |
68
|
|
|
$this->customerBunchProcessor = $customerBunchProcessor; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns the customer bunch processor instance. |
73
|
|
|
* |
74
|
|
|
* @return \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface The processor instance |
75
|
|
|
*/ |
76
|
|
|
protected function getCustomerBunchProcessor() |
77
|
|
|
{ |
78
|
|
|
return $this->customerBunchProcessor; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Maps the passed customer address column name to the matching customer member name. |
83
|
|
|
* |
84
|
|
|
* @param string $columnName The column name to map |
85
|
|
|
* |
86
|
|
|
* @return string The mapped customer member name |
87
|
|
|
* @throws \Exception Is thrown if the column can't be mapped |
88
|
|
|
*/ |
89
|
|
|
protected function mapColumName($columnName) |
90
|
|
|
{ |
91
|
|
|
|
92
|
|
|
// query whether or not we can match the column name |
93
|
|
|
if (isset($this->defaultAddressMapping[$columnName])) { |
94
|
|
|
return $this->defaultAddressMapping[$columnName]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// throw an exception if NOT |
98
|
|
|
throw new \Exception(sprintf('Can\'t map member name to default address column "%s"', $columnName)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Save default address by type. |
103
|
|
|
* |
104
|
|
|
* @param string $type The address type to save |
105
|
|
|
* @param string|null $changeSetName The change set name to use |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
protected function saveDefaultAddressByType($type, $changeSetName = null) |
109
|
|
|
{ |
110
|
|
|
|
111
|
|
|
// load email and website ID |
112
|
|
|
$email = $this->getValue(ColumnKeys::EMAIL); |
113
|
|
|
$websiteId = $this->getSubject()->getStoreWebsiteIdByCode($this->getValue(ColumnKeys::WEBSITE)); |
114
|
|
|
|
115
|
|
|
// try to load the customer with the given email + website ID |
116
|
|
|
if ($customer = $this->getCustomerBunchProcessor()->loadCustomerByEmailAndWebsiteId($email, $websiteId)) { |
117
|
|
|
// initialize an empty address ID |
118
|
|
|
$addressId = null; |
|
|
|
|
119
|
|
|
|
120
|
|
|
// query whether or not we've a default shipping/billing address |
121
|
|
|
if ((integer) $this->getValue($type) === 1) { |
122
|
|
|
$addressId = $this->getSubject()->getLastEntityId(); |
123
|
|
|
} else { |
124
|
|
|
// Don't update address when not marked as default |
125
|
|
|
// Prevent resetting default address |
126
|
|
|
return; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$customerMerged = $this->mergeEntity( |
130
|
|
|
$customer, |
131
|
|
|
array( |
132
|
|
|
$this->mapColumName($type) => $addressId, |
133
|
|
|
MemberNames::UPDATED_AT => $this->formatDate(date($this->getSourceDateFormat())) |
134
|
|
|
), |
135
|
|
|
$changeSetName |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
// finally update the customer if relation has changed |
139
|
|
|
if ($this->hasChanges($customerMerged)) { |
140
|
|
|
$this->getCustomerBunchProcessor()->persistCustomer($customerMerged); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|