src/EventSubscriber/JWTSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber;
  4. use App\Client\Qoodos\QoodosClient;
  5. use App\Entity\Security\Customer;
  6. use App\Entity\Security\Manager;
  7. use App\Entity\Security\ShopManager;
  8. use App\Entity\Security\UserInterface;
  9. use App\Helper\Image\ImageHelper;
  10. use App\Helper\Response\ResponseInterface as CustomResponseInterface;
  11. use App\Repository\Franchise\CustomerFranchiseRepository;
  12. use App\Repository\Franchise\FranchiseRepository;
  13. use App\Service\Security\CustomerService;
  14. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  19. class JWTSubscriber implements EventSubscriberInterface
  20. {
  21.     private $request;
  22.     public function __construct(
  23.         private ImageHelper $imageHelper,
  24.         private FranchiseRepository $franchiseRepository,
  25.         private QoodosClient $qoodosClient,
  26.         private CustomerService $customerService,
  27.         private CustomerFranchiseRepository $customerFranchiseRepository,
  28.         RequestStack $requestStack,
  29.     ) {
  30.         $this->request $requestStack->getCurrentRequest();
  31.     }
  32.     public function onLexikJwtAuthenticationOnJwtCreated(JWTCreatedEvent $event): void
  33.     {
  34.         $user $event->getUser();
  35.         $payload array_merge($event->getData(), [
  36.             'exp' => time() + UserInterface::TOKEN_LIFE_TIME,
  37.             'ip' => $this->request->getClientIp(),
  38.         ]);
  39.         if ($user instanceof Customer) {
  40.             $franchiseToken $this->request->headers->get('franchise-token');
  41.             if (null === $franchiseToken) {
  42.                 throw new BadRequestHttpException(Response::$statusTexts[Response::HTTP_BAD_REQUEST]);
  43.             }
  44.             $franchise $this->franchiseRepository->findOneBy(['token' => $franchiseToken]);
  45.             if (null === $franchise) {
  46.                 throw new BadRequestHttpException(CustomResponseInterface::FRANCHISE_NOT_FOUND);
  47.             }
  48.             if (false === $this->customerService->customerHasFranchise($user$franchise)) {
  49.                 throw new BadRequestHttpException(CustomResponseInterface::CUSTOMER_NOT_FOUND_IN_FRANCHISE);
  50.             }
  51.             $payload array_merge($payload, [
  52.                 'franchise' => $franchise->getId(),
  53.                 'name' => $user->getFullName(),
  54.                 'id' => $user->getId(),
  55.                 'imageUrl' => $this->imageHelper->getAbsolutePath($user),
  56.                 'askForSubscriptionToQoodos' => $this->customerFranchiseRepository->canAskForSubscriptionToQoodos($user$franchise),
  57.             ]);
  58.             if (true === $franchise->hasQoodos()) {
  59.                 try {
  60.                     $this->qoodosClient->init($franchise);
  61.                     $result $this->qoodosClient->getFranchise();
  62.                     $payload array_merge($payload, [
  63.                         'canUseCashback' => $result['can_use_cashback'],
  64.                         'canUsePoints' => $result['can_use_points'],
  65.                     ]);
  66.                 } catch (\Exception $e) {
  67.                 }
  68.             }
  69.         }
  70.         if ($user instanceof ShopManager) {
  71.             $payload['shop'] = $user->getShop()->getId();
  72.         }
  73.         if ($user instanceof Manager) {
  74.             $payload['franchise'] = $user->getFranchise()->getId();
  75.         }
  76.         $event->setData($payload);
  77.         $header $event->getHeader();
  78.         $header['cty'] = 'JWT';
  79.         $event->setHeader($header);
  80.     }
  81.     public static function getSubscribedEvents()
  82.     {
  83.         return [
  84.             'lexik_jwt_authentication.on_jwt_created' => 'onLexikJwtAuthenticationOnJwtCreated',
  85.         ];
  86.     }
  87. }