src/EventListener/AuthenticationSuccessAddDataListener.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Employee;
  4. use App\Entity\User;
  5. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. use Vich\UploaderBundle\Storage\StorageInterface;
  8. class AuthenticationSuccessAddDataListener
  9. {
  10.     /**
  11.      * @var StorageInterface
  12.      */
  13.     private $storage;
  14.     public function __construct(StorageInterface $storage)
  15.     {
  16.         $this->storage $storage;
  17.     }
  18.     /**
  19.      * @param AuthenticationSuccessEvent $event
  20.      */
  21.     public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event)
  22.     {
  23.         $data $event->getData();
  24.         /** @var User $user */
  25.         $user $event->getUser();
  26.         if (!$user instanceof UserInterface) {
  27.             return;
  28.         }
  29.         /** @var Employee $employee */
  30.         $employee $user->getEmployee();
  31.         $urlPhoto '';
  32.         if(!is_null($employee->getPhoto())){
  33.             $urlPhoto $this->storage->resolveUri($employee->getPhoto(), 'file');
  34.         }
  35.         $data['user'] = array(
  36.             'id' => $user->getEmployee()->getId(),
  37.             'name' => $user->getEmployee()->getName(),
  38.             'lastName' => $user->getEmployee()->getLastName(),
  39.             'email' => $user->getEmail(),
  40.             'photo' => $urlPhoto,
  41.             'roles' => $user->getRoles(),
  42.         );
  43.         $event->setData($data);
  44.     }
  45. }