src/Event/OrderEvent.php line 60

Open in your IDE?
  1. <?php
  2. namespace App\Event;
  3. use App\Entity\OnlineShop\Order;
  4. use App\Enum\MenuRolesManagerEnum;
  5. use App\Enum\OrderEventEnum;
  6. use App\Kernel;
  7. use App\Manager\EmailsNotificationsManager;
  8. use App\Manager\WhatsappNotificationManager;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\EventDispatcher\GenericEvent;
  11. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  12. use Symfony\Component\Security\Core\Security;
  13. class OrderEvent implements EventSubscriberInterface
  14. {
  15.     private EmailsNotificationsManager $emailsNotificationsManager;
  16.     private WhatsappNotificationManager $whatsappNotificationManager;
  17.     private Security $security;
  18.     public function __construct(EmailsNotificationsManager $emailsNotificationsManagerWhatsappNotificationManager $whatsappNotificationManagerSecurity $security)
  19.     {
  20.         $this->emailsNotificationsManager $emailsNotificationsManager;
  21.         $this->whatsappNotificationManager $whatsappNotificationManager;
  22.         $this->security $security;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             OrderEventEnum::ORDER_NEW => 'onNewOrder',
  28.             OrderEventEnum::ORDER_PROCESSED => 'onOrderProcessed',
  29.         ];
  30.     }
  31.     /**
  32.      * @throws TransportExceptionInterface
  33.      */
  34.     public function onNewOrder(GenericEvent $event): bool
  35.     {
  36.         $order $event->getSubject();
  37.         if (!$order instanceof Order || Kernel::CLI_ENV === PHP_SAPI) {
  38.             return false;
  39.         }
  40.         if ($this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_ONLINE_SHOP)) {
  41.             $this->emailsNotificationsManager->notifyAdminNewOrderCreatedByAdmin($order);
  42.             $this->emailsNotificationsManager->notifyAssociatedNewOrderCreatedByAdmin($order);
  43.         } else {
  44.             $this->emailsNotificationsManager->notifyAdminNewOrderCreatedByAssociated($order);
  45.             $this->emailsNotificationsManager->notifyAssociatedNewOrderCreatedByAssociated($order);
  46.         }
  47.         return true;
  48.     }
  49.     /**
  50.      * @throws TransportExceptionInterface
  51.      */
  52.     public function onOrderProcessed(GenericEvent $event): bool
  53.     {
  54.         $order $event->getSubject();
  55.         if (!$order instanceof Order || Kernel::CLI_ENV === PHP_SAPI) {
  56.             return false;
  57.         }
  58.         $this->emailsNotificationsManager->notifyAssociatedOrderProcessed($order);
  59.         $this->emailsNotificationsManager->notifySupplierOrderProcessed($order);
  60.         if ($order->getWhatsappOrder() !== null) {
  61.             // If the order comes from Whatsapp
  62.             $this->whatsappNotificationManager->notifyAssociatedOrderProcessed($order);
  63.         }
  64.         return true;
  65.     }
  66. }