From d79099271f2fb29e88b3a0dae3c2ce88e584ba7f Mon Sep 17 00:00:00 2001 From: Luca Gallinari Date: Mon, 30 Oct 2023 12:04:49 +0100 Subject: [PATCH 1/2] Dispatch product variant pre and post update/create event in Import --- src/Product/Importer.php | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Product/Importer.php b/src/Product/Importer.php index 424bc57f..0184ffcb 100644 --- a/src/Product/Importer.php +++ b/src/Product/Importer.php @@ -160,12 +160,15 @@ public function import(string $identifier): void } } - $eventName = $product->getId() ? 'update' : 'create'; - $this->dispatchPreEvent($product, $eventName); + $productEventName = $product->getId() ? 'update' : 'create'; + $productVariantEventName = $productVariant->getId() ? 'update' : 'create'; + $this->dispatchPreEvent($productVariant, 'product_variant', $productVariantEventName); + $this->dispatchPreEvent($product, 'product', $productEventName); // TODO We should handle $event->isStopped() where $event is the return value of the dispatchPreEvent method. // See \Sylius\Bundle\ResourceBundle\Controller\ResourceController. $this->productRepository->add($product); - $this->dispatchPostEvent($product, $eventName); + $this->dispatchPostEvent($productVariant, 'product_variant', $productVariantEventName); + $this->dispatchPostEvent($product, 'product', $productEventName); } /** @@ -217,21 +220,21 @@ private function getOrCreateProductFromVariantResponse(array $productVariantResp return $product; } - private function dispatchPreEvent(ResourceInterface $product, string $eventName): ResourceControllerEvent + private function dispatchPreEvent(ResourceInterface $product, string $resourceName, string $eventName): ResourceControllerEvent { $event = new ResourceControllerEvent($product); $event->setArgument(self::EVENT_AKENEO_IMPORT, true); - $this->eventDispatcher->dispatch($event, sprintf('sylius.product.pre_%s', $eventName)); + $this->eventDispatcher->dispatch($event, sprintf('sylius.%s.pre_%s', $resourceName, $eventName)); return $event; } - private function dispatchPostEvent(ResourceInterface $product, string $eventName): ResourceControllerEvent + private function dispatchPostEvent(ResourceInterface $product, string $resourceName, string $eventName): ResourceControllerEvent { $event = new ResourceControllerEvent($product); $event->setArgument(self::EVENT_AKENEO_IMPORT, true); /** @psalm-suppress InvalidArgument */ - $this->eventDispatcher->dispatch($event, sprintf('sylius.product.post_%s', $eventName)); + $this->eventDispatcher->dispatch($event, sprintf('sylius.%s.post_%s', $resourceName, $eventName)); return $event; } @@ -353,11 +356,13 @@ public function reconcile(array $identifiersToReconcileWith): void $product->setEnabled(false); - $this->dispatchPreEvent($product, 'update'); + $this->dispatchPreEvent($productVariantIdentifierToDisable, 'product_variant', 'update'); + $this->dispatchPreEvent($product, 'product', 'update'); // TODO We should handle $event->isStopped() where $event is the return value of the dispatchPreEvent method. // See \Sylius\Bundle\ResourceBundle\Controller\ResourceController. $this->productRepository->add($product); - $this->dispatchPostEvent($product, 'update'); + $this->dispatchPostEvent($productVariantIdentifierToDisable, 'product_variant', 'update'); + $this->dispatchPostEvent($product, 'product', 'update'); } } From 203dcd6d25e319e8a5b4fccbe84114655acebf21 Mon Sep 17 00:00:00 2001 From: Lorenzo Ruozzi Date: Mon, 30 Oct 2023 12:11:30 +0100 Subject: [PATCH 2/2] Apply suggestions from code review --- src/Product/Importer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Product/Importer.php b/src/Product/Importer.php index 0184ffcb..d9833278 100644 --- a/src/Product/Importer.php +++ b/src/Product/Importer.php @@ -356,12 +356,12 @@ public function reconcile(array $identifiersToReconcileWith): void $product->setEnabled(false); - $this->dispatchPreEvent($productVariantIdentifierToDisable, 'product_variant', 'update'); + $this->dispatchPreEvent($productVariantToDisable, 'product_variant', 'update'); $this->dispatchPreEvent($product, 'product', 'update'); // TODO We should handle $event->isStopped() where $event is the return value of the dispatchPreEvent method. // See \Sylius\Bundle\ResourceBundle\Controller\ResourceController. $this->productRepository->add($product); - $this->dispatchPostEvent($productVariantIdentifierToDisable, 'product_variant', 'update'); + $this->dispatchPostEvent($productVariantToDisable, 'product_variant', 'update'); $this->dispatchPostEvent($product, 'product', 'update'); } }