Skip to content

Commit dadbe14

Browse files
committed
ACP2E-2098: avoid loading all attribute collection
1 parent cd08acf commit dadbe14

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

app/code/Magento/QuoteGraphQl/Plugin/ProductAttributesExtender.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
namespace Magento\QuoteGraphQl\Plugin;
99

1010
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory as AttributeCollectionFactory;
11+
use Magento\Eav\Model\Validator\Attribute\Code;
12+
use Magento\Framework\App\ObjectManager;
1113
use Magento\Framework\GraphQl\Query\Fields;
14+
use Magento\Framework\Validator\ValidateException;
1215
use Magento\Quote\Model\Quote\Config as QuoteConfig;
1316

1417
/**
@@ -26,16 +29,44 @@ class ProductAttributesExtender
2629
*/
2730
private $attributeCollectionFactory;
2831

32+
/**
33+
* @var Code
34+
*/
35+
private Code $attributeCodeValidator;
36+
2937
/**
3038
* @param Fields $fields
3139
* @param AttributeCollectionFactory $attributeCollectionFactory
40+
* @param Code|null $attributeCodeValidator
3241
*/
3342
public function __construct(
3443
Fields $fields,
35-
AttributeCollectionFactory $attributeCollectionFactory
44+
AttributeCollectionFactory $attributeCollectionFactory,
45+
Code $attributeCodeValidator = null
3646
) {
3747
$this->fields = $fields;
3848
$this->attributeCollectionFactory = $attributeCollectionFactory;
49+
$this->attributeCodeValidator = $attributeCodeValidator ?? ObjectManager::getInstance()->get(Code::class);
50+
}
51+
52+
/**
53+
* Get only attribute code that pass validation
54+
*
55+
* @return array
56+
*/
57+
private function getValidatedAttributeCodes(): array
58+
{
59+
return array_filter($this->fields->getFieldsUsedInQuery(), [$this,'validateAttributeCode']);
60+
}
61+
62+
/**
63+
* @param string|int $code
64+
* @return bool
65+
* @throws ValidateException
66+
*/
67+
private function validateAttributeCode(string|int $code)
68+
{
69+
return $this->attributeCodeValidator->isValid((string)$code);
3970
}
4071

4172
/**
@@ -51,7 +82,7 @@ public function afterGetProductAttributes(QuoteConfig $subject, array $result):
5182
$attributeCollection = $this->attributeCollectionFactory->create()
5283
->removeAllFieldsFromSelect()
5384
->addFieldToSelect('attribute_code')
54-
->setCodeFilter($this->fields->getFieldsUsedInQuery())
85+
->setCodeFilter($this->getValidatedAttributeCodes())
5586
->load();
5687
$attributes = $attributeCollection->getColumnValues('attribute_code');
5788

0 commit comments

Comments
 (0)