<?php
namespace App\EventListener;
use App\Entity\Users;
use Doctrine\ORM\EntityManagerInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Symfony\Component\HttpFoundation\Request;
class AuthenticationSuccessListener
{
/**
* @var string
*/
private $pictureUrl;
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var array
*/
private $zones;
/**
* AuthenticationSuccessListener constructor.
*
* @param string $pictureUrl
* @param EntityManagerInterface $entityManager
*/
public function __construct(string $pictureUrl, EntityManagerInterface $entityManager)
{
$this->pictureUrl = $pictureUrl;
$this->entityManager = $entityManager;
}
/**
* @param AuthenticationSuccessEvent $event
*/
public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event)
{
$data = $event->getData();
$user = $event->getUser();
if (!$user instanceof Users) {
return;
}
foreach ($user->getZone() as $zone) {
$this->zones[] = $zone->getId();
}
$user->setLastDateSynchroApi(new \DateTime());
$this->entityManager->flush();
$profilePicture = !empty($user->getProfilePicture()) ?
$this->pictureUrl
. $user->getId()
. DIRECTORY_SEPARATOR
. $user->getProfilePicture() :
$user->getProfilePicture();
$newData = [
'id' => $user->getId(),
'fullName' => $user->getFullName(),
'pictureUrl' => $profilePicture,
'roles' => $user->getRoles(),
'zone' => $this->zones,
'codeEnqueteur' => $user->getInvestigatorCode()
];
$event->setData(array_merge($data, $newData));
}
}