src/EventListener/CheckoutValidation.php line 25

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventListener;
  4. use App\Entity\User\ShopUser;
  5. use App\Service\Edlsso;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. use Symfony\Component\EventDispatcher\GenericEvent;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  11. use Symfony\Component\Routing\RouterInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Contracts\HttpClient\HttpClientInterface;
  15. class CheckoutValidation
  16. {
  17.     public function __construct(private ParameterBagInterface $params, private ContainerInterface $container, private HttpClientInterface $client, private TokenStorageInterface $tokenStorage, private FlashBagInterface $flashBag, private RouterInterface $router)
  18.     {
  19.     }
  20.     public function handleEdlValidation(GenericEvent $event): void
  21.     {
  22.         $checkoutData $event->getSubject();
  23.         /** @var TokenInterface $token */
  24.         $token $this->tokenStorage->getToken();
  25.         if ($token === null) {
  26.             return;
  27.         }
  28.         $edlsso = new Edlsso($this->client$this->params$this->container);
  29.         /** @var ShopUser $shopUser */
  30.         $shopUser $token->getUser();
  31.         $userToken $shopUser->getEdlToken();
  32.         /** @var string $userTokenData */
  33.         $userTokenData $userToken $userToken->getToken() : '';
  34.         $response $edlsso->verifyToken($userTokenData);
  35.         if ($response['status'] === 'success') {
  36.             return;
  37.         }
  38.         $this->tokenStorage->setToken(null);
  39.         $this->flashBag->add('error''Session Expired. Please login again.');
  40.         $response = new RedirectResponse($this->router->generate('sylius_shop_login'));
  41.         $response->send();
  42.     }
  43. }