src/EventListener/AuthenticationSuccessListener.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Users;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
  6. use Symfony\Component\HttpFoundation\Request;
  7. class AuthenticationSuccessListener
  8. {
  9.     /**
  10.      * @var string
  11.      */
  12.     private $pictureUrl;
  13.     /**
  14.      * @var EntityManagerInterface
  15.      */
  16.     private $entityManager;
  17.     /**
  18.      * @var array
  19.      */
  20.     private $zones;
  21.     /**
  22.      * AuthenticationSuccessListener constructor.
  23.      *
  24.      * @param string $pictureUrl
  25.      * @param EntityManagerInterface $entityManager
  26.      */
  27.     public function __construct(string $pictureUrlEntityManagerInterface $entityManager)
  28.     {
  29.         $this->pictureUrl $pictureUrl;
  30.         $this->entityManager $entityManager;
  31.     }
  32.     /**
  33.      * @param AuthenticationSuccessEvent $event
  34.      */
  35.     public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event)
  36.     {
  37.         $data $event->getData();
  38.         $user $event->getUser();
  39.         if (!$user instanceof Users) {
  40.             return;
  41.         }
  42.         foreach ($user->getZone() as $zone) {
  43.             $this->zones[] = $zone->getId();
  44.         }
  45.         $user->setLastDateSynchroApi(new \DateTime());
  46.         $this->entityManager->flush();
  47.         $profilePicture = !empty($user->getProfilePicture()) ?
  48.             $this->pictureUrl
  49.             $user->getId()
  50.             . DIRECTORY_SEPARATOR
  51.             $user->getProfilePicture() :
  52.             $user->getProfilePicture();
  53.         $newData = [
  54.             'id' => $user->getId(),
  55.             'fullName' => $user->getFullName(),
  56.             'pictureUrl' => $profilePicture,
  57.             'roles' => $user->getRoles(),
  58.             'zone' => $this->zones,
  59.             'codeEnqueteur' => $user->getInvestigatorCode()
  60.         ];
  61.         $event->setData(array_merge($data$newData));
  62.     }
  63. }