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