<?php
namespace App\EventListener;
use App\Entity\Gos\User;
use App\Utils\GetResponse\GetResponseContactCreator;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Events as JWTAuthenticationEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;
class GetResponseListener implements EventSubscriberInterface
{
private $getResponseContactCreator;
public function __construct(GetResponseContactCreator $getResponseContactCreator)
{
$this->getResponseContactCreator = $getResponseContactCreator;
}
public static function getSubscribedEvents(): array
{
return [
SecurityEvents::INTERACTIVE_LOGIN => ['onSecurityInteractiveLogin', -1],
JWTAuthenticationEvents::AUTHENTICATION_SUCCESS => 'onAuthenticationSuccessResponse',
];
}
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
$this->createGetResponseContact($event->getAuthenticationToken()->getUser());
}
public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event)
{
$this->createGetResponseContact($event->getUser());
}
private function createGetResponseContact(User $user)
{
if ($user->getGetResponseContactId() === null) {
$this->getResponseContactCreator->addContact($user);
}
}
}