src/EventSubscriber/VoteQuestionSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\VoteQuestion;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use App\Event\VoteQuestionPreUpdateEvent;
  6. use App\Event\VoteQuestionPrePersistEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  9. class VoteQuestionSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(
  12.         private EntityManagerInterface $_em,
  13.     ) {}
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             VoteQuestionPrePersistEvent::NAME => 'onPreCreate',
  18.             //VoteQuestionPreUpdateEvent::NAME => 'onPreUpdate'
  19.         ];
  20.     }
  21.     public function onPreCreate(VoteQuestionPrePersistEvent $event)
  22.     {
  23.         $object $event->getObject();
  24.         $session $object->getSession();
  25.         if ($session) {
  26.             $this->_em->getRepository(VoteQuestion::class)
  27.                 ->resetVoteQuestionSession($session);
  28.         }
  29.     }
  30.     public function onPreUpdate(VoteQuestionPreUpdateEvent $event)
  31.     {
  32.         /*
  33.         $object = $event->getObject();
  34.         if ($object->getIsSelected()) {
  35.             throw new BadRequestHttpException('validation.voteQuestion:voteIsSelected');
  36.         }
  37.         */
  38.     }
  39. }