Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/app/code/Cnc/Catalog/Setup/Patch/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Cnc/Catalog/Setup/Patch/AbstractCatalogEavAttributeDataPatch.php
<?php
/**
 * Copyright (c) 2020 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved.
 * https://opensource.org/licenses/OSL-3.0  Open Software License (OSL 3.0)
 * Hugo Herblot <[email protected]> <[email protected]>
 */

declare(strict_types=1);

namespace Cnc\Catalog\Setup\Patch;

use Exception;
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Product;
use Magento\Eav\Api\Data\AttributeFrontendLabelInterfaceFactory;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;

class AbstractCatalogEavAttributeDataPatch
{
    /**
     * @var array
     */
    public $categoryAttributes = [];
    /**
     * @var array
     */
    public $productAttributes = [];
    /**
     * @var ModuleDataSetupInterface $moduleDataSetup
     */
    private $moduleDataSetup;
    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;
    /**
     * @var EavSetup
     */
    private $eavSetup;
    /**
     * @var LoggerInterface
     */
    private $logger;
    /**
     * @var ProductAttributeRepositoryInterface
     */
    private $attributeRepository;
    /**
     * @var AttributeFrontendLabelInterfaceFactory
     */
    private $attributeFrontendLabelInterfaceFactory;
    /**
     * @var StoreManagerInterface
     */
    private $storeManager;
    private $storesData;

    /**
     * AbstractCatalogEavAttributeDataPatch constructor.
     *
     * @param EavSetupFactory $eavSetupFactory
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param LoggerInterface $logger
     * @param ProductAttributeRepositoryInterface $attributeRepository
     * @param AttributeFrontendLabelInterfaceFactory $attributeFrontendLabelInterfaceFactory
     * @param StoreManagerInterface $storeManager
     */
    public function __construct(
        EavSetupFactory $eavSetupFactory,
        ModuleDataSetupInterface $moduleDataSetup,
        LoggerInterface $logger,
        ProductAttributeRepositoryInterface $attributeRepository,
        AttributeFrontendLabelInterfaceFactory $attributeFrontendLabelInterfaceFactory,
        StoreManagerInterface $storeManager
    ) {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->moduleDataSetup = $moduleDataSetup;
        $this->logger = $logger;
        $this->attributeRepository = $attributeRepository;
        $this->attributeFrontendLabelInterfaceFactory = $attributeFrontendLabelInterfaceFactory;
        $this->storeManager = $storeManager;
    }

    public function addCategoryAttributes(): void
    {
        $this->addAttributes(Category::ENTITY, $this->categoryAttributes);
    }

    private function addAttributes(string $entityTypeId, array $attributes): void
    {
        foreach ($attributes as $attributeCode => $rawData) {
            try {
                $data = $this->prepareData($rawData, $entityTypeId);

                $this->getEavSetup()->addAttribute(
                    $entityTypeId,
                    $attributeCode,
                    $data
                );

                if ($rawData['store_labels']) {
                    $this->setStoreAttributeLabels($attributeCode, $rawData['store_labels']);
                }

                if ($entityTypeId == Product::ENTITY) {
                    $this->addAttributeToSet($attributeCode, $data['attribute_set']);
                }
            } catch (Exception $e) {
                $this->logger->critical($e->getMessage());
                $this->logger->critical(var_export($data, true));
            }
        }
    }

