|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Product\Observers\AbstractProductImportObserver |
|
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 2016 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-product |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Product\Observers; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\EntityStatus; |
|
24
|
|
|
use TechDivision\Import\Product\Utils\ColumnKeys; |
|
25
|
|
|
use TechDivision\Import\Observers\AbstractObserver; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* A SLSB that handles the process to import product bunches. |
|
29
|
|
|
* |
|
30
|
|
|
* @author Tim Wagner <[email protected]> |
|
31
|
|
|
* @copyright 2016 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-product |
|
34
|
|
|
* @link http://www.techdivision.com |
|
35
|
|
|
*/ |
|
36
|
|
|
abstract class AbstractProductImportObserver extends AbstractObserver implements ProductImportObserverInterface |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The actual row, that has to be processed. |
|
41
|
|
|
* |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $row = array(); |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Set's the actual row, that has to be processed. |
|
48
|
|
|
* |
|
49
|
|
|
* @param array $row The row |
|
50
|
|
|
* |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
7 |
|
public function setRow(array $row) |
|
54
|
|
|
{ |
|
55
|
7 |
|
$this->row = $row; |
|
56
|
7 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Return's the actual row, that has to be processed. |
|
60
|
|
|
* |
|
61
|
|
|
* @return array The row |
|
62
|
|
|
*/ |
|
63
|
7 |
|
public function getRow() |
|
64
|
|
|
{ |
|
65
|
7 |
|
return $this->row; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Will be invoked by the action on the events the listener has been registered for. |
|
70
|
|
|
* |
|
71
|
|
|
* @param array $row The row to handle |
|
72
|
|
|
* |
|
73
|
|
|
* @return array The modified row |
|
74
|
|
|
* @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle() |
|
75
|
|
|
*/ |
|
76
|
7 |
|
public function handle(array $row) |
|
77
|
|
|
{ |
|
78
|
|
|
|
|
79
|
|
|
// initialize the row |
|
80
|
7 |
|
$this->setRow($row); |
|
81
|
|
|
|
|
82
|
|
|
// process the functionality and return the row |
|
83
|
7 |
|
$this->process(); |
|
84
|
|
|
|
|
85
|
|
|
// return the processed row |
|
86
|
7 |
|
return $this->getRow(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Process the observer's business logic. |
|
91
|
|
|
* |
|
92
|
|
|
* @return array The processed row |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function process() |
|
95
|
|
|
{ |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Set's the array containing header row. |
|
101
|
|
|
* |
|
102
|
|
|
* @param array $headers The array with the header row |
|
103
|
|
|
* |
|
104
|
|
|
* @return void |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setHeaders(array $headers) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->getSubject()->setHeaders($headers); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Return's the array containing header row. |
|
113
|
|
|
* |
|
114
|
|
|
* @return array The array with the header row |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function getHeaders() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->getSubject()->getHeaders(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Queries whether or not the header with the passed name is available. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $name The header name to query |
|
125
|
|
|
* |
|
126
|
|
|
* @return boolean TRUE if the header is available, else FALSE |
|
127
|
|
|
*/ |
|
128
|
7 |
|
protected function hasHeader($name) |
|
129
|
|
|
{ |
|
130
|
7 |
|
return $this->getSubject()->hasHeader($name); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Return's the header value for the passed name. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $name The name of the header to return the value for |
|
137
|
|
|
* |
|
138
|
|
|
* @return mixed The header value |
|
139
|
|
|
* \InvalidArgumentException Is thrown, if the header with the passed name is NOT available |
|
140
|
|
|
*/ |
|
141
|
7 |
|
protected function getHeader($name) |
|
142
|
|
|
{ |
|
143
|
7 |
|
return $this->getSubject()->getHeader($name); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Add's the header with the passed name and position, if not NULL. |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $name The header name to add |
|
150
|
|
|
* |
|
151
|
|
|
* @return integer The new headers position |
|
152
|
|
|
*/ |
|
153
|
|
|
protected function addHeader($name) |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->getSubject()->addHeader($name); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Return's TRUE if the passed SKU is the actual one. |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $sku The SKU to check |
|
162
|
|
|
* |
|
163
|
|
|
* @return boolean TRUE if the passed SKU is the actual one |
|
164
|
|
|
*/ |
|
165
|
7 |
|
protected function isLastSku($sku) |
|
166
|
|
|
{ |
|
167
|
7 |
|
return $this->getSubject()->getLastSku() === $sku; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Return's the ID of the product that has been created recently. |
|
172
|
|
|
* |
|
173
|
|
|
* @return string The entity Id |
|
174
|
|
|
*/ |
|
175
|
3 |
|
protected function getLastEntityId() |
|
176
|
|
|
{ |
|
177
|
3 |
|
return $this->getSubject()->getLastEntityId(); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Return's the source date format to use. |
|
182
|
|
|
* |
|
183
|
|
|
* @return string The source date format |
|
184
|
|
|
*/ |
|
185
|
1 |
|
protected function getSourceDateFormat() |
|
186
|
|
|
{ |
|
187
|
1 |
|
return $this->getSubject()->getSourceDateFormat(); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Cast's the passed value based on the backend type information. |
|
192
|
|
|
* |
|
193
|
|
|
* @param string $backendType The backend type to cast to |
|
194
|
|
|
* @param mixed $value The value to be casted |
|
195
|
|
|
* |
|
196
|
|
|
* @return mixed The casted value |
|
197
|
|
|
*/ |
|
198
|
|
|
protected function castValueByBackendType($backendType, $value) |
|
199
|
|
|
{ |
|
200
|
|
|
return $this->getSubject()->castValueByBackendType($backendType, $value); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Set's the store view code the create the product/attributes for. |
|
205
|
|
|
* |
|
206
|
|
|
* @param string $storeViewCode The store view code |
|
207
|
|
|
* |
|
208
|
|
|
* @return void |
|
209
|
|
|
*/ |
|
210
|
3 |
|
protected function setStoreViewCode($storeViewCode) |
|
211
|
|
|
{ |
|
212
|
3 |
|
$this->getSubject()->setStoreViewCode($storeViewCode); |
|
213
|
3 |
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Return's the store view code the create the product/attributes for. |
|
217
|
|
|
* |
|
218
|
|
|
* @param string|null $default The default value to return, if the store view code has not been set |
|
219
|
|
|
* |
|
220
|
|
|
* @return string The store view code |
|
221
|
|
|
*/ |
|
222
|
|
|
protected function getStoreViewCode($default = null) |
|
223
|
|
|
{ |
|
224
|
|
|
return $this->getSubject()->getStoreViewCode($default); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Prepare's the store view code in the subject. |
|
229
|
|
|
* |
|
230
|
|
|
* @return void |
|
231
|
|
|
*/ |
|
232
|
3 |
|
protected function prepareStoreViewCode() |
|
233
|
|
|
{ |
|
234
|
|
|
|
|
235
|
|
|
// re-set the store view code |
|
236
|
3 |
|
$this->setStoreViewCode(null); |
|
237
|
|
|
|
|
238
|
|
|
// initialize the store view code |
|
239
|
3 |
|
if ($storeViewCode = $this->getValue(ColumnKeys::STORE_VIEW_CODE)) { |
|
240
|
2 |
|
$this->setStoreViewCode($storeViewCode); |
|
241
|
|
|
} |
|
242
|
3 |
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Tries to format the passed value to a valid date with format 'Y-m-d H:i:s'. |
|
246
|
|
|
* If the passed value is NOT a valid date, NULL will be returned. |
|
247
|
|
|
* |
|
248
|
|
|
* @param string|null $value The value to format |
|
249
|
|
|
* |
|
250
|
|
|
* @return string The formatted date |
|
251
|
|
|
*/ |
|
252
|
1 |
|
protected function formatDate($value) |
|
253
|
|
|
{ |
|
254
|
|
|
|
|
255
|
|
|
// create a DateTime instance from the passed value |
|
256
|
1 |
|
if ($dateTime = \DateTime::createFromFormat($this->getSourceDateFormat(), $value)) { |
|
257
|
1 |
|
return $dateTime->format('Y-m-d H:i:s'); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
// return NULL, if the passed value is NOT a valid date |
|
261
|
|
|
return null; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Extracts the elements of the passed value by exploding them |
|
266
|
|
|
* with the also passed separator. |
|
267
|
|
|
* |
|
268
|
|
|
* @param string $value The value to extract |
|
269
|
|
|
* @param string $separator The separator used to extrace the elements |
|
270
|
|
|
* |
|
271
|
|
|
* @return array The exploded values |
|
272
|
|
|
*/ |
|
273
|
|
|
protected function explode($value, $separator = ',') |
|
274
|
|
|
{ |
|
275
|
|
|
return explode($separator, $value); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* Query whether or not the value with the passed key exists. |
|
280
|
|
|
* |
|
281
|
|
|
* @param string $key The key of the value to query |
|
282
|
|
|
* |
|
283
|
|
|
* @return boolean TRUE if the value is set, else FALSE |
|
284
|
|
|
*/ |
|
285
|
2 |
|
protected function hasValue($key) |
|
286
|
|
|
{ |
|
287
|
2 |
|
return isset($this->row[$this->getHeader($key)]); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Set the value in the passed column name. |
|
292
|
|
|
* |
|
293
|
|
|
* @param string $columName The column name to set the value for |
|
294
|
|
|
* @param mixed $value The value to set |
|
295
|
|
|
* |
|
296
|
|
|
* @return void |
|
297
|
|
|
*/ |
|
298
|
|
|
protected function setValue($columName, $value) |
|
299
|
|
|
{ |
|
300
|
|
|
$this->row[$this->getHeader($columName)] = $value; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Resolve's the value with the passed key from the actual row. If a callback will |
|
305
|
|
|
* be passed, the callback will be invoked with the found value as parameter. If |
|
306
|
|
|
* the value is NULL or empty, the default value will be returned. |
|
307
|
|
|
* |
|
308
|
|
|
* @param string $key The key of the value to return |
|
309
|
|
|
* @param mixed|null $default The default value, that has to be returned, if the row's value is empty |
|
310
|
|
|
* @param callable|null $callback The callback that has to be invoked on the value, e. g. to format it |
|
311
|
|
|
* |
|
312
|
|
|
* @return mixed|null The, almost formatted, value |
|
313
|
|
|
*/ |
|
314
|
7 |
|
protected function getValue($key, $default = null, callable $callback = null) |
|
315
|
|
|
{ |
|
316
|
|
|
|
|
317
|
|
|
// query whether or not the header is available |
|
318
|
7 |
|
if (!$this->hasHeader($key)) { |
|
319
|
|
|
return; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
// initialize the value |
|
323
|
7 |
|
$value = null; |
|
324
|
|
|
|
|
325
|
|
|
// try to load the header |
|
326
|
7 |
|
$header = $this->getHeader($key); |
|
327
|
|
|
|
|
328
|
|
|
// query wheter or not, the value with the requested key is available |
|
329
|
7 |
|
if (isset($this->row[$header])) { |
|
330
|
7 |
|
$value = $this->row[$header]; |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
// query whether or not, a callback has been passed |
|
334
|
7 |
|
if ($value != null && is_callable($callback)) { |
|
335
|
1 |
|
$value = call_user_func($callback, $value); |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
// query whether or not |
|
339
|
7 |
|
if ($value == null && $default != null) { |
|
340
|
|
|
$value = $default; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
// return the value |
|
344
|
7 |
|
return $value; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* Initialize's and return's a new entity with the status 'create'. |
|
349
|
|
|
* |
|
350
|
|
|
* @param array $attr The attributes to merge into the new entity |
|
351
|
|
|
* |
|
352
|
|
|
* @return array The initialized entity |
|
353
|
|
|
*/ |
|
354
|
4 |
|
protected function initializeEntity(array $attr = array()) |
|
355
|
|
|
{ |
|
356
|
4 |
|
return array_merge(array(EntityStatus::MEMBER_NAME => EntityStatus::STATUS_CREATE), $attr); |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
/** |
|
360
|
|
|
* Merge's and return's the entity with the passed attributes and set's the |
|
361
|
|
|
* status to 'update'. |
|
362
|
|
|
* |
|
363
|
|
|
* @param array $entity The entity to merge the attributes into |
|
364
|
|
|
* @param array $attr The attributes to be merged |
|
365
|
|
|
* |
|
366
|
|
|
* @return array The merged entity |
|
367
|
|
|
*/ |
|
368
|
2 |
|
protected function mergeEntity(array $entity, array $attr) |
|
369
|
|
|
{ |
|
370
|
2 |
|
return array_merge($entity, $attr, array(EntityStatus::MEMBER_NAME => EntityStatus::STATUS_UPDATE)); |
|
371
|
|
|
} |
|
372
|
|
|
} |
|
373
|
|
|
|