src/EventSubscriber/EasyAdminSubscriber.php line 31

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Order;
  4. use App\Enum\NotificationContent;
  5. use App\Service\SendEmailNotification;
  6. use DateTimeImmutable;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  9. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  10. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class EasyAdminSubscriber implements EventSubscriberInterface
  13. {
  14.     private $manager;
  15.     public function __construct(EntityManagerInterface $manager, private SendEmailNotification $email)
  16.     {
  17.         $this->manager $manager;
  18.     }
  19.     public static function getSubscribedEvents()
  20.     {
  21.         return [
  22.             BeforeEntityUpdatedEvent::class => ['addDeliveryDate'],
  23.         ];
  24.     }
  25.     public function addDeliveryDate(BeforeEntityUpdatedEvent $event)
  26.     {
  27.         $entity $event->getEntityInstance();
  28.         if (!($entity instanceof Order)) {
  29.             return;
  30.         }
  31.         if ($entity->getStatus() == 'livred') {
  32.             $entity->setLivredAt(new DateTimeImmutable());
  33.         }
  34.         if ($entity->getStatus() == 'orderCanceled') {
  35.             $this->email->sendBoth(NotificationContent::ORDERCANCELED$entity);
  36.         }
  37.         if ($entity->getStatus() == 'refund') {
  38.             $this->email->sendBoth(NotificationContent::ORDERREFUND$entity);
  39.         }
  40.        
  41.     }
  42. }