src/EventSubscriber/Order/NotifyAdminPrinterCancelOrErrorRequestSubscriber.php line 36

  1. <?php
  2. /*
  3.  * This file is part of the Pellipop project.
  4.  *
  5.  * (c) Mobizel
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\EventSubscriber\Order;
  12. use App\Entity\Order\Order;
  13. use App\Message\SendOrderEmailMessage;
  14. use App\OrderProductionStates;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\Messenger\MessageBusInterface;
  17. use Symfony\Component\Workflow\Event\Event;
  18. /**
  19.  * Quand un imprimeur demande une annulation/erreur, notifie l'admin (async).
  20.  * L'email client d'annulation n'est envoyé qu'après validation admin (canceled/errored).
  21.  * Les états cancel_pending/error_pending ne sont atteignables que via les actions
  22.  * imprimeur (gardées par printer_workflow_enabled), donc pas de garde ici en plus.
  23.  */
  24. final class NotifyAdminPrinterCancelOrErrorRequestSubscriber implements EventSubscriberInterface
  25. {
  26.     public function __construct(
  27.         private MessageBusInterface $messageBus,
  28.     ) {
  29.     }
  30.     public function onEnter(Event $event): void
  31.     {
  32.         /** @var Order $order */
  33.         $order = $event->getSubject();
  34.         $orderId = $order->getId();
  35.         if (null === $orderId) {
  36.             return;
  37.         }
  38.         $this->messageBus->dispatch(new SendOrderEmailMessage(
  39.             $orderId,
  40.             SendOrderEmailMessage::TYPE_ADMIN_CANCEL_OR_ERROR_REQUEST,
  41.             $order->getProductionState() ?? OrderProductionStates::STATE_CANCEL_PENDING,
  42.         ));
  43.     }
  44.     public static function getSubscribedEvents(): array
  45.     {
  46.         return [
  47.             'workflow.order_production.entered.cancel_pending' => 'onEnter',
  48.             'workflow.order_production.entered.error_pending' => 'onEnter',
  49.         ];
  50.     }
  51. }