Skip to content

Commit

Permalink
fix taxon attribute value import while using different locale on sylius
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGrimmChester committed Nov 23, 2023
1 parent 9b0add0 commit 76d9fff
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
53 changes: 30 additions & 23 deletions src/Processor/Category/AttributeProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
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;

class AttributeProcessor implements CategoryProcessorInterface
{
private array $taxonAttributes = [];

private array $taxonAttributeValues = [];

public static function getDefaultPriority(): int
{
return 700;
Expand All @@ -38,6 +37,7 @@ public function __construct(
private FactoryInterface $taxonAttributeValueFactory,
private TaxonAttributeTypeMatcher $taxonAttributeTypeMatcher,
private TaxonAttributeValueBuilder $taxonAttributeValueBuilder,
private SyliusAkeneoLocaleCodeProvider $syliusAkeneoLocaleCodeProvider,
) {
}

Expand All @@ -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'],
Expand All @@ -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(),
Expand Down Expand Up @@ -109,42 +108,50 @@ 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,
'localeCode' => $locale,
]);

if ($taxonAttributeValue instanceof TaxonAttributeValueInterface) {
$this->taxonAttributeValues[$taxon->getCode()][$taxonAttribute->getCode()][$locale ?? 'unknown'] = $taxonAttributeValue;
$taxonAttributeValue->setValue($value);

return $taxonAttributeValue;
return;
}

/** @var TaxonAttributeValueInterface $taxonAttributeValue */
$taxonAttributeValue = $this->taxonAttributeValueFactory->createNew();
$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);
}
}
5 changes: 5 additions & 0 deletions src/Provider/SyliusAkeneoLocaleCodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 76d9fff

Please sign in to comment.