<?php
namespace App\EventListener;
use App\Entity\AuthUser;
use App\Entity\UserIps;
use App\Service\Csb\Auth\CsbAuthService;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class JWTCreatedListener
{
/** @var Security $security */
private Security $security;
/** @var EntityManagerInterface $em */
private EntityManagerInterface $em;
/** @var LoggerInterface $logger */
private LoggerInterface $logger;
/** @var CsbAuthService $csbAuthService */
private CsbAuthService $csbAuthService;
/**
* @param Security $security
* @param EntityManagerInterface $em
* @param LoggerInterface $logger
* @param CsbAuthService $csbAuthService
*/
public function __construct(
Security $security,
EntityManagerInterface $em,
LoggerInterface $logger,
CsbAuthService $csbAuthService
)
{
$this->security = $security;
$this->em = $em;
$this->logger = $logger;
$this->csbAuthService = $csbAuthService;
}
/**
* @return void
*/
public function onJWTCreated(): void
{
/** @var AuthUser $currentUser */
$currentUser = $this->security->getUser();
try {
if (null != $currentUser) {
$userIp = new UserIps();
$this->csbAuthService->setUserIpInstance($currentUser,$userIp);
$this->em->persist($userIp);
$this->em->flush();
}
} catch (ClientExceptionInterface | RedirectionExceptionInterface | ServerExceptionInterface | TransportExceptionInterface $e) {
$this->logger->info("Exception on JWTCreatedListener.onJWTCreated : " . $e->getMessage());
}
}
}