Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix taxon attribute value import while using different locale on sylius #176

Merged
merged 2 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Fixture/ProductConfigurationFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
$optionsNode
->children()
->scalarNode('akeneo_sylius_enabled_channels_attribute')->defaultNull()->end()
->scalarNode('akeneo_price_attribute')->end()
->scalarNode('akeneo_price_attribute')->defaultNull()->end()
->booleanNode('regenerate_url')->defaultFalse()->end()
->booleanNode('import_media_files')->defaultFalse()->end()
->arrayNode('images_attributes')
Expand Down
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
Loading