<?php
namespace App\Event;
use App\Entity\OnlineShop\Order;
use App\Enum\MenuRolesManagerEnum;
use App\Enum\OrderEventEnum;
use App\Kernel;
use App\Manager\EmailsNotificationsManager;
use App\Manager\WhatsappNotificationManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Security\Core\Security;
class OrderEvent implements EventSubscriberInterface
{
private EmailsNotificationsManager $emailsNotificationsManager;
private WhatsappNotificationManager $whatsappNotificationManager;
private Security $security;
public function __construct(EmailsNotificationsManager $emailsNotificationsManager, WhatsappNotificationManager $whatsappNotificationManager, Security $security)
{
$this->emailsNotificationsManager = $emailsNotificationsManager;
$this->whatsappNotificationManager = $whatsappNotificationManager;
$this->security = $security;
}
public static function getSubscribedEvents(): array
{
return [
OrderEventEnum::ORDER_NEW => 'onNewOrder',
OrderEventEnum::ORDER_PROCESSED => 'onOrderProcessed',
];
}
/**
* @throws TransportExceptionInterface
*/
public function onNewOrder(GenericEvent $event): bool
{
$order = $event->getSubject();
if (!$order instanceof Order || Kernel::CLI_ENV === PHP_SAPI) {
return false;
}
if ($this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_ONLINE_SHOP)) {
$this->emailsNotificationsManager->notifyAdminNewOrderCreatedByAdmin($order);
$this->emailsNotificationsManager->notifyAssociatedNewOrderCreatedByAdmin($order);
} else {
$this->emailsNotificationsManager->notifyAdminNewOrderCreatedByAssociated($order);
$this->emailsNotificationsManager->notifyAssociatedNewOrderCreatedByAssociated($order);
}
return true;
}
/**
* @throws TransportExceptionInterface
*/
public function onOrderProcessed(GenericEvent $event): bool
{
$order = $event->getSubject();
if (!$order instanceof Order || Kernel::CLI_ENV === PHP_SAPI) {
return false;
}
$this->emailsNotificationsManager->notifyAssociatedOrderProcessed($order);
$this->emailsNotificationsManager->notifySupplierOrderProcessed($order);
if ($order->getWhatsappOrder() !== null) {
// If the order comes from Whatsapp
$this->whatsappNotificationManager->notifyAssociatedOrderProcessed($order);
}
return true;
}
}