diff --git a/src/Task/AssociationType/BatchAssociationTypesTask.php b/src/Task/AssociationType/BatchAssociationTypesTask.php index c630134b..8bc48e60 100644 --- a/src/Task/AssociationType/BatchAssociationTypesTask.php +++ b/src/Task/AssociationType/BatchAssociationTypesTask.php @@ -9,10 +9,8 @@ use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; use Sylius\Component\Product\Model\ProductAssociationTypeInterface; -use Sylius\Component\Product\Model\ProductAssociationTypeTranslationInterface; use Sylius\Component\Product\Repository\ProductAssociationTypeRepositoryInterface; use Sylius\Component\Resource\Factory\FactoryInterface; -use Sylius\Component\Resource\Repository\RepositoryInterface; use Synolia\SyliusAkeneoPlugin\Exceptions\Attribute\ExcludedAttributeException; use Synolia\SyliusAkeneoPlugin\Exceptions\Attribute\InvalidAttributeException; use Synolia\SyliusAkeneoPlugin\Exceptions\NoAttributeResourcesException; @@ -20,6 +18,7 @@ use Synolia\SyliusAkeneoPlugin\Logger\Messages; use Synolia\SyliusAkeneoPlugin\Payload\Association\AssociationTypePayload; use Synolia\SyliusAkeneoPlugin\Payload\PipelinePayloadInterface; +use Synolia\SyliusAkeneoPlugin\Provider\SyliusAkeneoLocaleCodeProvider; use Synolia\SyliusAkeneoPlugin\Task\AbstractBatchTask; use Throwable; @@ -31,9 +30,8 @@ public function __construct( EntityManagerInterface $entityManager, private LoggerInterface $logger, private FactoryInterface $productAssociationTypeFactory, - private FactoryInterface $productAssociationTypeTranslationFactory, private ProductAssociationTypeRepositoryInterface $productAssociationTypeRepository, - private RepositoryInterface $productAssociationTypeTranslationRepository, + private SyliusAkeneoLocaleCodeProvider $syliusAkeneoLocaleCodeProvider, ) { parent::__construct($entityManager); } @@ -59,6 +57,7 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte while ($results = $queryResult->fetchAll()) { foreach ($results as $result) { + /** @var array{code: string, labels: array} $resource */ $resource = json_decode($result['values'], true, 512, \JSON_THROW_ON_ERROR); try { @@ -75,7 +74,7 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte $productAssociationType->setCode($resource['code']); } - $this->addTranslations($resource, $productAssociationType); + $this->setTranslations($resource['labels'], $productAssociationType); $this->entityManager->flush(); @@ -113,30 +112,21 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte return $payload; } - private function addTranslations(array $resource, ProductAssociationTypeInterface $productAssociationType): void + private function setTranslations(array $labels, ProductAssociationTypeInterface $productAssociationType): void { - foreach ($resource['labels'] as $localeCode => $label) { - $productAssociationTypeTranslation = $this->productAssociationTypeTranslationRepository->findOneBy([ - 'translatable' => $productAssociationType, - 'locale' => $localeCode, - ]); - - if (!$productAssociationTypeTranslation instanceof ProductAssociationTypeTranslationInterface) { - $productAssociationTypeTranslation = $this->createTranslation($localeCode, $label); - } + foreach ($this->syliusAkeneoLocaleCodeProvider->getUsedLocalesOnBothPlatforms() as $usedLocalesOnBothPlatform) { + $akeneoLocale = $this->syliusAkeneoLocaleCodeProvider->getAkeneoLocale($usedLocalesOnBothPlatform); - $productAssociationType->addTranslation($productAssociationTypeTranslation); - } - } + $productAssociationType->setCurrentLocale($usedLocalesOnBothPlatform); + $productAssociationType->setFallbackLocale($usedLocalesOnBothPlatform); - private function createTranslation(string $localeCode, string $label): ProductAssociationTypeTranslationInterface - { - /** @var ProductAssociationTypeTranslationInterface $productAssociationTypeTranslation */ - $productAssociationTypeTranslation = $this->productAssociationTypeTranslationFactory->createNew(); - $productAssociationTypeTranslation->setLocale($localeCode); - $productAssociationTypeTranslation->setName($label); - $this->entityManager->persist($productAssociationTypeTranslation); + if (!isset($labels[$akeneoLocale])) { + $productAssociationType->setName(sprintf('[%s]', $productAssociationType->getCode())); - return $productAssociationTypeTranslation; + continue; + } + + $productAssociationType->setName($labels[$akeneoLocale]); + } } } diff --git a/src/Task/AssociationType/ProcessAssociationTypeTask.php b/src/Task/AssociationType/ProcessAssociationTypeTask.php index 1a04e498..040755b0 100644 --- a/src/Task/AssociationType/ProcessAssociationTypeTask.php +++ b/src/Task/AssociationType/ProcessAssociationTypeTask.php @@ -59,6 +59,7 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte ); $this->handle($payload, $page); + $this->processManager->waitForAllProcesses(); return $payload; }