From a3187ccc3d600f9ef76ba9be2bfd5c8fce165385 Mon Sep 17 00:00:00 2001 From: Lorenzo Ruozzi Date: Wed, 22 Nov 2023 15:42:27 +0100 Subject: [PATCH] Import from Akeneo only product options with right type (#187) --- src/AttributeOptions/Importer.php | 39 +++++++++++-------- ..._attributes_or_sylius_product_options.yaml | 2 + .../AttributeOptions/ImporterTest.php | 4 ++ 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/AttributeOptions/Importer.php b/src/AttributeOptions/Importer.php index c03e3d56..ddac7f27 100644 --- a/src/AttributeOptions/Importer.php +++ b/src/AttributeOptions/Importer.php @@ -40,6 +40,8 @@ final class Importer implements ImporterInterface private const BOOLEAN_TYPE = 'pim_catalog_boolean'; + private const METRIC_TYPE = 'pim_catalog_metric'; + /** * @param RepositoryInterface $attributeRepository * @param ?FactoryInterface $productOptionValueTranslationFactory @@ -199,6 +201,9 @@ private function filterSyliusOptionCodes(ResourceCursorInterface $akeneoAttribut $akeneoAttributeCodes = []; /** @var AkeneoAttribute $akeneoAttribute */ foreach ($akeneoAttributes as $akeneoAttribute) { + if (!in_array($akeneoAttribute['type'], [self::SIMPLESELECT_TYPE, self::MULTISELECT_TYPE, self::BOOLEAN_TYPE, self::METRIC_TYPE], true)) { + continue; + } $akeneoAttributeCodes[] = $akeneoAttribute['code']; } $syliusOptions = $productOptionRepository->findByCodes($akeneoAttributeCodes); @@ -256,14 +261,7 @@ private function importOptionValues(array $akeneoAttribute, ProductOptionInterfa if ($akeneoAttribute['type'] === self::BOOLEAN_TYPE) { foreach ([true, false] as $booleanValue) { $optionValueCode = $this->getSyliusProductOptionValueCode($attributeCode, (string) $booleanValue); - $productOptionValue = null; - foreach ($option->getValues() as $value) { - if ($value->getCode() === $optionValueCode) { - $productOptionValue = $value; - - break; - } - } + $productOptionValue = $this->getProductOptionValueFromOption($option, $optionValueCode); if ($productOptionValue === null) { $productOptionValue = $this->createNewProductOptionValue($optionValueCode, $option); } @@ -276,14 +274,7 @@ private function importOptionValues(array $akeneoAttribute, ProductOptionInterfa foreach ($attributeOptions as $attributeOption) { $optionValueCode = $this->getSyliusProductOptionValueCode($attributeCode, $attributeOption['code']); - $optionValue = null; - foreach ($option->getValues() as $value) { - if ($value->getCode() === $optionValueCode) { - $optionValue = $value; - - break; - } - } + $optionValue = $this->getProductOptionValueFromOption($option, $optionValueCode); if ($optionValue === null) { $optionValue = $this->createNewProductOptionValue($optionValueCode, $option); } @@ -380,4 +371,20 @@ private function getTranslator(): TranslatorInterface return $translator; } + + private function getProductOptionValueFromOption( + ProductOptionInterface $option, + string $optionValueCode, + ): ?ProductOptionValueInterface { + $productOptionValue = null; + foreach ($option->getValues() as $value) { + if ($value->getCode() === $optionValueCode) { + $productOptionValue = $value; + + break; + } + } + + return $productOptionValue; + } } diff --git a/tests/DataFixtures/ORM/resources/Importer/AttributeOptions/it_returns_all_boolean_metric_simple_select_and_multiselect_attributes_identifiers_that_are_also_sylius_select_attributes_or_sylius_product_options.yaml b/tests/DataFixtures/ORM/resources/Importer/AttributeOptions/it_returns_all_boolean_metric_simple_select_and_multiselect_attributes_identifiers_that_are_also_sylius_select_attributes_or_sylius_product_options.yaml index ba0778d5..8a78addd 100644 --- a/tests/DataFixtures/ORM/resources/Importer/AttributeOptions/it_returns_all_boolean_metric_simple_select_and_multiselect_attributes_identifiers_that_are_also_sylius_select_attributes_or_sylius_product_options.yaml +++ b/tests/DataFixtures/ORM/resources/Importer/AttributeOptions/it_returns_all_boolean_metric_simple_select_and_multiselect_attributes_identifiers_that_are_also_sylius_select_attributes_or_sylius_product_options.yaml @@ -17,3 +17,5 @@ Sylius\Component\Product\Model\ProductOption: code: 'length' sellable: code: 'sellable' + product_option_of_wrong_type: + code: 'product_option_of_wrong_type' diff --git a/tests/Integration/AttributeOptions/ImporterTest.php b/tests/Integration/AttributeOptions/ImporterTest.php index 9747a918..0c61769e 100644 --- a/tests/Integration/AttributeOptions/ImporterTest.php +++ b/tests/Integration/AttributeOptions/ImporterTest.php @@ -304,6 +304,10 @@ public function it_updates_boolean_attribute_options_from_akeneo_to_sylius_optio */ public function it_returns_all_boolean_metric_simple_select_and_multiselect_attributes_identifiers_that_are_also_sylius_select_attributes_or_sylius_product_options(): void { + InMemoryAttributeApi::addResource(Attribute::create('product_option_of_wrong_type', [ + 'type' => AttributeType::TEXT, + ])); + $identifiers = $this->importer->getIdentifiersModifiedSince(new DateTime()); $this->assertEquals(['material', 'size', 'length', 'sellable'], $identifiers);