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