src/EventSubscriber/LogoutSubscriber.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\Security\Http\Event\LogoutEvent;
  6. class LogoutSubscriber implements EventSubscriberInterface
  7. {
  8.     private $entityManager;
  9.     public function __construct(EntityManagerInterface $entityManager)
  10.     {
  11.         $this->entityManager $entityManager;
  12.     }
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             LogoutEvent::class => 'onLogout',
  17.         ];
  18.     }
  19.     public function onLogout(LogoutEvent $event): void
  20.     {
  21.         $token $event->getToken();
  22.         if (!$token) {
  23.             return;
  24.         }
  25.         $admin $token->getUser();
  26.         if (!$admin instanceof \App\Entity\Admin) {
  27.             return;
  28.         }
  29.         $connection $this->entityManager->getConnection();
  30.         date_default_timezone_set("Asia/Calcutta");
  31.         $cli_dt date("Y-m-d H:i:s");
  32.         $connection->executeStatement(
  33.             "UPDATE admin SET user_sts='0' WHERE admin_id = ?",
  34.             [$admin->getAdminId()]
  35.         );
  36.         $connection->executeStatement(
  37.             "UPDATE login_log SET update_dt = ? WHERE designation='client' AND admin_id = ? AND update_dt = '0000-00-00 00:00:00'",
  38.             [$cli_dt$admin->getAdminId()]
  39.         );
  40.         
  41.         $request $event->getRequest();
  42.         $request->getSession()->clear();
  43.     }
  44. }