src/EventListener/JWTCreatedListener.php line 52

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\AuthUser;
  4. use App\Entity\UserIps;
  5. use App\Service\Csb\Auth\CsbAuthService;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Psr\Log\LoggerInterface;
  8. use Symfony\Component\Security\Core\Security;
  9. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  10. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  11. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  12. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  13. class JWTCreatedListener
  14. {
  15.     /** @var Security $security */
  16.     private Security $security;
  17.     /** @var EntityManagerInterface $em */
  18.     private EntityManagerInterface $em;
  19.     /** @var LoggerInterface $logger */
  20.     private LoggerInterface $logger;
  21.     /** @var CsbAuthService $csbAuthService */
  22.     private CsbAuthService $csbAuthService;
  23.     /**
  24.      * @param Security $security
  25.      * @param EntityManagerInterface $em
  26.      * @param LoggerInterface $logger
  27.      * @param CsbAuthService $csbAuthService
  28.      */
  29.     public function __construct(
  30.         Security               $security,
  31.         EntityManagerInterface $em,
  32.         LoggerInterface        $logger,
  33.         CsbAuthService         $csbAuthService
  34.     )
  35.     {
  36.         $this->security $security;
  37.         $this->em $em;
  38.         $this->logger $logger;
  39.         $this->csbAuthService $csbAuthService;
  40.     }
  41.     /**
  42.      * @return void
  43.      */
  44.     public function onJWTCreated(): void
  45.     {
  46.         /** @var  AuthUser $currentUser */
  47.         $currentUser $this->security->getUser();
  48.         try {
  49.             if (null != $currentUser) {
  50.                 $userIp = new UserIps();
  51.                 $this->csbAuthService->setUserIpInstance($currentUser,$userIp);
  52.                 $this->em->persist($userIp);
  53.                 $this->em->flush();
  54.             }
  55.         } catch (ClientExceptionInterface RedirectionExceptionInterface ServerExceptionInterface TransportExceptionInterface $e) {
  56.             $this->logger->info("Exception on JWTCreatedListener.onJWTCreated : " $e->getMessage());
  57.         }
  58.     }
  59. }