<?php
namespace App\EventSubscriber;
use App\Entity\TextLoi;
use App\Repository\TextLoiRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Contracts\EventDispatcher\Event;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\Security\Http\Event\LogoutEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;
class LogoutSubscriber implements EventSubscriberInterface
{
private $rtl;
private $entityManager;
private $security;
public function __construct(TextLoiRepository $rtl,EntityManagerInterface $em,Security $security)
{
$this->rtl=$rtl;
$this->entityManager=$em;
$this->security = $security;
}
public static function getSubscribedEvents()
{
// return the subscribed events, their methods and priorities
return [
LogoutEvent::class =>['processLogout']
];
}
public function processLogout(LogoutEvent $event)
{
$user = $this->security->getUser();
if (!$user instanceof \App\Entity\User) {
return;
}
$societe = $user->getSociete();
$nbTextLoi = $this->rtl->countTextLoi();
$societe->setNbTextLoi($nbTextLoi);
$this->entityManager->flush();
}
}