AttributeLoader.php 2.94 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Eav\Model\Entity;

use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\ObjectManagerInterface;

/**
 * Attribute loader
 */
class AttributeLoader implements AttributeLoaderInterface
{
    /**
     * Default attributes
     *
     * @var array
     */
    private $defaultAttributes = [];

    /**
     * @var ObjectManagerInterface
     */
    protected $objectManager;

    /**
     * @var Config
     */
    private $config;

    /**
     * Constructor
     *
     * @param Config $config
     * @param ObjectManagerInterface $objectManager
     */
    public function __construct(
        Config $config,
        ObjectManagerInterface $objectManager
    ) {
        $this->config = $config;
        $this->objectManager = $objectManager;
    }

    /**
     * Retrieve configuration for all attributes
     *
     * @param AbstractEntity $resource
     * @param DataObject|null $object
     * @return AbstractEntity
     * @throws LocalizedException
     */
    public function loadAllAttributes(AbstractEntity $resource, DataObject $object = null)
    {
        $attributes = $this->config->getEntityAttributes($resource->getEntityType(), $object);
        $attributeCodes = array_keys($attributes);
        /**
         * Check and init default attributes
         */
        $defaultAttributesCodes = array_diff($resource->getDefaultAttributes(), $attributeCodes);

        $resource->unsetAttributes();

        foreach ($defaultAttributesCodes as $attributeCode) {
            $resource->addAttribute($this->_getDefaultAttribute($resource, $attributeCode), $object);
        }
        foreach ($attributes as $attributeCode => $attribute) {
            $resource->addAttribute($attribute, $object);
        }
        return $resource;
    }

    /**
     * Return default static virtual attribute that doesn't exists in EAV attributes
     *
     * @param AbstractEntity $resource
     * @param string $attributeCode
     * @return Attribute
     */
    protected function _getDefaultAttribute(AbstractEntity $resource, $attributeCode)
    {
        $entityTypeId = $resource->getEntityType()->getId();
        if (!isset($this->defaultAttributes[$entityTypeId][$attributeCode])) {
            $attribute = $this->objectManager->create($resource->getEntityType()->getAttributeModel())
                ->setAttributeCode($attributeCode)
                ->setBackendType(AbstractAttribute::TYPE_STATIC)
                ->setIsGlobal(1)
                ->setEntityType($resource->getEntityType())
                ->setEntityTypeId($resource->getEntityType()->getId());
            $this->defaultAttributes[$entityTypeId][$attributeCode] = $attribute;
        }
        return $this->defaultAttributes[$entityTypeId][$attributeCode];
    }
}