    /**
     * @param array $data
     * @param $entityTypeId
     *
     * @return array
     */
    private function prepareData(array $data, string $entityTypeId): array
    {
        /**
         * @see \Magento\Eav\Model\Entity\Setup\PropertyMapper::map()
         * @see \Magento\Catalog\Model\ResourceModel\Setup\PropertyMapper::map()
         */
        return [
            // Product/Category entities
            'attribute_model' => isset($data['attribute_model']) ? $data['attribute_model'] : null,
            'backend' => isset($data['backend']) ? $data['backend'] : null,
            'type' => isset($data['type']) ? $data['type'] : 'varchar',
            'table' => isset($data['table']) ? $data['table'] : null,
            'frontend' => isset($data['frontend']) ? $data['frontend'] : null,
            'input' => isset($data['input']) ? $data['input'] : 'text',
            'label' => isset($data['label']) ? $data['label'] : null,
            'frontend_class' => isset($data['frontend_class']) ? $data['frontend_class'] : null,
            'source' => isset($data['source']) ? $data['source'] : null,
            'required' => isset($data['required']) ? $data['required'] : 0,
            'user_defined' => 1,
            'default' => isset($data['default']) ? $data['default'] : null,
            'unique' => isset($data['unique']) ? $data['unique'] : 0,
            'note' => isset($data['note']) ? $data['note'] : null,
            'group' => isset($data['group']) ? $data['group'] :
                (($entityTypeId == Product::ENTITY) ? 'General' : 'General Information'),
            'sort_order' => isset($data['sort_order']) ? $data['sort_order'] : 999,
            'global' => isset($data['global']) ? $data['global'] : ScopedAttributeInterface::SCOPE_GLOBAL,

            // Product entity specific
            'input_renderer' => isset($data['input_renderer']) ? $data['frontend_input_renderer'] : null,
            'visible' => isset($data['visible']) ? $data['visible'] : null,
            'searchable' => isset($data['searchable']) ? $data['searchable'] : null,
            'filterable' => isset($data['filterable']) ? $data['filterable'] : null,
            'comparable' => isset($data['comparable']) ? $data['comparable'] : null,
            'visible_on_front' => isset($data['visible_on_front']) ? $data['visible_on_front'] : null,
            'wysiwyg_enabled' => isset($data['wysiwyg_enabled']) ? $data['wysiwyg_enabled'] : null,
            'is_html_allowed_on_front' => isset($data['is_html_allowed_on_front'])
                ? $data['is_html_allowed_on_front'] : null,
            'visible_in_advanced_search' => isset($data['visible_in_advanced_search'])
                ? $data['visible_in_advanced_search'] : null,
            'filterable_in_search' => isset($data['filterable_in_search']) ? $data['filterable_in_search'] : null,
            'used_in_product_listing' => isset($data['used_in_product_listing'])
                ? $data['used_in_product_listing'] : null,
            'used_for_sort_by' => isset($data['used_for_sort_by']) ? $data['used_for_sort_by'] : null,
            'apply_to' => isset($data['apply_to']) ? $data['apply_to'] : null,
            'position' => isset($data['position']) ? $data['position'] : null,
            'used_for_promo_rules' => isset($data['used_for_promo_rules']) ? $data['used_for_promo_rules'] : null,
            'is_used_in_grid' => isset($data['is_used_in_grid']) ? $data['is_used_in_grid'] : null,
            'is_visible_in_grid' => isset($data['is_visible_in_grid']) ? $data['is_visible_in_grid'] : null,
            'is_filterable_in_grid' => isset($data['is_filterable_in_grid']) ? $data['is_filterable_in_grid'] : null,
            'attribute_set' => isset($data['attribute_set']) ? $data['attribute_set'] : null,
            'option' => isset($data['option']) ? $data['option'] : null,
        ];
    }

    /**
     * @return EavSetup
     */
    public function getEavSetup(): EavSetup
    {
        if ($this->eavSetup === null) {
            $this->eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
        }

        return $this->eavSetup;
    }

    private function setStoreAttributeLabels(string $attributeCode, array $labelsByStore): void
    {
        try {
            $frontendLabels = [];
            $stores = $this->getStoresData();
            $attribute = $this->attributeRepository->get($attributeCode);

            foreach ($labelsByStore as $storeCode => $label) {
                if (isset($stores[$storeCode])) {
                    $frontendLabels = [
                        $this->attributeFrontendLabelInterfaceFactory->create()
                            ->setStoreId($stores[$storeCode])
                            ->setLabel($label)
                    ];
                }
            }

            if (!empty($frontendLabels)) {
                $attribute->setFrontendLabels($frontendLabels);
                $this->attributeRepository->save($attribute);
            }
        } catch (Exception $e) {
            $this->logger->critical($e->getMessage());
            $this->logger->critical(var_export($frontendLabels, true));
        }
    }

    private function getStoresData(): array
    {
        if (!$this->storesData) {
            $stores = $this->storeManager->getStores();
            foreach ($stores as $store) {
                $this->storesData[$store->getCode()] = $store->getId();
            }
        }

        return $this->storesData;
    }

    /**
     * @param string $attributeCode
     * @param array|null $productAttributeSet
     */
    private function addAttributeToSet(string $attributeCode, $productAttributeSet): void
    {
        if (isset($productAttributeSet)) {
            $groupName = isset($productAttributeSet[0]) ? $productAttributeSet[0] : '';
            $attributeSets = isset($productAttributeSet[1]) ? $productAttributeSet[1] : [];

            foreach ($attributeSets as $attributeSet) {
                try {
                    $attributeSetId = $this->getEavSetup()
                        ->getAttributeSetId(Product::ENTITY, $attributeSet);
                    $attributeGroupId = $this->getEavSetup()
                        ->getAttributeGroupId(Product::ENTITY, $attributeSetId, $groupName);
                    $attributeId = $this->getEavSetup()->getAttributeId(Product::ENTITY, $attributeCode);
                    $this->getEavSetup()
                        ->addAttributeToSet(Product::ENTITY, $attributeSetId, $attributeGroupId, $attributeId);
                } catch (Exception $e) {
                    $this->logger->critical($e->getMessage());
                    $this->logger->critical(var_export($attributeSet, true));
                }
            }
        }
    }

    public function addProductAttributes(): void
    {
        $this->addAttributes(Product::ENTITY, $this->productAttributes);
    }
}

Spamworldpro Mini