1 | <?php |
||
39 | trait AttributeObserverTrait |
||
40 | { |
||
41 | |||
42 | /** |
||
43 | * The ID of the attribute to create the values for. |
||
44 | * |
||
45 | * @var integer |
||
46 | */ |
||
47 | protected $attributeId; |
||
48 | |||
49 | /** |
||
50 | * The attribute code of the attribute to create the values for. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $attributeCode; |
||
55 | |||
56 | /** |
||
57 | * The backend type of the attribute to create the values for. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $backendType; |
||
62 | |||
63 | /** |
||
64 | * The attribute value to process. |
||
65 | * |
||
66 | * @var mixed |
||
67 | */ |
||
68 | protected $attributeValue; |
||
69 | |||
70 | /** |
||
71 | * The array with the column keys that has to be cleaned up when their values are empty. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $cleanUpEmptyColumnKeys; |
||
76 | |||
77 | /** |
||
78 | * The entity's existing attribues. |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $attributes; |
||
83 | |||
84 | /** |
||
85 | * The attribute code that has to be processed. |
||
86 | * |
||
87 | * @return string The attribute code |
||
88 | */ |
||
89 | 1 | public function getAttributeCode() |
|
93 | |||
94 | /** |
||
95 | * The attribute value that has to be processed. |
||
96 | * |
||
97 | * @return string The attribute value |
||
98 | */ |
||
99 | 1 | public function getAttributeValue() |
|
103 | |||
104 | /** |
||
105 | * Remove all the empty values from the row and return the cleared row. |
||
106 | * |
||
107 | * @return array The cleared row |
||
108 | */ |
||
109 | 7 | protected function clearRow() |
|
142 | |||
143 | /** |
||
144 | * Returns the value(s) of the primary key column(s). As the primary key column can |
||
145 | * also consist of two columns, the return value can be an array also. |
||
146 | * |
||
147 | * @return mixed The primary key value(s) |
||
148 | */ |
||
149 | 7 | protected function getPrimaryKeyValue() |
|
153 | |||
154 | /** |
||
155 | * Process the observer's business logic. |
||
156 | * |
||
157 | * @return void |
||
158 | */ |
||
159 | 7 | protected function process() |
|
160 | { |
||
161 | |||
162 | // initialize the store view code |
||
163 | 7 | $this->prepareStoreViewCode(); |
|
164 | |||
165 | // load the store ID, use the admin store if NO store view code has been set |
||
166 | 7 | $storeId = $this->getRowStoreId(StoreViewCodes::ADMIN); |
|
167 | |||
168 | // load the entity's existing attributes |
||
169 | 7 | $this->getAttributesByPrimaryKeyAndStoreId($this->getPrimaryKey(), $storeId); |
|
170 | |||
171 | // load the store view - if no store view has been set, we assume the admin |
||
172 | // store view, which will contain the default (fallback) attribute values |
||
173 | 7 | $storeViewCode = $this->getSubject()->getStoreViewCode(StoreViewCodes::ADMIN); |
|
174 | |||
175 | // query whether or not the row has already been processed |
||
176 | 7 | if ($this->storeViewHasBeenProcessed($pk = $this->getPrimaryKeyValue(), $storeViewCode)) { |
|
177 | // log a message |
||
178 | $this->getSystemLogger()->warning( |
||
179 | $this->appendExceptionSuffix( |
||
180 | sprintf( |
||
181 | 'Attributes for %s "%s" + store view code "%s" has already been processed', |
||
182 | $this->getPrimaryKeyColumnName(), |
||
183 | $pk, |
||
184 | $storeViewCode |
||
185 | ) |
||
186 | ) |
||
187 | ); |
||
188 | |||
189 | // return immediately |
||
190 | return; |
||
191 | } |
||
192 | |||
193 | // load the attributes by the found attribute set and the backend types |
||
194 | 7 | $attributes = $this->getAttributes(); |
|
195 | 7 | $backendTypes = $this->getBackendTypes(); |
|
196 | |||
197 | // load the header keys |
||
198 | 7 | $headers = array_flip($this->getHeaders()); |
|
199 | |||
200 | // remove all the empty values from the row |
||
201 | 7 | $row = $this->clearRow(); |
|
202 | |||
203 | // iterate over the attributes and append them to the row |
||
204 | 7 | foreach ($row as $key => $attributeValue) { |
|
205 | // query whether or not attribute with the found code exists |
||
206 | 6 | if (!isset($attributes[$attributeCode = $headers[$key]])) { |
|
207 | // log a message in debug mode |
||
208 | 1 | if ($this->isDebugMode()) { |
|
209 | 1 | $this->getSystemLogger()->debug( |
|
210 | 1 | $this->appendExceptionSuffix( |
|
211 | 1 | sprintf( |
|
212 | 1 | 'Can\'t find attribute with attribute code %s', |
|
213 | $attributeCode |
||
214 | ) |
||
215 | ) |
||
216 | ); |
||
217 | } |
||
218 | |||
219 | // stop processing |
||
220 | 1 | continue; |
|
221 | } else { |
||
222 | // log a message in debug mode |
||
223 | 5 | if ($this->isDebugMode()) { |
|
224 | // log a message in debug mode |
||
225 | 2 | $this->getSystemLogger()->debug( |
|
226 | 2 | $this->appendExceptionSuffix( |
|
227 | 2 | sprintf( |
|
228 | 2 | 'Found attribute with attribute code %s', |
|
229 | $attributeCode |
||
230 | ) |
||
231 | ) |
||
232 | ); |
||
233 | } |
||
234 | } |
||
235 | |||
236 | // if yes, load the attribute by its code |
||
237 | 5 | $attribute = $attributes[$attributeCode]; |
|
238 | |||
239 | // load the backend type => to find the apropriate entity |
||
240 | 5 | $backendType = $attribute[MemberNames::BACKEND_TYPE]; |
|
241 | 5 | if ($backendType === null) { |
|
242 | // log a message in debug mode |
||
243 | 1 | $this->getSystemLogger()->warning( |
|
244 | 1 | $this->appendExceptionSuffix( |
|
245 | 1 | sprintf( |
|
246 | 1 | 'Found EMTPY backend type for attribute %s', |
|
247 | $attributeCode |
||
248 | ) |
||
249 | ) |
||
250 | ); |
||
251 | // stop processing |
||
252 | 1 | continue; |
|
253 | } |
||
254 | |||
255 | // do nothing on static backend type |
||
256 | 4 | if ($backendType === BackendTypeKeys::BACKEND_TYPE_STATIC) { |
|
257 | 1 | continue; |
|
258 | } |
||
259 | |||
260 | // query whether or not we've found a supported backend type |
||
261 | 3 | if (isset($backendTypes[$backendType])) { |
|
262 | // initialize attribute ID/code and backend type |
||
263 | 2 | $this->attributeId = $attribute[MemberNames::ATTRIBUTE_ID]; |
|
264 | 2 | $this->attributeCode = $attributeCode; |
|
265 | 2 | $this->backendType = $backendType; |
|
266 | |||
267 | // initialize the persist method for the found backend type |
||
268 | 2 | list ($persistMethod, , $deleteMethod) = $backendTypes[$backendType]; |
|
269 | |||
270 | // set the attribute value |
||
271 | 2 | $this->attributeValue = $attributeValue; |
|
272 | |||
273 | // prepare/initialize the attribute value |
||
274 | 2 | $value = $this->initializeAttribute($this->prepareAttributes()); |
|
275 | |||
276 | // query whether or not the entity's value has to be persisted or deleted. if the value is |
||
277 | // an empty string and the status is UPDATE, then the value exists and has to be deleted |
||
278 | 2 | if ($value[MemberNames::VALUE] === '' && $value[EntityStatus::MEMBER_NAME] === EntityStatus::STATUS_UPDATE) { |
|
279 | $this->$deleteMethod(array(MemberNames::VALUE_ID => $value[MemberNames::VALUE_ID])); |
||
280 | 2 | } elseif ($value[MemberNames::VALUE] !== '' && $value[MemberNames::VALUE] !== null) { |
|
281 | 1 | $this->$persistMethod($value); |
|
282 | } else { |
||
283 | // log a debug message, because this should never happen |
||
284 | 1 | $this->getSubject()->getSystemLogger()->debug(sprintf('Found empty value for attribute "%s"', $attributeCode)); |
|
285 | } |
||
286 | |||
287 | // continue with the next value |
||
288 | 2 | continue; |
|
289 | } |
||
290 | |||
291 | // log the debug message |
||
292 | 1 | $this->getSystemLogger()->debug( |
|
293 | 1 | $this->getSubject()->appendExceptionSuffix( |
|
294 | 1 | sprintf( |
|
295 | 1 | 'Found invalid backend type %s for attribute %s', |
|
296 | $backendType, |
||
297 | $attributeCode |
||
298 | ) |
||
299 | ) |
||
300 | ); |
||
301 | } |
||
302 | 7 | } |
|
303 | |||
304 | /** |
||
305 | * Prepare the attributes of the entity that has to be persisted. |
||
306 | * |
||
307 | * @return array|null The prepared attributes |
||
308 | */ |
||
309 | 2 | protected function prepareAttributes() |
|
339 | |||
340 | /** |
||
341 | * Initialize the category product with the passed attributes and returns an instance. |
||
342 | * |
||
343 | * @param array $attr The category product attributes |
||
344 | * |
||
345 | * @return array The initialized category product |
||
346 | */ |
||
347 | 2 | protected function initializeAttribute(array $attr) |
|
351 | |||
352 | /** |
||
353 | * Return's the array with callbacks for the passed type. |
||
354 | * |
||
355 | * @param string $type The type of the callbacks to return |
||
356 | * |
||
357 | * @return array The callbacks |
||
358 | */ |
||
359 | 2 | protected function getCallbacksByType($type) |
|
363 | |||
364 | /** |
||
365 | * Return's mapping for the supported backend types (for the product entity) => persist methods. |
||
366 | * |
||
367 | * @return array The mapping for the supported backend types |
||
368 | */ |
||
369 | 7 | protected function getBackendTypes() |
|
373 | |||
374 | /** |
||
375 | * Return's the attributes for the attribute set of the product that has to be created. |
||
376 | * |
||
377 | * @return array The attributes |
||
378 | * @throws \Exception |
||
379 | */ |
||
380 | 7 | protected function getAttributes() |
|
384 | |||
385 | /** |
||
386 | * Intializes the existing attributes for the entity with the passed primary key. |
||
387 | * |
||
388 | * @param string $pk The primary key of the entity to load the attributes for |
||
389 | * @param integer $storeId The ID of the store view to load the attributes for |
||
390 | * |
||
391 | * @return array The entity attributes |
||
392 | */ |
||
393 | abstract protected function getAttributesByPrimaryKeyAndStoreId($pk, $storeId); |
||
394 | |||
395 | /** |
||
396 | * Return's the logger with the passed name, by default the system logger. |
||
397 | * |
||
398 | * @param string $name The name of the requested system logger |
||
399 | * |
||
400 | * @return \Psr\Log\LoggerInterface The logger instance |
||
401 | * @throws \Exception Is thrown, if the requested logger is NOT available |
||
402 | */ |
||
403 | abstract protected function getSystemLogger($name = LoggerKeys::SYSTEM); |
||
404 | |||
405 | /** |
||
406 | * Return's the PK to create the product => attribute relation. |
||
407 | * |
||
408 | * @return integer The PK to create the relation with |
||
409 | */ |
||
410 | abstract protected function getPrimaryKey(); |
||
411 | |||
412 | /** |
||
413 | * Return's the PK column name to create the product => attribute relation. |
||
414 | * |
||
415 | * @return string The PK column name |
||
416 | */ |
||
417 | abstract protected function getPrimaryKeyMemberName(); |
||
418 | |||
419 | /** |
||
420 | * Return's the column name that contains the primary key. |
||
421 | * |
||
422 | * @return string the column name that contains the primary key |
||
423 | */ |
||
424 | abstract protected function getPrimaryKeyColumnName(); |
||
425 | |||
426 | /** |
||
427 | * Queries whether or not the passed PK and store view code has already been processed. |
||
428 | * |
||
429 | * @param string $pk The PK to check been processed |
||
430 | * @param string $storeViewCode The store view code to check been processed |
||
431 | * |
||
432 | * @return boolean TRUE if the PK and store view code has been processed, else FALSE |
||
433 | */ |
||
434 | abstract protected function storeViewHasBeenProcessed($pk, $storeViewCode); |
||
435 | |||
436 | /** |
||
437 | * Persist's the passed varchar attribute. |
||
438 | * |
||
439 | * @param array $attribute The attribute to persist |
||
440 | * |
||
441 | * @return void |
||
442 | */ |
||
443 | abstract protected function persistVarcharAttribute($attribute); |
||
444 | |||
445 | /** |
||
446 | * Persist's the passed integer attribute. |
||
447 | * |
||
448 | * @param array $attribute The attribute to persist |
||
449 | * |
||
450 | * @return void |
||
451 | */ |
||
452 | abstract protected function persistIntAttribute($attribute); |
||
453 | |||
454 | /** |
||
455 | * Persist's the passed decimal attribute. |
||
456 | * |
||
457 | * @param array $attribute The attribute to persist |
||
458 | * |
||
459 | * @return void |
||
460 | */ |
||
461 | abstract protected function persistDecimalAttribute($attribute); |
||
462 | |||
463 | /** |
||
464 | * Persist's the passed datetime attribute. |
||
465 | * |
||
466 | * @param array $attribute The attribute to persist |
||
467 | * |
||
468 | * @return void |
||
469 | */ |
||
470 | abstract protected function persistDatetimeAttribute($attribute); |
||
471 | |||
472 | /** |
||
473 | * Persist's the passed text attribute. |
||
474 | * |
||
475 | * @param array $attribute The attribute to persist |
||
476 | * |
||
477 | * @return void |
||
478 | */ |
||
479 | abstract protected function persistTextAttribute($attribute); |
||
480 | |||
481 | /** |
||
482 | * Delete's the datetime attribute with the passed value ID. |
||
483 | * |
||
484 | * @param array $row The attributes of the entity to delete |
||
485 | * @param string|null $name The name of the prepared statement that has to be executed |
||
486 | * |
||
487 | * @return void |
||
488 | */ |
||
489 | abstract protected function deleteDatetimeAttribute(array $row, $name = null); |
||
490 | |||
491 | /** |
||
492 | * Delete's the decimal attribute with the passed value ID. |
||
493 | * |
||
494 | * @param array $row The attributes of the entity to delete |
||
495 | * @param string|null $name The name of the prepared statement that has to be executed |
||
496 | * |
||
497 | * @return void |
||
498 | */ |
||
499 | abstract protected function deleteDecimalAttribute(array $row, $name = null); |
||
500 | |||
501 | /** |
||
502 | * Delete's the integer attribute with the passed value ID. |
||
503 | * |
||
504 | * @param array $row The attributes of the entity to delete |
||
505 | * @param string|null $name The name of the prepared statement that has to be executed |
||
506 | * |
||
507 | * @return void |
||
508 | */ |
||
509 | abstract protected function deleteIntAttribute(array $row, $name = null); |
||
510 | |||
511 | /** |
||
512 | * Delete's the text attribute with the passed value ID. |
||
513 | * |
||
514 | * @param array $row The attributes of the entity to delete |
||
515 | * @param string|null $name The name of the prepared statement that has to be executed |
||
516 | * |
||
517 | * @return void |
||
518 | */ |
||
519 | abstract protected function deleteTextAttribute(array $row, $name = null); |
||
520 | |||
521 | /** |
||
522 | * Delete's the varchar attribute with the passed value ID. |
||
523 | * |
||
524 | * @param array $row The attributes of the entity to delete |
||
525 | * @param string|null $name The name of the prepared statement that has to be executed |
||
526 | * |
||
527 | * @return void |
||
528 | */ |
||
529 | abstract protected function deleteVarcharAttribute(array $row, $name = null); |
||
530 | } |
||
531 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.