src/EventListener/JWTCreatedListener.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Client;
  4. use App\Repository\ClientRepository;
  5. use App\Repository\HotelRepository;
  6. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. class JWTCreatedListener
  9. {
  10.     /**
  11.      * @var ClientRepository
  12.      */
  13.     private $clientRepository;
  14.     /**
  15.      * @var HotelRepository
  16.      */
  17.     private $hotelRepository;
  18.     /**
  19.      * JWTCreatedListener constructor.
  20.      */
  21.     public function __construct(ClientRepository $clientRepositoryHotelRepository $hotelRepository)
  22.     {
  23.         $this->clientRepository $clientRepository;
  24.         $this->hotelRepository $hotelRepository;
  25.     }
  26.     public function onJWTCreated(JWTCreatedEvent $event)
  27.     {
  28.         $payload $event->getData();
  29.         $user $event->getUser();
  30.         if (!$user instanceof UserInterface)
  31.             return;
  32.         $client $this->clientRepository->findOneBy(['email' => $user->getUserIdentifier()]);
  33.         if (!$client)
  34.             return;
  35.         $payload['id'] = $client->getId();
  36.         $payload['uid'] = $client->getUid();
  37.         $payload['civility'] = $client->getCivility();
  38.         $payload['nationality'] = $client->getNationality();
  39.         $payload['firstname'] = $client->getFirstname();
  40.         $payload['lastname'] = $client->getLastname();
  41.         $payload['language'] = $client->getLanguage();
  42.         $payload['city'] = $client->getCity();
  43.         $payload['country'] = $client->getCountry();
  44.         $payload['birthday'] = !$client->getBirthday() ?: $client->getBirthday()->format(\DateTimeInterface::ISO8601);
  45.         $payload['address'] = $client->getAddress();
  46.         $payload['mobile'] = $client->getMobile();
  47.         $payload['emailContact'] = $client->getEmailContact();
  48.         $payload['contactByMail'] = $client->getContactByMail();
  49.         $payload['contactBySms'] = $client->getContactBySms();
  50.         $hotel $this->hotelRepository->findCurrentHotel($client);
  51.         if ($hotel) {
  52.             $payload['hotel']['id'] = $hotel->getId();
  53.             $payload['hotel']['name'] = $hotel->getName();
  54.             $payload['hotel']['logo'] = $hotel->getLogo();
  55.             $payload['hotel']['slug'] = $hotel->getSlug();
  56.         }
  57.         $event->setData($payload);
  58.     }
  59. }