diff --git a/src/EventListener/Doctrine/ProductListener.php b/src/EventSubscriber/CreateUploadProductVariantRequestSubscriber.php similarity index 65% rename from src/EventListener/Doctrine/ProductListener.php rename to src/EventSubscriber/CreateUploadProductVariantRequestSubscriber.php index 5284cdb..6b76072 100644 --- a/src/EventListener/Doctrine/ProductListener.php +++ b/src/EventSubscriber/CreateUploadProductVariantRequestSubscriber.php @@ -2,20 +2,19 @@ declare(strict_types=1); -namespace Setono\SyliusPeakPlugin\EventListener\Doctrine; +namespace Setono\SyliusPeakPlugin\EventSubscriber; -use Doctrine\Persistence\Event\LifecycleEventArgs; use Setono\SyliusPeakPlugin\Factory\UploadProductVariantRequestFactoryInterface; use Setono\SyliusPeakPlugin\Model\ProductVariantInterface; use Setono\SyliusPeakPlugin\Workflow\UploadProductVariantRequestWorkflow; +use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent; use Sylius\Component\Core\Model\ProductInterface; -use Sylius\Component\Core\Model\ProductTranslationInterface; use Sylius\Component\Core\Model\ProductVariantInterface as BaseProductVariantInterface; -use Sylius\Component\Product\Model\ProductVariantTranslationInterface; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Workflow\WorkflowInterface; use Webmozart\Assert\Assert; -final class ProductListener +final class CreateUploadProductVariantRequestSubscriber implements EventSubscriberInterface { public function __construct( private readonly UploadProductVariantRequestFactoryInterface $uploadProductVariantRequestFactory, @@ -23,30 +22,32 @@ public function __construct( ) { } - public function prePersist(LifecycleEventArgs $eventArgs): void + public static function getSubscribedEvents(): array { - $this->handle($eventArgs); + return [ + 'sylius.product.pre_create' => 'handle', + 'sylius.product.pre_update' => 'handle', + 'sylius.product_variant.pre_create' => 'handle', + 'sylius.product_variant.pre_update' => 'handle', + ]; } - public function preUpdate(LifecycleEventArgs $eventArgs): void + public function handle(ResourceControllerEvent $event): void { - $this->handle($eventArgs); - } - - private function handle(LifecycleEventArgs $eventArgs): void - { - $obj = $eventArgs->getObject(); + /** @var mixed $obj */ + $obj = $event->getSubject(); - /** @psalm-suppress UndefinedInterfaceMethod */ $variants = match (true) { $obj instanceof ProductInterface => $obj->getVariants(), - $obj instanceof ProductTranslationInterface => $obj->getTranslatable()->getVariants(), $obj instanceof ProductVariantInterface => [$obj], - $obj instanceof ProductVariantTranslationInterface => [$obj->getTranslatable()], default => [], }; - if (!is_iterable($variants) || !is_countable($variants) || count($variants) === 0) { + if (!is_countable($variants)) { + throw new \LogicException('The variants must be iterable and countable.'); + } + + if (count($variants) === 0) { return; } diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 2a29f0d..83626b0 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -6,7 +6,6 @@ - diff --git a/src/Resources/config/services/event_listener.xml b/src/Resources/config/services/event_listener.xml deleted file mode 100644 index f0b6701..0000000 --- a/src/Resources/config/services/event_listener.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - diff --git a/src/Resources/config/services/event_subscriber.xml b/src/Resources/config/services/event_subscriber.xml index 3d5a546..d7d9a38 100644 --- a/src/Resources/config/services/event_subscriber.xml +++ b/src/Resources/config/services/event_subscriber.xml @@ -29,6 +29,13 @@ + + + + + + +