Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.17'
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Oct 30, 2023
2 parents 871dbd4 + 203dcd6 commit 0e0dcd9
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/Product/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,15 @@ public function import(string $identifier): void

$this->validator->validate($productVariant);

$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);
}

/** @psalm-return array<array-key, string> */
Expand Down Expand Up @@ -159,21 +162,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;
}
Expand Down Expand Up @@ -282,11 +285,13 @@ public function reconcile(array $identifiersToReconcileWith): void

$product->setEnabled(false);

$this->dispatchPreEvent($product, '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($product, 'update');
$this->dispatchPostEvent($productVariantToDisable, 'product_variant', 'update');
$this->dispatchPostEvent($product, 'product', 'update');
}
}

Expand Down

0 comments on commit 0e0dcd9

Please sign in to comment.