src/EventSubscriber/NotifSubscriber.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Notif;
  4. use App\Event\NotifEvent;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class NotifSubscriber implements EventSubscriberInterface
  8. {
  9.     private $entityManager;
  10.     public function __construct(EntityManagerInterface $entityManager)
  11.     {
  12.         $this->entityManager $entityManager;
  13.     }
  14.     /**
  15.      * @param NotifEvent $notifEvent
  16.      */
  17.     public function onNotifEvent(NotifEvent $notifEvent)
  18.     {
  19.         $notif = new Notif();
  20.         $notif->setRecepient($notifEvent->getNotifRecipient());
  21.         $notif->setBody($notifEvent->getNotifBody());
  22.         $notif->setCreatedAt(new \DateTime());
  23.         $this->entityManager->persist($notif);
  24.         $this->entityManager->flush();
  25.     }
  26.     /**
  27.      * @return array|\array[][]
  28.      */
  29.     public static function getSubscribedEvents()
  30.     {
  31.         return [
  32.             NotifEvent::class => [['onNotifEvent',0]],
  33.         ];
  34.     }
  35. }