<?php
namespace App\EventListener;
use App\Entity\Client;
use App\Repository\ClientRepository;
use App\Repository\HotelRepository;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
use Symfony\Component\Security\Core\User\UserInterface;
class JWTCreatedListener
{
/**
* @var ClientRepository
*/
private $clientRepository;
/**
* @var HotelRepository
*/
private $hotelRepository;
/**
* JWTCreatedListener constructor.
*/
public function __construct(ClientRepository $clientRepository, HotelRepository $hotelRepository)
{
$this->clientRepository = $clientRepository;
$this->hotelRepository = $hotelRepository;
}
public function onJWTCreated(JWTCreatedEvent $event)
{
$payload = $event->getData();
$user = $event->getUser();
if (!$user instanceof UserInterface)
return;
$client = $this->clientRepository->findOneBy(['email' => $user->getUserIdentifier()]);
if (!$client)
return;
$payload['id'] = $client->getId();
$payload['uid'] = $client->getUid();
$payload['civility'] = $client->getCivility();
$payload['nationality'] = $client->getNationality();
$payload['firstname'] = $client->getFirstname();
$payload['lastname'] = $client->getLastname();
$payload['language'] = $client->getLanguage();
$payload['city'] = $client->getCity();
$payload['country'] = $client->getCountry();
$payload['birthday'] = !$client->getBirthday() ?: $client->getBirthday()->format(\DateTimeInterface::ISO8601);
$payload['address'] = $client->getAddress();
$payload['mobile'] = $client->getMobile();
$payload['emailContact'] = $client->getEmailContact();
$payload['contactByMail'] = $client->getContactByMail();
$payload['contactBySms'] = $client->getContactBySms();
$hotel = $this->hotelRepository->findCurrentHotel($client);
if ($hotel) {
$payload['hotel']['id'] = $hotel->getId();
$payload['hotel']['name'] = $hotel->getName();
$payload['hotel']['logo'] = $hotel->getLogo();
$payload['hotel']['slug'] = $hotel->getSlug();
}
$event->setData($payload);
}
}