![]() 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/Model/Attribute/ |
<?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) * Krzysztof Majkowski <[email protected]> */ declare(strict_types=1); namespace Cnc\Catalog\Model\Attribute; use Wyomind\MsiCustomAttributes\Model\CustomAttribute; use Wyomind\MsiCustomAttributes\Model\ResourceModel\CustomAttributeDropdownOption; use Wyomind\MsiCustomAttributes\Model\CustomAttributeDropdownOptionValue; use Wyomind\MsiCustomAttributes\Model\CustomAttributeDropdownOptionValueFactory; use Wyomind\MsiCustomAttributes\Model\CustomAttributeFactory; use Wyomind\MsiCustomAttributes\Model\CustomAttributeValue; use Wyomind\MsiCustomAttributes\Model\CustomAttributeValueFactory; use Wyomind\MsiCustomAttributes\Model\ResourceModel\CustomAttribute as CustomAttributeResource; use Wyomind\MsiCustomAttributes\Model\ResourceModel\CustomAttributeDropdownOptionValue as CustomAttributeDropdownOptionValueResource; use Wyomind\MsiCustomAttributes\Model\ResourceModel\CustomAttributeValue as CustomAttributeValueResource; use Wyomind\MsiCustomAttributes\Model\ResourceModel\CustomAttributeValue\Collection as CustomAttributeValueCollection; use Wyomind\MsiCustomAttributes\Model\ResourceModel\CustomAttributeValue\CollectionFactory as CustomAttributeValueCollectionFactory; use Wyomind\MsiCustomAttributes\Model\ResourceModel\CustomAttributeDropdownOptionValue\Collection as CustomAttributeDropdownOptionValueCollection; use Wyomind\MsiCustomAttributes\Model\ResourceModel\CustomAttributeDropdownOptionValue\CollectionFactory as CustomAttributeDropdownOptionValueCollectionFactory; class MsiAttributes { /** * @var CustomAttributeValueFactory */ private $customAttributeValueFactory; /** * @var CustomAttributeValueResource */ private $customAttributeValueResource; /** * @var CustomAttributeValueCollectionFactory */ private $customAttributeValueCollectionFactory; /** * @var CustomAttributeFactory */ private $customAttributeFactory; /** * @var CustomAttributeResource */ private $customAttributeResource; /** * @var CustomAttributeDropdownOptionValueFactory */ private $customAttributeDropdownOptionValueFactory; /** * @var CustomAttributeDropdownOptionValueResource */ private $customAttributeDropdownOptionValueResource; /** * @var CustomAttributeDropdownOptionValueCollectionFactory */ private $customAttributeDropdownOptionValueCollectionFactory; /** * @var array */ private $cacheInstance = []; public function __construct( CustomAttributeFactory $customAttributeFactory, CustomAttributeResource $customAttributeResource, CustomAttributeValueFactory $customAttributeValueFactory, CustomAttributeValueResource $customAttributeValueResource, CustomAttributeValueCollectionFactory $customAttributeValueCollectionFactory, CustomAttributeDropdownOptionValueFactory $customAttributeDropdownOptionValueFactory, CustomAttributeDropdownOptionValueResource $customAttributeDropdownOptionValueResource, CustomAttributeDropdownOptionValueCollectionFactory $customAttributeDropdownOptionValueCollectionFactory ) { $this->customAttributeValueFactory = $customAttributeValueFactory; $this->customAttributeValueResource = $customAttributeValueResource; $this->customAttributeValueCollectionFactory = $customAttributeValueCollectionFactory; $this->customAttributeFactory = $customAttributeFactory; $this->customAttributeResource = $customAttributeResource; $this->customAttributeDropdownOptionValueFactory = $customAttributeDropdownOptionValueFactory; $this->customAttributeDropdownOptionValueResource = $customAttributeDropdownOptionValueResource; $this->customAttributeDropdownOptionValueCollectionFactory = $customAttributeDropdownOptionValueCollectionFactory; } /** * @param $attributeCode * @return int */ public function getAttributeId($attributeCode): int { $key = implode('_', ['custom-attribute-id', $attributeCode]); if (isset($this->cacheInstance[$key])) { return $this->cacheInstance[$key]; } $keyAttribute = implode('_', ['custom-attribute', $attributeCode]); if (isset($this->cacheInstance[$keyAttribute])) { $id = $this->cacheInstance[$keyAttribute]->getId(); $this->cacheInstance[$key] = (int) $id; return (int) $id; } $customAttribute = $this->customAttributeFactory->create(); $this->customAttributeResource->load($customAttribute, $attributeCode, 'code'); $this->cacheInstance[$key] = (int) $customAttribute->getId(); return $this->cacheInstance[$key]; } /** * @param $attributeCode * @return CustomAttribute|null */ public function getAttribute($attributeCode): ?CustomAttribute { $key = implode('_', ['custom-attribute', $attributeCode]); if (!isset($this->cacheInstance[$key])) { $customAttribute = $this->customAttributeFactory->create(); $this->customAttributeResource->load($customAttribute, $attributeCode, 'code'); $customAttribute->load($customAttribute->getId()); $this->cacheInstance[$key] = $customAttribute; } return isset($this->cacheInstance[$key]) ? $this->cacheInstance[$key] : null; } /** * @param $attributeId * @param $sku * @param $sourceCode * @return CustomAttributeValue|null */ public function getAttributeValue($attributeId, $sku, $sourceCode): ?CustomAttributeValue { $key = implode('_', ['custom-attribute-value', $attributeId, $sku, $sourceCode]); if (!isset($this->cacheInstance[$key])) { /** @var CustomAttributeValueCollection $collection */ $collection = $this->customAttributeValueCollectionFactory->create(); $collection ->addFilter('attribute_id', $attributeId) ->addFilter('sku', $sku) ->addFilter('source_code', $sourceCode); /** @var CustomAttributeValue $item */ $item = $collection->getFirstItem(); $this->cacheInstance[$key] = $item; } return isset($this->cacheInstance[$key]) ? $this->cacheInstance[$key] : null; } /** * @param $valueId * @return CustomAttributeValue|null */ public function getAttributeValueById($valueId): ?CustomAttributeValue { $key = implode('_', ['custom-attribute-value-by-id', $valueId]); if (!isset($this->cacheInstance[$key])) { /** @var CustomAttributeValueCollection $collection */ $collection = $this->customAttributeValueCollectionFactory->create(); $collection ->addFilter('value_id', $valueId); /** @var CustomAttributeValue $item */ $item = $collection->getFirstItem(); $this->cacheInstance[$key] = $item; } return isset($this->cacheInstance[$key]) ? $this->cacheInstance[$key] : null; } /** * @param bool $asArray * @return array */ public function getAttributeValues($asArray = true): array { /** @var CustomAttributeValueCollection $collection */ $collection = $this->customAttributeValueCollectionFactory->create(); return ($asArray ? $collection->toArray() : $collection); } /** * @param string|null $optionId * @param int $storeId * @return CustomAttributeDropdownOptionValue|null */ public function getDropdownValue(?string $optionId, $storeId = 0): ?CustomAttributeDropdownOptionValue { $key = implode('_', ['custom-attribute-dropdown-option-value', $optionId]); if (!isset($this->cacheInstance[$key])) { $dropdownValue = $this->customAttributeDropdownOptionValueFactory->create(); $this->customAttributeDropdownOptionValueResource->load($dropdownValue, $optionId, 'option_id'); $this->cacheInstance[$key] = $dropdownValue; } return isset($this->cacheInstance[$key]) ? $this->cacheInstance[$key] : null; } /** * @param int $attributeId * @param int $storeId * @return array|mixed */ public function getDropdownOptions(int $attributeId, int $storeId = 0) { $key = implode('_', ['custom-attribute-dropdown-option-options-', $attributeId, '-', $storeId]); if (!isset($this->cacheInstance[$key])) { $connection = $this->customAttributeResource->getConnection(); $select = $connection->select() ->from(CustomAttributeDropdownOption::MSI_CUSTOM_ATTRIBUTE_DROPDOWN_OPTION) ->columns(['option_id']) ->where("attribute_id ='" . $attributeId . "'"); $optionsId = $connection->fetchCol($select); if (is_array($optionsId) && count($optionsId)) { /** @var CustomAttributeDropdownOptionValueCollection $collection */ $collection = $this->customAttributeDropdownOptionValueCollectionFactory->create(); $where = "option_id IN (" . implode(',', $optionsId) . ") AND store_id = '" . $storeId . "'"; $collection->getSelect()->where($where); $data = []; /** @var CustomAttributeDropdownOptionValue $item */ foreach ($collection as $item) { $data[$item->getData('value')] = $item->getData('option_id'); } $this->cacheInstance[$key] = $data; } } return isset($this->cacheInstance[$key]) ? $this->cacheInstance[$key] : []; } }