|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Customer\Subjects\AbstractCustomerSubject |
|
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 |
|
12
|
|
|
* @link http://www.techdivision.com |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace TechDivision\Import\Customer\Subjects; |
|
16
|
|
|
|
|
17
|
|
|
use TechDivision\Import\Customer\Utils\MemberNames; |
|
18
|
|
|
use TechDivision\Import\Subjects\AbstractEavSubject; |
|
19
|
|
|
use TechDivision\Import\Subjects\EntitySubjectInterface; |
|
20
|
|
|
use TechDivision\Import\Customer\Utils\ConfigurationKeys; |
|
21
|
|
|
use TechDivision\Import\Customer\Utils\RegistryKeys; |
|
22
|
|
|
use TechDivision\Import\Utils\FrontendInputTypes; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The abstract customer subject implementation that provides basic customer |
|
26
|
|
|
* handling business logic. |
|
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 |
|
32
|
|
|
* @link http://www.techdivision.com |
|
33
|
|
|
*/ |
|
34
|
|
|
abstract class AbstractCustomerSubject extends AbstractEavSubject implements EntitySubjectInterface |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The ID of the customer that has been created recently. |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $lastEntityId; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The identifier (email + website) of the customer that has been created recently. |
|
46
|
|
|
* |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $lastIdentifier; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* The Magento 2 configuration. |
|
53
|
|
|
* |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $coreConfigData; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* The mapping for the mail webseite code to the created entity IDs. |
|
60
|
|
|
* |
|
61
|
|
|
* @var array |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $customerIdentifierEntityIdMapping = array(); |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* The available store websites. |
|
67
|
|
|
* |
|
68
|
|
|
* @var array |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $storeWebsites = array(); |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* The default mappings for the user defined attributes, based on the attributes frontend input type. |
|
75
|
|
|
* |
|
76
|
|
|
* @var array |
|
77
|
|
|
*/ |
|
78
|
|
|
protected $defaultFrontendInputCallbackMappings = array( |
|
79
|
|
|
FrontendInputTypes::SELECT => array('import_customer.callback.select'), |
|
80
|
|
|
FrontendInputTypes::MULTISELECT => array('import_customer.callback.multiselect'), |
|
81
|
|
|
FrontendInputTypes::BOOLEAN => array('import_customer.callback.boolean') |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Sets the customer identifier of the last imported customer. |
|
86
|
|
|
* |
|
87
|
|
|
* @param array $identifier The unique customer identifier |
|
88
|
|
|
* |
|
89
|
|
|
* @return void |
|
90
|
|
|
*/ |
|
91
|
|
|
public function setLastIdentifier(array $identifier) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->lastIdentifier = $identifier; |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Return's the SKU of the last imported customer. |
|
98
|
|
|
* |
|
99
|
|
|
* @return string|null The SKU |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getLastIdentifier() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->lastIdentifier; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Set's the ID of the customer that has been created recently. |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $lastEntityId The entity ID |
|
110
|
|
|
* |
|
111
|
|
|
* @return void |
|
112
|
|
|
*/ |
|
113
|
|
|
public function setLastEntityId($lastEntityId) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->lastEntityId = $lastEntityId; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Return's the ID of the customer that has been created recently. |
|
120
|
|
|
* |
|
121
|
|
|
* @return string The entity Id |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getLastEntityId() |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->lastEntityId; |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Queries whether or not the customer with the passed identifier has already been processed. |
|
130
|
|
|
* |
|
131
|
|
|
* @param array $identifier The customer identifier to check |
|
132
|
|
|
* |
|
133
|
|
|
* @return boolean TRUE if the customer has been processed, else FALSE |
|
134
|
|
|
*/ |
|
135
|
|
|
public function hasBeenProcessed(array $identifier) |
|
136
|
|
|
{ |
|
137
|
|
|
|
|
138
|
|
|
// explode the identifier (we need email + website code) |
|
139
|
|
|
list ($email, $website) = $identifier; |
|
140
|
|
|
|
|
141
|
|
|
// query whether or not the entity ID has already been mapped |
|
142
|
|
|
return isset($this->customerIdentifierEntityIdMapping[$email][$website]); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Queries whether or not the passed PK and store view code has already been processed. |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $pk The PK to check been processed |
|
149
|
|
|
* @param string $storeViewCode The store view code to check been processed |
|
150
|
|
|
* |
|
151
|
|
|
* @return boolean TRUE if the PK and store view code has been processed, else FALSE |
|
152
|
|
|
*/ |
|
153
|
|
|
public function storeViewHasBeenProcessed($pk, $storeViewCode) |
|
|
|
|
|
|
154
|
|
|
{ |
|
155
|
|
|
return false; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Add the passed mail address/website code => entity ID mapping. |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $email The mail address of the customer |
|
162
|
|
|
* @param string $website The website code the customer is bound to |
|
163
|
|
|
* |
|
164
|
|
|
* @return void |
|
165
|
|
|
*/ |
|
166
|
|
|
public function addCustomerIdentifierEntityIdMapping($email, $website) |
|
167
|
|
|
{ |
|
168
|
|
|
$this->customerIdentifierEntityIdMapping[$email][$website] = $this->getLastEntityId(); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Intializes the previously loaded global data for exactly one bunch. |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $serial The serial of the actual import |
|
175
|
|
|
* |
|
176
|
|
|
* @return void |
|
177
|
|
|
*/ |
|
178
|
|
|
public function setUp($serial) |
|
179
|
|
|
{ |
|
180
|
|
|
|
|
181
|
|
|
// load the status of the actual import |
|
182
|
|
|
$status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS); |
|
183
|
|
|
|
|
184
|
|
|
// load the global data we've prepared initially |
|
185
|
|
|
$this->storeWebsites = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::STORE_WEBSITES]; |
|
186
|
|
|
|
|
187
|
|
|
// invoke the parent method |
|
188
|
|
|
parent::setUp($serial); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Clean up the global data after importing the bunch. |
|
193
|
|
|
* |
|
194
|
|
|
* @param string $serial The serial of the actual import |
|
195
|
|
|
* |
|
196
|
|
|
* @return void |
|
197
|
|
|
*/ |
|
198
|
|
|
public function tearDown($serial) |
|
199
|
|
|
{ |
|
200
|
|
|
|
|
201
|
|
|
// load the registry processor |
|
202
|
|
|
$registryProcessor = $this->getRegistryProcessor(); |
|
203
|
|
|
|
|
204
|
|
|
// update the status |
|
205
|
|
|
$registryProcessor->mergeAttributesRecursive( |
|
206
|
|
|
RegistryKeys::STATUS, |
|
207
|
|
|
array( |
|
208
|
|
|
RegistryKeys::CUSTOMER_IDENTIFIER_ENTITY_ID_MAPPING => $this->customerIdentifierEntityIdMapping, |
|
209
|
|
|
) |
|
210
|
|
|
); |
|
211
|
|
|
|
|
212
|
|
|
// invoke the parent method |
|
213
|
|
|
parent::tearDown($serial); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Return's the store ID of the actual row, or of the default store |
|
218
|
|
|
* if no store view code is set in the CSV file. |
|
219
|
|
|
* |
|
220
|
|
|
* @param string|null $default The default store view code to use, if no store view code is set in the CSV file |
|
221
|
|
|
* |
|
222
|
|
|
* @return integer The ID of the actual store |
|
223
|
|
|
* @throws \Exception Is thrown, if the store with the actual code is not available |
|
224
|
|
|
*/ |
|
225
|
|
|
public function getRowStoreId($default = null) |
|
226
|
|
|
{ |
|
227
|
|
|
|
|
228
|
|
|
// initialize the default store view code, if not passed |
|
229
|
|
|
if ($default == null) { |
|
|
|
|
|
|
230
|
|
|
$defaultStore = $this->getDefaultStore(); |
|
231
|
|
|
$default = $defaultStore[MemberNames::CODE]; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
// load the store view code to create the customer/attributes for |
|
235
|
|
|
$storeViewCode = $this->getStoreViewCode($default); |
|
236
|
|
|
|
|
237
|
|
|
// query whether or not, the requested store is available |
|
238
|
|
|
if (isset($this->stores[$storeViewCode])) { |
|
239
|
|
|
return (integer) $this->stores[$storeViewCode][MemberNames::STORE_ID]; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
// throw an exception, if not |
|
243
|
|
|
throw new \Exception( |
|
244
|
|
|
$this->appendExceptionSuffix( |
|
245
|
|
|
sprintf('Found invalid store view code %s', $storeViewCode) |
|
246
|
|
|
) |
|
247
|
|
|
); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Merge the columns from the configuration with all image type columns to define which |
|
252
|
|
|
* columns should be cleaned-up. |
|
253
|
|
|
* |
|
254
|
|
|
* @return array The columns that has to be cleaned-up |
|
255
|
|
|
*/ |
|
256
|
|
|
public function getCleanUpColumns() |
|
257
|
|
|
{ |
|
258
|
|
|
return $this->getConfiguration()->getParam(ConfigurationKeys::CLEAN_UP_EMPTY_COLUMNS); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Return's the store website for the passed code. |
|
263
|
|
|
* |
|
264
|
|
|
* @param string $code The code of the store website to return the ID for |
|
265
|
|
|
* |
|
266
|
|
|
* @return integer The store website ID |
|
267
|
|
|
* @throws \Exception Is thrown, if the store website with the requested code is not available |
|
268
|
|
|
*/ |
|
269
|
|
|
public function getStoreWebsiteIdByCode($code) |
|
270
|
|
|
{ |
|
271
|
|
|
|
|
272
|
|
|
// query whether or not, the requested store website is available |
|
273
|
|
|
if (isset($this->storeWebsites[$code])) { |
|
274
|
|
|
return (integer) $this->storeWebsites[$code][MemberNames::WEBSITE_ID]; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
// throw an exception, if not |
|
278
|
|
|
throw new \Exception( |
|
279
|
|
|
$this->appendExceptionSuffix( |
|
280
|
|
|
sprintf('Found invalid website code %s', $code) |
|
281
|
|
|
) |
|
282
|
|
|
); |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..