<?php
namespace App\EventListener;
use App\Entity\Employee;
use App\Entity\User;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Symfony\Component\Security\Core\User\UserInterface;
use Vich\UploaderBundle\Storage\StorageInterface;
class AuthenticationSuccessAddDataListener
{
/**
* @var StorageInterface
*/
private $storage;
public function __construct(StorageInterface $storage)
{
$this->storage = $storage;
}
/**
* @param AuthenticationSuccessEvent $event
*/
public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event)
{
$data = $event->getData();
/** @var User $user */
$user = $event->getUser();
if (!$user instanceof UserInterface) {
return;
}
/** @var Employee $employee */
$employee = $user->getEmployee();
$urlPhoto = '';
if(!is_null($employee->getPhoto())){
$urlPhoto = $this->storage->resolveUri($employee->getPhoto(), 'file');
}
$data['user'] = array(
'id' => $user->getEmployee()->getId(),
'name' => $user->getEmployee()->getName(),
'lastName' => $user->getEmployee()->getLastName(),
'email' => $user->getEmail(),
'photo' => $urlPhoto,
'roles' => $user->getRoles(),
);
$event->setData($data);
}
}