|
8 | 8 | use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
|
9 | 9 | use Magento\Eav\Model\Entity\Type;
|
10 | 10 | use Magento\Eav\Model\ResourceModel\Attribute\DefaultEntityAttributes\ProviderInterface;
|
| 11 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
11 | 12 | use Magento\Framework\App\ObjectManager;
|
12 | 13 | use Magento\Framework\Exception\LocalizedException;
|
13 | 14 | use Magento\Framework\Model\AbstractModel;
|
14 | 15 | use Magento\Framework\Serialize\SerializerInterface;
|
15 |
| -use Magento\Framework\App\Config\ScopeConfigInterface; |
16 | 16 |
|
17 | 17 | /**
|
| 18 | + * EAV config model. |
| 19 | + * |
18 | 20 | * @api
|
| 21 | + * @SuppressWarnings(PHPMD.TooManyFields) |
19 | 22 | * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
20 | 23 | * @since 100.0.2
|
21 | 24 | */
|
@@ -552,50 +555,9 @@ public function getAttribute($entityType, $code)
|
552 | 555 | }
|
553 | 556 |
|
554 | 557 | if ($this->scopeConfig->getValue(self::XML_PATH_CACHE_USER_DEFINED_ATTRIBUTES)) {
|
555 |
| - $cacheKey = self::ATTRIBUTES_CACHE_ID . '-attribute-' . $entityTypeCode . '-' . $code; |
556 |
| - $attributeData = $this->isCacheEnabled() && ($attribute = $this->_cache->load($cacheKey)) |
557 |
| - ? $this->serializer->unserialize($attribute) |
558 |
| - : null; |
559 |
| - if ($attributeData) { |
560 |
| - if (isset($attributeData['attribute_id'])) { |
561 |
| - $attribute = $this->_createAttribute($entityType, $attributeData); |
562 |
| - } else { |
563 |
| - $entityType = $this->getEntityType($entityType); |
564 |
| - $attribute = $this->createAttribute($entityType->getAttributeModel()); |
565 |
| - $attribute->setAttributeCode($code); |
566 |
| - $attribute = $this->setAttributeData($attribute, $entityType); |
567 |
| - } |
568 |
| - } else { |
569 |
| - $attribute = $this->createAttributeByAttributeCode($entityType, $code); |
570 |
| - $this->_addAttributeReference( |
571 |
| - $attribute->getAttributeId(), |
572 |
| - $attribute->getAttributeCode(), |
573 |
| - $entityTypeCode |
574 |
| - ); |
575 |
| - $this->saveAttribute($attribute, $entityTypeCode, $attribute->getAttributeCode()); |
576 |
| - if ($this->isCacheEnabled()) { |
577 |
| - $this->_cache->save( |
578 |
| - $this->serializer->serialize($attribute->getData()), |
579 |
| - $cacheKey, |
580 |
| - [ |
581 |
| - \Magento\Eav\Model\Cache\Type::CACHE_TAG, |
582 |
| - \Magento\Eav\Model\Entity\Attribute::CACHE_TAG |
583 |
| - ] |
584 |
| - ); |
585 |
| - } |
586 |
| - } |
| 558 | + $attribute = $this->cacheUserDefinedAttribute($entityType, $entityTypeCode, $code); |
587 | 559 | } else {
|
588 |
| - $attributes = $this->loadAttributes($entityTypeCode); |
589 |
| - $attribute = $attributes[$code] ?? null; |
590 |
| - if (!$attribute) { |
591 |
| - $attribute = $this->createAttributeByAttributeCode($entityType, $code); |
592 |
| - $this->_addAttributeReference( |
593 |
| - $attribute->getAttributeId(), |
594 |
| - $attribute->getAttributeCode(), |
595 |
| - $entityTypeCode |
596 |
| - ); |
597 |
| - $this->saveAttribute($attribute, $entityTypeCode, $attribute->getAttributeCode()); |
598 |
| - } |
| 560 | + $attribute = $this->initUserDefinedAttribute($entityType, $entityTypeCode, $code); |
599 | 561 | }
|
600 | 562 |
|
601 | 563 | \Magento\Framework\Profiler::stop('EAV: ' . __METHOD__);
|
@@ -668,6 +630,79 @@ private function initSystemAttributes($entityType, $systemAttributes)
|
668 | 630 | return $this;
|
669 | 631 | }
|
670 | 632 |
|
| 633 | + /** |
| 634 | + * Initialize user defined attribute from cache or cache it. |
| 635 | + * |
| 636 | + * @param string $entityType |
| 637 | + * @param mixed $entityTypeCode |
| 638 | + * @param string $code |
| 639 | + * @return AbstractAttribute |
| 640 | + * @throws LocalizedException |
| 641 | + */ |
| 642 | + private function cacheUserDefinedAttribute($entityType, $entityTypeCode, $code): AbstractAttribute |
| 643 | + { |
| 644 | + $cacheKey = self::ATTRIBUTES_CACHE_ID . '-attribute-' . $entityTypeCode . '-' . $code; |
| 645 | + $attributeData = $this->isCacheEnabled() && ($attribute = $this->_cache->load($cacheKey)) |
| 646 | + ? $this->serializer->unserialize($attribute) |
| 647 | + : null; |
| 648 | + if ($attributeData) { |
| 649 | + if (isset($attributeData['attribute_id'])) { |
| 650 | + $attribute = $this->_createAttribute($entityType, $attributeData); |
| 651 | + } else { |
| 652 | + $entityType = $this->getEntityType($entityType); |
| 653 | + $attribute = $this->createAttribute($entityType->getAttributeModel()); |
| 654 | + $attribute->setAttributeCode($code); |
| 655 | + $attribute = $this->setAttributeData($attribute, $entityType); |
| 656 | + } |
| 657 | + } else { |
| 658 | + $attribute = $this->createAttributeByAttributeCode($entityType, $code); |
| 659 | + $this->_addAttributeReference( |
| 660 | + $attribute->getAttributeId(), |
| 661 | + $attribute->getAttributeCode(), |
| 662 | + $entityTypeCode |
| 663 | + ); |
| 664 | + $this->saveAttribute($attribute, $entityTypeCode, $attribute->getAttributeCode()); |
| 665 | + if ($this->isCacheEnabled()) { |
| 666 | + $this->_cache->save( |
| 667 | + $this->serializer->serialize($attribute->getData()), |
| 668 | + $cacheKey, |
| 669 | + [ |
| 670 | + \Magento\Eav\Model\Cache\Type::CACHE_TAG, |
| 671 | + \Magento\Eav\Model\Entity\Attribute::CACHE_TAG |
| 672 | + ] |
| 673 | + ); |
| 674 | + } |
| 675 | + } |
| 676 | + |
| 677 | + return $attribute; |
| 678 | + } |
| 679 | + |
| 680 | + /** |
| 681 | + * Initialize user defined attribute and save it to memory cache. |
| 682 | + * |
| 683 | + * @param mixed $entityType |
| 684 | + * @param string $entityTypeCode |
| 685 | + * @param string $code |
| 686 | + * @return AbstractAttribute|null |
| 687 | + * @throws LocalizedException |
| 688 | + */ |
| 689 | + private function initUserDefinedAttribute($entityType, $entityTypeCode, $code): ?AbstractAttribute |
| 690 | + { |
| 691 | + $attributes = $this->loadAttributes($entityTypeCode); |
| 692 | + $attribute = $attributes[$code] ?? null; |
| 693 | + if (!$attribute) { |
| 694 | + $attribute = $this->createAttributeByAttributeCode($entityType, $code); |
| 695 | + $this->_addAttributeReference( |
| 696 | + $attribute->getAttributeId(), |
| 697 | + $attribute->getAttributeCode(), |
| 698 | + $entityTypeCode |
| 699 | + ); |
| 700 | + $this->saveAttribute($attribute, $entityTypeCode, $attribute->getAttributeCode()); |
| 701 | + } |
| 702 | + |
| 703 | + return $attribute; |
| 704 | + } |
| 705 | + |
671 | 706 | /**
|
672 | 707 | * Create attribute
|
673 | 708 | *
|
|
0 commit comments