diff --git a/src/Processor/Category/AttributeProcessor.php b/src/Processor/Category/AttributeProcessor.php index 7698476d..afd585c1 100644 --- a/src/Processor/Category/AttributeProcessor.php +++ b/src/Processor/Category/AttributeProcessor.php @@ -15,6 +15,7 @@ use Synolia\SyliusAkeneoPlugin\Entity\TaxonAttributeInterface; use Synolia\SyliusAkeneoPlugin\Entity\TaxonAttributeValueInterface; use Synolia\SyliusAkeneoPlugin\Exceptions\UnsupportedAttributeTypeException; +use Synolia\SyliusAkeneoPlugin\Provider\SyliusAkeneoLocaleCodeProvider; use Synolia\SyliusAkeneoPlugin\TypeMatcher\TaxonAttribute\TaxonAttributeTypeMatcher; use Webmozart\Assert\Assert; @@ -22,8 +23,6 @@ class AttributeProcessor implements CategoryProcessorInterface { private array $taxonAttributes = []; - private array $taxonAttributeValues = []; - public static function getDefaultPriority(): int { return 700; @@ -38,6 +37,7 @@ public function __construct( private FactoryInterface $taxonAttributeValueFactory, private TaxonAttributeTypeMatcher $taxonAttributeTypeMatcher, private TaxonAttributeValueBuilder $taxonAttributeValueBuilder, + private SyliusAkeneoLocaleCodeProvider $syliusAkeneoLocaleCodeProvider, ) { } @@ -50,12 +50,6 @@ public function process(TaxonInterface $taxon, array $resource): void $attributeValue['type'], ); - $taxonAttributeValue = $this->getTaxonAttributeValues( - $taxon, - $taxonAttribute, - $attributeValue['locale'], - ); - $value = $this->taxonAttributeValueBuilder->build( $attributeValue['attribute_code'], $attributeValue['type'], @@ -64,7 +58,12 @@ public function process(TaxonInterface $taxon, array $resource): void $attributeValue['data'], ); - $taxonAttributeValue->setValue($value); + $this->getTaxonAttributeValues( + $attributeValue, + $taxon, + $taxonAttribute, + $value, + ); } catch (UnsupportedAttributeTypeException $e) { $this->logger->warning($e->getMessage(), [ 'trace' => $e->getTrace(), @@ -109,21 +108,31 @@ private function getTaxonAttributes(string $attributeCode, string $type): TaxonA } private function getTaxonAttributeValues( + array $attributeValue, TaxonInterface $taxon, TaxonAttributeInterface $taxonAttribute, - ?string $locale, - ): TaxonAttributeValueInterface { + mixed $value, + ): void { Assert::string($taxon->getCode()); Assert::string($taxonAttribute->getCode()); - if ( - array_key_exists($taxon->getCode(), $this->taxonAttributeValues) && - array_key_exists($taxonAttribute->getCode(), $this->taxonAttributeValues[$taxon->getCode()]) && - array_key_exists($locale ?? 'unknown', $this->taxonAttributeValues[$taxon->getCode()][$taxonAttribute->getCode()]) - ) { - return $this->taxonAttributeValues[$taxon->getCode()][$taxonAttribute->getCode()][$locale ?? 'unknown']; + if (null === $attributeValue['locale']) { + $this->handleValue($taxon, $taxonAttribute, null, $value); + + return; } + foreach ($this->syliusAkeneoLocaleCodeProvider->getSyliusLocales($attributeValue['locale']) as $syliusLocale) { + $this->handleValue($taxon, $taxonAttribute, $syliusLocale, $value); + } + } + + private function handleValue( + TaxonInterface $taxon, + TaxonAttributeInterface $taxonAttribute, + ?string $locale, + mixed $value, + ): void { $taxonAttributeValue = $this->taxonAttributeValueRepository->findOneBy([ 'subject' => $taxon, 'attribute' => $taxonAttribute, @@ -131,9 +140,9 @@ private function getTaxonAttributeValues( ]); if ($taxonAttributeValue instanceof TaxonAttributeValueInterface) { - $this->taxonAttributeValues[$taxon->getCode()][$taxonAttribute->getCode()][$locale ?? 'unknown'] = $taxonAttributeValue; + $taxonAttributeValue->setValue($value); - return $taxonAttributeValue; + return; } /** @var TaxonAttributeValueInterface $taxonAttributeValue */ @@ -141,10 +150,8 @@ private function getTaxonAttributeValues( $taxonAttributeValue->setAttribute($taxonAttribute); $taxonAttributeValue->setTaxon($taxon); $taxonAttributeValue->setLocaleCode($locale); - $this->entityManager->persist($taxonAttributeValue); + $taxonAttributeValue->setValue($value); - $this->taxonAttributeValues[$taxon->getCode()][$taxonAttribute->getCode()][$locale ?? 'unknown'] = $taxonAttributeValue; - - return $taxonAttributeValue; + $this->entityManager->persist($taxonAttributeValue); } } diff --git a/src/Provider/SyliusAkeneoLocaleCodeProvider.php b/src/Provider/SyliusAkeneoLocaleCodeProvider.php index 9b882b11..70317e8e 100644 --- a/src/Provider/SyliusAkeneoLocaleCodeProvider.php +++ b/src/Provider/SyliusAkeneoLocaleCodeProvider.php @@ -60,6 +60,11 @@ public function getAkeneoLocale(string $syliusLocale): string return $this->akeneoSyliusLocaleMapper->unmap($syliusLocale); } + public function getSyliusLocales(string $akeneoLocale): array + { + return $this->akeneoSyliusLocaleMapper->map($akeneoLocale); + } + public function isActiveLocale(string $locale): bool { $locales = $this->getUsedLocalesOnBothPlatforms();