|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Customer\Address\Observers\CustomerAddressObserver |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2018 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import-customer-address |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Customer\Address\Observers; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Customer\Address\Utils\ColumnKeys; |
|
24
|
|
|
use TechDivision\Import\Customer\Address\Utils\MemberNames; |
|
25
|
|
|
use TechDivision\Import\Customer\Address\Services\CustomerAddressBunchProcessorInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Observer that create's the customer address itself. |
|
29
|
|
|
* |
|
30
|
|
|
* @author Tim Wagner <[email protected]> |
|
31
|
|
|
* @copyright 2018 TechDivision GmbH <[email protected]> |
|
32
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
33
|
|
|
* @link https://github.com/techdivision/import-customer-address |
|
34
|
|
|
* @link http://www.techdivision.com |
|
35
|
|
|
*/ |
|
36
|
|
|
class CustomerAddressObserver extends AbstractCustomerAddressImportObserver |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The customer address bunch processor instance. |
|
41
|
|
|
* |
|
42
|
|
|
* @var \TechDivision\Import\Customer\Address\Services\CustomerAddressBunchProcessorInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $customerAddressBunchProcessor; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Initialize the observer with the passed customer bunch processor instance. |
|
48
|
|
|
* |
|
49
|
|
|
* @param \TechDivision\Import\Customer\Address\Services\CustomerAddressBunchProcessorInterface $customerAddressBunchProcessor The customer address bunch processor instance |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(CustomerAddressBunchProcessorInterface $customerAddressBunchProcessor) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->customerAddressBunchProcessor = $customerAddressBunchProcessor; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Return's the customer address bunch processor instance. |
|
58
|
|
|
* |
|
59
|
|
|
* @return \TechDivision\Import\Customer\Address\Services\CustomerAddressBunchProcessorInterface The customer address bunch processor instance |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function getCustomerAddressBunchProcessor() |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->customerAddressBunchProcessor; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Process the observer's business logic. |
|
68
|
|
|
* |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function process() |
|
72
|
|
|
{ |
|
73
|
|
|
|
|
74
|
|
|
// prepare the static entity values |
|
75
|
|
|
$customerAddress = $this->initializeCustomerAddress($this->prepareAttributes()); |
|
76
|
|
|
|
|
77
|
|
|
// insert the entity and set the entity ID |
|
78
|
|
|
$this->setLastEntityId($this->persistCustomerAddress($customerAddress)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
|
83
|
|
|
* |
|
84
|
|
|
* @return array The prepared attributes |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function prepareAttributes() |
|
87
|
|
|
{ |
|
88
|
|
|
|
|
89
|
|
|
// load the website ID for the given code |
|
90
|
|
|
$websiteId = $this->getStoreWebsiteIdByCode($this->getValue(ColumnKeys::WEBSITE)); |
|
91
|
|
|
|
|
92
|
|
|
// load the customer |
|
93
|
|
|
$customer = $this->loadCustomerByEmailAndWebsiteId($this->getValue(ColumnKeys::EMAIL), $websiteId); |
|
94
|
|
|
|
|
95
|
|
|
// initialize the customer values |
|
96
|
|
|
$entityId = $this->getValue(ColumnKeys::ENTITY_ID); |
|
97
|
|
|
$city = $this->getValue(ColumnKeys::CITY); |
|
98
|
|
|
$company = $this->getValue(ColumnKeys::COMPANY); |
|
99
|
|
|
$countryId = $this->getValue(ColumnKeys::COUNTRY_ID); |
|
100
|
|
|
$fax = $this->getValue(ColumnKeys::FAX); |
|
101
|
|
|
$firstname = $this->getValue(ColumnKeys::FIRSTNAME); |
|
102
|
|
|
$lastname = $this->getValue(ColumnKeys::LASTNAME); |
|
103
|
|
|
$middlename = $this->getValue(ColumnKeys::MIDDLENAME); |
|
104
|
|
|
$postcode = $this->getValue(ColumnKeys::POSTCODE); |
|
105
|
|
|
$prefix = $this->getValue(ColumnKeys::PREFIX); |
|
106
|
|
|
$region = $this->getValue(ColumnKeys::REGION); |
|
107
|
|
|
$regionId = $this->getValue(ColumnKeys::REGION_ID); |
|
108
|
|
|
$street = $this->getValue(ColumnKeys::STREET); |
|
109
|
|
|
$suffix = $this->getValue(ColumnKeys::SUFFIX); |
|
110
|
|
|
$telephone = $this->getValue(ColumnKeys::TELEPHONE); |
|
111
|
|
|
$vatId = $this->getValue(ColumnKeys::VAT_ID); |
|
112
|
|
|
$vatIsValid = $this->getValue(ColumnKeys::VAT_IS_VALID); |
|
113
|
|
|
$vatRequestId = $this->getValue(ColumnKeys::VAT_REQUEST_ID); |
|
114
|
|
|
$vatRequestSuccess = $this->getValue(ColumnKeys::VAT_REQUEST_SUCCESS); |
|
115
|
|
|
|
|
116
|
|
|
// load the customer's addtional attributes |
|
117
|
|
|
$incrementId = $this->getValue(ColumnKeys::INCREMENT_ID); |
|
|
|
|
|
|
118
|
|
|
$isActive = 1; |
|
119
|
|
|
|
|
120
|
|
|
// prepare the date format for the created at/updated at dates |
|
121
|
|
|
$createdAt = $this->getValue(ColumnKeys::CREATED_AT, date('Y-m-d H:i:s'), array($this, 'formatDate')); |
|
122
|
|
|
$updatedAt = $this->getValue(ColumnKeys::UPDATED_AT, date('Y-m-d H:i:s'), array($this, 'formatDate')); |
|
123
|
|
|
$vatRequestDate = $this->getValue(ColumnKeys::VAT_REQUEST_DATE, null, array($this, 'formatDate')); |
|
124
|
|
|
|
|
125
|
|
|
// return the prepared customer |
|
126
|
|
|
return $this->initializeEntity( |
|
127
|
|
|
array( |
|
128
|
|
|
MemberNames::ENTITY_ID => $entityId, |
|
129
|
|
|
MemberNames::INCREMENT_ID => $incrementId, |
|
130
|
|
|
MemberNames::PARENT_ID => $customer[MemberNames::ENTITY_ID], |
|
131
|
|
|
MemberNames::CREATED_AT => $createdAt, |
|
132
|
|
|
MemberNames::UPDATED_AT => $updatedAt, |
|
133
|
|
|
MemberNames::IS_ACTIVE => $isActive, |
|
134
|
|
|
MemberNames::CITY => $city, |
|
135
|
|
|
MemberNames::COMPANY => $company, |
|
136
|
|
|
MemberNames::COUNTRY_ID => $countryId, |
|
137
|
|
|
MemberNames::FAX => $fax, |
|
138
|
|
|
MemberNames::FIRSTNAME => $firstname, |
|
139
|
|
|
MemberNames::LASTNAME => $lastname, |
|
140
|
|
|
MemberNames::MIDDLENAME => $middlename, |
|
141
|
|
|
MemberNames::POSTCODE => $postcode, |
|
142
|
|
|
MemberNames::PREFIX => $prefix, |
|
143
|
|
|
MemberNames::REGION => $region, |
|
144
|
|
|
MemberNames::REGION_ID => $regionId, |
|
145
|
|
|
MemberNames::STREET => $street, |
|
146
|
|
|
MemberNames::SUFFIX => $suffix, |
|
147
|
|
|
MemberNames::TELEPHONE => $telephone, |
|
148
|
|
|
MemberNames::VAT_ID => $vatId, |
|
149
|
|
|
MemberNames::VAT_IS_VALID => $vatIsValid, |
|
150
|
|
|
MemberNames::VAT_REQUEST_DATE => $vatRequestDate, |
|
151
|
|
|
MemberNames::VAT_REQUEST_ID => $vatRequestId, |
|
152
|
|
|
MemberNames::VAT_REQUEST_SUCCESS => $vatRequestSuccess |
|
153
|
|
|
) |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Initialize the customer address with the passed attributes and returns an instance. |
|
159
|
|
|
* |
|
160
|
|
|
* @param array $attr The customer address attributes |
|
161
|
|
|
* |
|
162
|
|
|
* @return array The initialized customer address |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function initializeCustomerAddress(array $attr) |
|
165
|
|
|
{ |
|
166
|
|
|
|
|
167
|
|
|
// try to load the customer address with the given entity ID |
|
168
|
|
|
if ($entity = $this->loadCustomerAddress($attr[MemberNames::ENTITY_ID])) { |
|
169
|
|
|
return $this->mergeEntity($entity, $attr); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
// try to load the customer address with the given increment ID |
|
173
|
|
|
if (!empty($attr[MemberNames::INCREMENT_ID]) && $entity = $this->loadCustomerAddressByIncrementId($attr[MemberNames::INCREMENT_ID])) { |
|
174
|
|
|
return $this->mergeEntity($entity, $attr); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
// remove the entity ID |
|
178
|
|
|
unset($attr[MemberNames::ENTITY_ID]); |
|
179
|
|
|
|
|
180
|
|
|
// simply return the attributes |
|
181
|
|
|
return $attr; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Return's the customer with the passed entity ID. |
|
186
|
|
|
* |
|
187
|
|
|
* @param integer $id The entity ID of the customer to return |
|
188
|
|
|
* |
|
189
|
|
|
* @return array|null The customer |
|
190
|
|
|
*/ |
|
191
|
|
|
protected function loadCustomerAddress($id) |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->getCustomerAddressBunchProcessor()->loadCustomerAddress($id); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Return's the customer with the passed increment ID. |
|
198
|
|
|
* |
|
199
|
|
|
* @param string|integer $incrementId The increment ID of the customer to return |
|
200
|
|
|
* |
|
201
|
|
|
* @return array|null The customer |
|
202
|
|
|
*/ |
|
203
|
|
|
protected function loadCustomerAddressByIncrementId($incrementId) |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->getCustomerAddressBunchProcessor()->loadCustomerAddressByIncrementId($incrementId); |
|
|
|
|
|
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Return's the customer with the passed email and website ID. |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $email The email of the customer to return |
|
212
|
|
|
* @param string $websiteId The website ID of the customer to return |
|
213
|
|
|
* |
|
214
|
|
|
* @return array|null The customer |
|
215
|
|
|
*/ |
|
216
|
|
|
protected function loadCustomerByEmailAndWebsiteId($email, $websiteId) |
|
217
|
|
|
{ |
|
218
|
|
|
return $this->getCustomerAddressBunchProcessor()->loadCustomerByEmailAndWebsiteId($email, $websiteId); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Persist's the passed customer address data and return's the ID. |
|
223
|
|
|
* |
|
224
|
|
|
* @param array $customerAddress The customer address data to persist |
|
225
|
|
|
* |
|
226
|
|
|
* @return string The ID of the persisted entity |
|
227
|
|
|
*/ |
|
228
|
|
|
protected function persistCustomerAddress($customerAddress) |
|
229
|
|
|
{ |
|
230
|
|
|
return $this->getCustomerAddressBunchProcessor()->persistCustomerAddress($customerAddress); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Set's the ID of the customer that has been created recently. |
|
235
|
|
|
* |
|
236
|
|
|
* @param string $lastEntityId The entity ID |
|
237
|
|
|
* |
|
238
|
|
|
* @return void |
|
239
|
|
|
*/ |
|
240
|
|
|
protected function setLastEntityId($lastEntityId) |
|
241
|
|
|
{ |
|
242
|
|
|
$this->getSubject()->setLastEntityId($lastEntityId); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Return's the store website for the passed code. |
|
247
|
|
|
* |
|
248
|
|
|
* @param string $code The code of the store website to return the ID for |
|
249
|
|
|
* |
|
250
|
|
|
* @return integer The store website ID |
|
251
|
|
|
* @throws \Exception Is thrown, if the store website with the requested code is not available |
|
252
|
|
|
*/ |
|
253
|
|
|
protected function getStoreWebsiteIdByCode($code) |
|
254
|
|
|
{ |
|
255
|
|
|
return $this->getSubject()->getStoreWebsiteIdByCode($code); |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|