src/Subscriber/EasyAdminSubscriber.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber;
  3. use App\Entity\JobOffer;
  4. use App\Service\JobOfferNewsletterSender;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\EventDispatcher\GenericEvent;
  7. use Twig\Error\LoaderError;
  8. use Twig\Error\RuntimeError;
  9. use Twig\Error\SyntaxError;
  10. class EasyAdminSubscriber implements EventSubscriberInterface
  11. {
  12.     private $jobOfferNewsletterSender;
  13.     public function __construct(JobOfferNewsletterSender $jobOfferNewsletterSender)
  14.     {
  15.         $this->jobOfferNewsletterSender $jobOfferNewsletterSender;
  16.     }
  17.     public static function getSubscribedEvents()
  18.     {
  19.         return array(
  20.             'easy_admin.pre_update' => array('setUpdatedAt'),
  21.             'easy_admin.post_persist' => array('afterCreated'),
  22.             'easy_admin.post_update' => array('afterUpdated'),
  23.         );
  24.     }
  25.     public function setUpdatedAt(GenericEvent $event)
  26.     {
  27.         $entity $event->getSubject();
  28.         try {
  29.             $entity->setUpdatedAt(new \DateTime('now'));
  30.         } catch (\Exception $e) {
  31.             die($e->getMessage());
  32.         }
  33.         $event['entity'] = $entity;
  34.     }
  35.     /**
  36.      * @param GenericEvent $event
  37.      * @throws LoaderError
  38.      * @throws RuntimeError
  39.      * @throws SyntaxError
  40.      */
  41.     public function afterCreated(GenericEvent $event)
  42.     {
  43.         $entity $event->getSubject();
  44.         if($entity instanceof JobOffer && $entity->isEnabled())
  45.             $this->jobOfferNewsletterSender->send($entity);
  46.     }
  47.     /**
  48.      * @param GenericEvent $event
  49.      * @throws LoaderError
  50.      * @throws RuntimeError
  51.      * @throws SyntaxError
  52.      */
  53.     public function afterUpdated(GenericEvent $event)
  54.     {
  55.         $entity $event->getSubject();
  56.         if($entity instanceof JobOffer && $entity->isEnabled() && !$entity->isNewsletterSent())
  57.             $this->jobOfferNewsletterSender->send($entity);
  58.     }
  59. }