src/EventSubscriber/OrderSubscriber.php line 29

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Order;
  4. use App\Event\Order\OrderCreatedEvent;
  5. use App\Event\Order\OrderCancelledEvent;
  6. use App\Event\Order\OrderInitEvent;
  7. use App\Event\Order\OrderUpdatedEvent;
  8. use App\Service\NotificationService;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class OrderSubscriber implements EventSubscriberInterface
  11. {
  12.     public function __construct(private readonly NotificationService $notificationService){}
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             OrderCreatedEvent::class => 'onCreated',
  17.             OrderInitEvent::class => 'onInit',
  18.             OrderUpdatedEvent::class => 'onUpdated',
  19.             OrderCancelledEvent::class => 'onCancelled',
  20.         ];
  21.     }
  22.     public function onCreated(OrderCreatedEvent $event): void
  23.     {
  24.         $order $event->getOrder();
  25.         if ($order instanceof Order) {
  26.             $this->notificationService->notifyUser(
  27.                 $order->getAuthor(),
  28.                 'Commande ajouté',
  29.                 sprintf(
  30.                     "Vous venez d'ajouter la commande <strong>%s</strong>",
  31.                     $order->getReference()
  32.                 ),
  33.                 $order
  34.             );
  35.         }
  36.     }
  37.     public function onInit(OrderInitEvent $event): void
  38.     {
  39.         $order $event->getOrder();
  40.         if ($order instanceof Order) {
  41.             $this->notificationService->notifyUser(
  42.                 $order->getAuthor(),
  43.                 'Commande initié',
  44.                 sprintf(
  45.                     "Vous venez d'initier la commande <strong>%s</strong>",
  46.                     $order->getReference()
  47.                 ),
  48.                 $order
  49.             );
  50.         }
  51.     }
  52.     public function onUpdated(OrderUpdatedEvent $event): void
  53.     {
  54.         $order $event->getOrder();
  55.         if ($order instanceof Order) {
  56.             $this->notificationService->notifyUser(
  57.                 $order->getAuthor(),
  58.                 'Commande modifié',
  59.                 sprintf(
  60.                     "Vous venez de modifier la commande <strong>%s</strong>",
  61.                     $order->getReference()
  62.                 ),
  63.                 $order
  64.             );
  65.         }
  66.     }
  67.     public function onCancelled(OrderCancelledEvent $event): void
  68.     {
  69.         $order $event->getOrder();
  70.         if ($order instanceof Order) {
  71.             $this->notificationService->notifyUser(
  72.                 $order->getAuthor(),
  73.                 'Commande annulé',
  74.                 sprintf(
  75.                     "Vous venez d'annuler la commande <strong>%s</strong>",
  76.                     $order->getReference()
  77.                 ),
  78.                 $order
  79.             );
  80.         }
  81.     }
  82. }