<?php
namespace App\EventSubscriber;
use App\Entity\VoteQuestion;
use Doctrine\ORM\EntityManagerInterface;
use App\Event\VoteQuestionPreUpdateEvent;
use App\Event\VoteQuestionPrePersistEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class VoteQuestionSubscriber implements EventSubscriberInterface
{
public function __construct(
private EntityManagerInterface $_em,
) {}
public static function getSubscribedEvents(): array
{
return [
VoteQuestionPrePersistEvent::NAME => 'onPreCreate',
//VoteQuestionPreUpdateEvent::NAME => 'onPreUpdate'
];
}
public function onPreCreate(VoteQuestionPrePersistEvent $event)
{
$object = $event->getObject();
$session = $object->getSession();
if ($session) {
$this->_em->getRepository(VoteQuestion::class)
->resetVoteQuestionSession($session);
}
}
public function onPreUpdate(VoteQuestionPreUpdateEvent $event)
{
/*
$object = $event->getObject();
if ($object->getIsSelected()) {
throw new BadRequestHttpException('validation.voteQuestion:voteIsSelected');
}
*/
}
}