src/EventListener/JwtCreatedListener.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Project;
  4. use App\Repository\ProjectRepository;
  5. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
  6. use Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTDecodeFailureException;
  7. use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. class JwtCreatedListener
  12. {
  13.     private RequestStack $requestStack;
  14.     private ProjectRepository $projectRepository;
  15.     private TokenStorageInterface $tokenStorage;
  16.     private JWTTokenManagerInterface $JWTTokenManager;
  17.     public function __construct(
  18.         RequestStack $requestStack,
  19.         ProjectRepository $projectRepository,
  20.         TokenStorageInterface $tokenStorage,
  21.         JWTTokenManagerInterface $JWTTokenManager,
  22.     ) {
  23.         $this->requestStack $requestStack;
  24.         $this->projectRepository $projectRepository;
  25.         $this->tokenStorage $tokenStorage;
  26.         $this->JWTTokenManager $JWTTokenManager;
  27.     }
  28.     public function onJWTCreated(JWTCreatedEvent $event): void
  29.     {
  30.         $data $event->getData();
  31.         $user $event->getUser();
  32.         if (!$user instanceof UserInterface) {
  33.             return;
  34.         }
  35.         $requestData json_decode($this->requestStack->getCurrentRequest()->getContent());
  36.         if (empty($requestData->projectId)) {
  37.             return;
  38.         }
  39.         $project $this->getCurrentProject();
  40.         if ($project instanceof Project) {
  41.             $data['hasCatalog'] = $project->isCatalogAllowed();
  42.             $data['lastCatalogUpdate'] = $project->getLastUpdated()->format('Y-m-d H:i:s');
  43.             $data['lastUpdateDelayAlert'] = $project->getLastUpdateAlert();
  44.             $data['competitorsCompany'] = $project->getCompetitors();
  45.             $data['projectId'] = $project->getId();
  46.         }
  47.         $event->setData($data);
  48.     }
  49.     /**
  50.      * @throws JWTDecodeFailureException
  51.      */
  52.     private function getCurrentProject(): ?Project
  53.     {
  54.         $rawData $this->requestStack->getCurrentRequest()->getContent();
  55.         if (null != $rawData) {
  56.             $data json_decode($rawData);
  57.             if (!isset($data->projectId)) {
  58.                 return null;
  59.             }
  60.             $projectId $data->projectId;
  61.         } else {
  62.             $token $this->tokenStorage->getToken();
  63.             $user $token?->getUser();
  64.             if ($user) {
  65.                 $datas $this->JWTTokenManager->decode($token);
  66.                 $projectId $datas['projectId'];
  67.             }
  68.         }
  69.         if (empty($projectId)) {
  70.             return null;
  71.         }
  72.         $project $this->projectRepository->find($projectId);
  73.         if (!$project) {
  74.             return null;
  75.         }
  76.         return $project;
  77.     }
  78. }