![]() 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/vendor/magento/module-catalog/Model/Category/Link/ |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace Magento\Catalog\Model\Category\Link; use Magento\Catalog\Api\Data\CategoryLinkInterface; use Magento\Framework\EntityManager\Operation\ExtensionInterface; /** * Save handler for catalog product link. */ class SaveHandler implements ExtensionInterface { /** * @var \Magento\Catalog\Model\ResourceModel\Product\CategoryLink */ private $productCategoryLink; /** * @var \Magento\Framework\EntityManager\HydratorPool */ private $hydratorPool; /** * SaveHandler constructor. * * @param \Magento\Catalog\Model\ResourceModel\Product\CategoryLink $productCategoryLink * @param \Magento\Framework\EntityManager\HydratorPool $hydratorPool */ public function __construct( \Magento\Catalog\Model\ResourceModel\Product\CategoryLink $productCategoryLink, \Magento\Framework\EntityManager\HydratorPool $hydratorPool ) { $this->productCategoryLink = $productCategoryLink; $this->hydratorPool = $hydratorPool; } /** * Execute * * @param object $entity * @param array $arguments * @return object * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute($entity, $arguments = []) { $entity->setIsChangedCategories(false); $extensionAttributes = $entity->getExtensionAttributes(); if ($extensionAttributes === null && !$entity->hasCategoryIds()) { return $entity; } $modelCategoryLinks = $this->getCategoryLinksPositions($entity); $dtoCategoryLinks = $extensionAttributes->getCategoryLinks(); if ($dtoCategoryLinks !== null) { $hydrator = $this->hydratorPool->getHydrator(CategoryLinkInterface::class); $dtoCategoryLinks = array_map(function ($categoryLink) use ($hydrator) { return $hydrator->extract($categoryLink) ; }, $dtoCategoryLinks); $processLinks = $this->mergeCategoryLinks($dtoCategoryLinks, $modelCategoryLinks); } else { $processLinks = $modelCategoryLinks; } $affectedCategoryIds = $this->productCategoryLink->saveCategoryLinks($entity, $processLinks); if (!empty($affectedCategoryIds)) { $entity->setAffectedCategoryIds($affectedCategoryIds); $entity->setIsChangedCategories(true); } return $entity; } /** * Get category links positions * * @param object $entity * @return array */ private function getCategoryLinksPositions($entity) { $result = []; $currentCategoryLinks = $this->productCategoryLink->getCategoryLinks($entity, $entity->getCategoryIds()); foreach ($entity->getCategoryIds() as $categoryId) { $key = array_search($categoryId, array_column($currentCategoryLinks, 'category_id')); if ($key === false) { $result[] = ['category_id' => (int)$categoryId, 'position' => 0]; } else { $result[] = $currentCategoryLinks[$key]; } } return $result; } /** * Merge category links * * @param array $newCategoryPositions * @param array $oldCategoryPositions * @return array */ private function mergeCategoryLinks($newCategoryPositions, $oldCategoryPositions) { if (empty($newCategoryPositions)) { return []; } $categoryPositions = array_combine(array_column($oldCategoryPositions, 'category_id'), $oldCategoryPositions); foreach ($newCategoryPositions as $newCategoryPosition) { $categoryId = $newCategoryPosition['category_id']; if (!isset($categoryPositions[$categoryId])) { $categoryPositions[$categoryId] = ['category_id' => $categoryId]; } $categoryPositions[$categoryId]['position'] = $newCategoryPosition['position']; } $result = array_values($categoryPositions); return $result; } }