-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '2.4-develop' of https://github.com/mage-os/mirror-magento2
- Loading branch information
Showing
20 changed files
with
751 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
app/code/Magento/CatalogImportExport/Model/Import/Product/UniqueAttributeValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
/************************************************************************ | ||
* | ||
* Copyright 2024 Adobe | ||
* All Rights Reserved. | ||
* | ||
* NOTICE: All information contained herein is, and remains | ||
* the property of Adobe and its suppliers, if any. The intellectual | ||
* and technical concepts contained herein are proprietary to Adobe | ||
* and its suppliers and are protected by all applicable intellectual | ||
* property laws, including trade secret and copyright laws. | ||
* Dissemination of this information or reproduction of this material | ||
* is strictly forbidden unless prior written permission is obtained | ||
* from Adobe. | ||
* ************************************************************************ | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogImportExport\Model\Import\Product; | ||
|
||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\CatalogImportExport\Model\Import\Product; | ||
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; | ||
use Magento\Framework\EntityManager\MetadataPool; | ||
use Magento\Framework\Exception\LocalizedException; | ||
|
||
class UniqueAttributeValidator | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private array $cache = []; | ||
|
||
/** | ||
* @param MetadataPool $metadataPool | ||
* @param SkuStorage $skuStorage | ||
*/ | ||
public function __construct( | ||
private readonly MetadataPool $metadataPool, | ||
private readonly SkuStorage $skuStorage | ||
) { | ||
} | ||
|
||
/** | ||
* Check if provided value is unique for the attribute | ||
* | ||
* @param Product $context | ||
* @param string $attributeCode | ||
* @param string $sku | ||
* @param string $value | ||
* @return bool | ||
* @throws \Exception | ||
*/ | ||
public function isValid(Product $context, string $attributeCode, string $sku, string $value): bool | ||
{ | ||
$cacheKey = strtolower($attributeCode); | ||
if (!isset($this->cache[$cacheKey])) { | ||
$this->cache[$cacheKey] = $this->load($context, $attributeCode); | ||
} | ||
$entityData = $this->skuStorage->get($sku); | ||
$id = null; | ||
if ($entityData !== null) { | ||
$id = $entityData[$this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()]; | ||
} | ||
return !isset($this->cache[$cacheKey][$value]) || in_array($id, $this->cache[$cacheKey][$value]); | ||
} | ||
|
||
/** | ||
* Load attribute values with corresponding entity ids | ||
* | ||
* @param Product $context | ||
* @param string $attributeCode | ||
* @return array | ||
* @throws LocalizedException | ||
*/ | ||
private function load(Product $context, string $attributeCode): array | ||
{ | ||
/** @var AbstractAttribute $attributeObject */ | ||
$attributeObject = $context->retrieveAttributeByCode($attributeCode); | ||
if ($attributeObject->isStatic()) { | ||
return []; | ||
} | ||
$metadata = $this->metadataPool->getMetadata(ProductInterface::class); | ||
$connection = $context->getConnection(); | ||
$idField = $metadata->getLinkField(); | ||
$select = $connection->select() | ||
->from( | ||
$attributeObject->getBackend()->getTable(), | ||
['value', $idField] | ||
) | ||
->where( | ||
'attribute_id = :attribute_id' | ||
); | ||
$result = []; | ||
foreach ($connection->fetchAll($select, ['attribute_id' => $attributeObject->getId()]) as $row) { | ||
$result[$row['value']][] = $row[$idField]; | ||
} | ||
return $result; | ||
} | ||
|
||
/** | ||
* Clear cached attribute values | ||
* | ||
* @return void | ||
*/ | ||
public function clearCache(): void | ||
{ | ||
$this->cache = []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.