|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Observers\AttributeObserverTrait |
|
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 |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Observers; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\MemberNames; |
|
24
|
|
|
use TechDivision\Import\Utils\StoreViewCodes; |
|
25
|
|
|
use TechDivision\Import\Utils\BackendTypeKeys; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Observer that creates/updates the EAV attributes. |
|
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 |
|
34
|
|
|
* @link http://www.techdivision.com |
|
35
|
|
|
*/ |
|
36
|
|
|
trait AttributeObserverTrait |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The ID of the attribute to create the values for. |
|
41
|
|
|
* |
|
42
|
|
|
* @var integer |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $attributeId; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* The attribute code of the attribute to create the values for. |
|
48
|
|
|
* |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $attributeCode; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* The backend type of the attribute to create the values for. |
|
55
|
|
|
* |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $backendType; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* The attribute value to process. |
|
62
|
|
|
* |
|
63
|
|
|
* @var mixed |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $attributeValue; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* The attribute code that has to be processed. |
|
69
|
|
|
* |
|
70
|
|
|
* @return string The attribute code |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function getAttributeCode() |
|
73
|
|
|
{ |
|
74
|
1 |
|
return $this->attributeCode; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* The attribute value that has to be processed. |
|
79
|
|
|
* |
|
80
|
|
|
* @return string The attribute value |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function getAttributeValue() |
|
83
|
|
|
{ |
|
84
|
1 |
|
return $this->attributeValue; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Remove all the empty values from the row and return the cleared row. |
|
89
|
|
|
* |
|
90
|
|
|
* @return array The cleared row |
|
91
|
|
|
*/ |
|
92
|
7 |
|
protected function clearRow() |
|
93
|
|
|
{ |
|
94
|
|
|
|
|
95
|
|
|
// remove all the empty values from the row |
|
96
|
7 |
|
return array_filter( |
|
97
|
7 |
|
$this->row, |
|
|
|
|
|
|
98
|
7 |
|
function ($value, $key) { |
|
|
|
|
|
|
99
|
7 |
|
return ($value !== null && $value !== ''); |
|
100
|
7 |
|
}, |
|
101
|
7 |
|
ARRAY_FILTER_USE_BOTH |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Process the observer's business logic. |
|
107
|
|
|
* |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
7 |
|
protected function process() |
|
111
|
|
|
{ |
|
112
|
|
|
|
|
113
|
|
|
// initialize the store view code |
|
114
|
7 |
|
$this->prepareStoreViewCode(); |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
// load the attributes by the found attribute set and the backend types |
|
117
|
7 |
|
$attributes = $this->getAttributes(); |
|
118
|
7 |
|
$backendTypes = $this->getBackendTypes(); |
|
119
|
|
|
|
|
120
|
|
|
// load the header keys |
|
121
|
7 |
|
$headers = array_flip($this->getHeaders()); |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
// remove all the empty values from the row |
|
124
|
7 |
|
$row = $this->clearRow(); |
|
125
|
|
|
|
|
126
|
|
|
// iterate over the attributes and append them to the row |
|
127
|
7 |
|
foreach ($row as $key => $attributeValue) { |
|
128
|
|
|
// query whether or not attribute with the found code exists |
|
129
|
6 |
|
if (!isset($attributes[$attributeCode = $headers[$key]])) { |
|
130
|
|
|
// log a message in debug mode |
|
131
|
1 |
|
if ($this->isDebugMode()) { |
|
|
|
|
|
|
132
|
1 |
|
$this->getSystemLogger()->debug( |
|
|
|
|
|
|
133
|
1 |
|
sprintf( |
|
134
|
1 |
|
'Can\'t find attribute with attribute code %s in file %s on line %d', |
|
135
|
1 |
|
$attributeCode, |
|
136
|
1 |
|
$this->getFilename(), |
|
|
|
|
|
|
137
|
1 |
|
$this->getLineNumber() |
|
|
|
|
|
|
138
|
|
|
) |
|
139
|
|
|
); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
// stop processing |
|
143
|
1 |
|
continue; |
|
144
|
|
|
|
|
|
|
|
|
|
145
|
|
|
} else { |
|
146
|
|
|
// log a message in debug mode |
|
147
|
5 |
|
if ($this->isDebugMode()) { |
|
|
|
|
|
|
148
|
2 |
|
$this->getSystemLogger()->debug( |
|
|
|
|
|
|
149
|
2 |
|
sprintf( |
|
150
|
2 |
|
'Found attribute with attribute code %s in file %s on line %d', |
|
151
|
2 |
|
$attributeCode, |
|
152
|
2 |
|
$this->getFilename(), |
|
|
|
|
|
|
153
|
2 |
|
$this->getLineNumber() |
|
|
|
|
|
|
154
|
|
|
) |
|
155
|
|
|
); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
// if yes, load the attribute by its code |
|
160
|
5 |
|
$attribute = $attributes[$attributeCode]; |
|
161
|
|
|
|
|
162
|
|
|
// load the backend type => to find the apropriate entity |
|
163
|
5 |
|
$backendType = $attribute[MemberNames::BACKEND_TYPE]; |
|
164
|
5 |
|
if ($backendType == null) { |
|
165
|
1 |
|
$this->getSystemLogger()->warning( |
|
|
|
|
|
|
166
|
1 |
|
sprintf( |
|
167
|
1 |
|
'Found EMTPY backend type for attribute %s in file %s on line %d', |
|
168
|
1 |
|
$attributeCode, |
|
169
|
1 |
|
$this->getFilename(), |
|
|
|
|
|
|
170
|
1 |
|
$this->getLineNumber() |
|
|
|
|
|
|
171
|
|
|
) |
|
172
|
|
|
); |
|
173
|
1 |
|
continue; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
// do nothing on static backend type |
|
177
|
4 |
|
if ($backendType === BackendTypeKeys::BACKEND_TYPE_STATIC) { |
|
178
|
1 |
|
continue; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
// query whether or not we've found a supported backend type |
|
182
|
3 |
|
if (isset($backendTypes[$backendType])) { |
|
183
|
|
|
// initialize attribute ID/code and backend type |
|
184
|
2 |
|
$this->attributeId = $attribute[MemberNames::ATTRIBUTE_ID]; |
|
185
|
2 |
|
$this->attributeCode = $attributeCode; |
|
186
|
2 |
|
$this->backendType = $backendType; |
|
187
|
|
|
|
|
188
|
|
|
// initialize the persist method for the found backend type |
|
189
|
2 |
|
list ($persistMethod, ) = $backendTypes[$backendType]; |
|
190
|
|
|
|
|
191
|
|
|
// set the attribute value |
|
192
|
2 |
|
$this->attributeValue = $attributeValue; |
|
193
|
|
|
|
|
194
|
|
|
// try to prepare the attribute values |
|
195
|
2 |
|
if ($attr = $this->prepareAttributes()) { |
|
196
|
|
|
// initialize and persist the attribute |
|
197
|
1 |
|
$entity = $this->initializeAttribute($attr); |
|
198
|
1 |
|
$this->$persistMethod($entity); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
// continue with the next value |
|
202
|
2 |
|
continue; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
// log the debug message |
|
206
|
1 |
|
$this->getSystemLogger()->debug( |
|
|
|
|
|
|
207
|
1 |
|
sprintf( |
|
208
|
1 |
|
'Found invalid backend type %s for attribute %s in file %s on line %s', |
|
209
|
1 |
|
$backendType, |
|
210
|
1 |
|
$attributeCode, |
|
211
|
1 |
|
$this->getFilename(), |
|
|
|
|
|
|
212
|
1 |
|
$this->getLineNumber() |
|
|
|
|
|
|
213
|
|
|
) |
|
214
|
|
|
); |
|
215
|
|
|
} |
|
216
|
7 |
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
|
220
|
|
|
* |
|
221
|
|
|
* @return array|null The prepared attributes |
|
222
|
|
|
*/ |
|
223
|
2 |
|
protected function prepareAttributes() |
|
224
|
|
|
{ |
|
225
|
|
|
|
|
226
|
|
|
// laod the callbacks for the actual attribute code |
|
227
|
2 |
|
$callbacks = $this->getCallbacksByType($this->attributeCode); |
|
228
|
|
|
|
|
229
|
|
|
// invoke the pre-cast callbacks |
|
230
|
|
|
/** @var \TechDivision\Import\Callbacks\CallbackInterface $callback */ |
|
231
|
2 |
|
foreach ($callbacks as $callback) { |
|
232
|
1 |
|
$this->attributeValue = $callback->handle($this); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
// query whether or not the attribute has been be processed by the callbacks |
|
236
|
2 |
|
if ($this->attributeValue === null) { |
|
237
|
1 |
|
return; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
// load the ID of the product that has been created recently |
|
241
|
1 |
|
$lastEntityId = $this->getPrimaryKey(); |
|
242
|
|
|
|
|
243
|
|
|
// load the store ID |
|
244
|
1 |
|
$storeId = $this->getRowStoreId(StoreViewCodes::ADMIN); |
|
|
|
|
|
|
245
|
|
|
|
|
246
|
|
|
// cast the value based on the backend type |
|
247
|
1 |
|
$castedValue = $this->castValueByBackendType($this->backendType, $this->attributeValue); |
|
|
|
|
|
|
248
|
|
|
|
|
249
|
|
|
// prepare the attribute values |
|
250
|
1 |
|
return $this->initializeEntity( |
|
|
|
|
|
|
251
|
|
|
array( |
|
252
|
1 |
|
MemberNames::ENTITY_ID => $lastEntityId, |
|
253
|
1 |
|
MemberNames::ATTRIBUTE_ID => $this->attributeId, |
|
254
|
1 |
|
MemberNames::STORE_ID => $storeId, |
|
255
|
1 |
|
MemberNames::VALUE => $castedValue |
|
256
|
|
|
) |
|
257
|
|
|
); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Initialize the category product with the passed attributes and returns an instance. |
|
262
|
|
|
* |
|
263
|
|
|
* @param array $attr The category product attributes |
|
264
|
|
|
* |
|
265
|
|
|
* @return array The initialized category product |
|
266
|
|
|
*/ |
|
267
|
1 |
|
protected function initializeAttribute(array $attr) |
|
268
|
|
|
{ |
|
269
|
1 |
|
return $attr; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Return's the PK to create the product => attribute relation. |
|
274
|
|
|
* |
|
275
|
|
|
* @return integer The PK to create the relation with |
|
276
|
|
|
*/ |
|
277
|
1 |
|
protected function getPrimaryKey() |
|
278
|
|
|
{ |
|
279
|
1 |
|
return $this->getLastEntityId(); |
|
|
|
|
|
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Return's the array with callbacks for the passed type. |
|
284
|
|
|
* |
|
285
|
|
|
* @param string $type The type of the callbacks to return |
|
286
|
|
|
* |
|
287
|
|
|
* @return array The callbacks |
|
288
|
|
|
*/ |
|
289
|
2 |
|
protected function getCallbacksByType($type) |
|
290
|
|
|
{ |
|
291
|
2 |
|
return $this->getSubject()->getCallbacksByType($type); |
|
|
|
|
|
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Return's mapping for the supported backend types (for the product entity) => persist methods. |
|
296
|
|
|
* |
|
297
|
|
|
* @return array The mapping for the supported backend types |
|
298
|
|
|
*/ |
|
299
|
7 |
|
protected function getBackendTypes() |
|
300
|
|
|
{ |
|
301
|
7 |
|
return $this->getSubject()->getBackendTypes(); |
|
|
|
|
|
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Return's the attributes for the attribute set of the product that has to be created. |
|
306
|
|
|
* |
|
307
|
|
|
* @return array The attributes |
|
308
|
|
|
* @throws \Exception |
|
309
|
|
|
*/ |
|
310
|
7 |
|
protected function getAttributes() |
|
311
|
|
|
{ |
|
312
|
7 |
|
return $this->getSubject()->getAttributes(); |
|
|
|
|
|
|
313
|
|
|
} |
|
314
|
|
|
} |
|
315
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: