src/EventListener/AuthenticationSuccessListener.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\AbstractCollaborator;
  4. use App\Entity\Agent;
  5. use App\Entity\Cgu;
  6. use App\Message\ConnexionLogsMessage;
  7. use App\Services\CurrencyService;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\Messenger\MessageBusInterface;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. class AuthenticationSuccessListener
  14. {
  15.     private EntityManagerInterface $manager;
  16.     private CurrencyService $currencyService;
  17.     private TranslatorInterface $translator;
  18.     private MessageBusInterface $bus;
  19.     private RequestStack $requestStack;
  20.     public function __construct(
  21.         EntityManagerInterface $manager,
  22.         CurrencyService $currencyService,
  23.         TranslatorInterface $translator,
  24.         MessageBusInterface $bus,
  25.         RequestStack $requestStack
  26.     ) {
  27.         $this->manager $manager;
  28.         $this->currencyService $currencyService;
  29.         $this->translator $translator;
  30.         $this->requestStack $requestStack;
  31.         $this->bus $bus;
  32.     }
  33.     public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event): void
  34.     {
  35.         $data $event->getData();
  36.         $qualification null;
  37.         $manager null;
  38.         $collaborator $event->getUser();
  39.         if (!$collaborator instanceof AbstractCollaborator) {
  40.             return;
  41.         }
  42.         if ($collaborator instanceof Agent) {
  43.             $qualification $collaborator->getQualificationLevel()->getLabel();
  44.             $manager = [
  45.                 'firstName' => $collaborator->getManager()->getFirstName(),
  46.                 'lastName' => $collaborator->getManager()->getLastName(),
  47.                 'phone' => $collaborator->getManager()->getMainPhone(),
  48.                 'email' => $collaborator->getManager()->getProfessionalEmail(),
  49.                 'fullName' => $collaborator->getManager()->getFullName(),
  50.             ];
  51.         }
  52.         $data['user'] = [
  53.             'id' => $collaborator->getId(),
  54.             'roles' => $collaborator->getRoles(),
  55.             'firstName' => $collaborator->getFirstName(),
  56.             'lastName' => $collaborator->getLastName(),
  57.             'fullName' => $collaborator->getFullName(),
  58.             'qualification' => $this->translator->trans($qualification, [], 'qualification_levels'),
  59.             'avatar' => $collaborator->getFullPathAvatar(),
  60.             'manager' => $manager,
  61.             'currency' => $this->currencyService->getCurrency(),
  62.             'gender' => [
  63.                 'code' => $collaborator->getGender()->getCode()
  64.             ],
  65.             'termsAccepted' => $collaborator->getCgu() instanceof Cgu ?? false
  66.         ];
  67.         // Add last connection date
  68.         $collaborator->setLastConnection(new \DateTime());
  69.         $this->manager->persist($collaborator);
  70.         $this->manager->flush();
  71.         $event->setData($data);
  72.         $userData json_decode($this->requestStack->getCurrentRequest()->getContent());
  73.         $this->bus->dispatch(new ConnexionLogsMessage(
  74.             $collaborator->getId(),
  75.             $userData->ipAddress ?? $_SERVER['REMOTE_ADDR'] ?? 'ND',
  76.             $userData->browser ?? 'ND',
  77.             new \DateTimeImmutable('now')
  78.         ));
  79.     }
  80. }