custom/plugins/OrcaOci/src/Subscriber/StorefrontSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Orcamultimedia\OciPunchout\Subscriber;
  4. use Orcamultimedia\OciPunchout\Service\OciService;
  5. use Shopware\Core\SalesChannelRequest;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. use Symfony\Component\Routing\RouterInterface;
  12. class StorefrontSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var OciService
  16.      */
  17.     private OciService $ociService;
  18.     /**
  19.      * @var RouterInterface
  20.      */
  21.     private RouterInterface $router;
  22.     public function __construct(
  23.         OciService      $ociService,
  24.         RouterInterface $router
  25.     )
  26.     {
  27.         $this->ociService $ociService;
  28.         $this->router $router;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             KernelEvents::REQUEST => 'preventPageLoadingInOciSession'
  34.         ];
  35.     }
  36.     public function preventPageLoadingInOciSession(RequestEvent $event): void
  37.     {
  38.         if ($event->getRequest()->isXmlHttpRequest()) {
  39.             return;
  40.         }
  41.         if (!$event->getRequest()->attributes->has(SalesChannelRequest::ATTRIBUTE_IS_SALES_CHANNEL_REQUEST)) {
  42.             return;
  43.         }
  44.         if (!$this->ociService->isPunchout()) {
  45.             return;
  46.         }
  47.         $route $event->getRequest()->attributes->get('_route');
  48.         $forbiddenRoutes = [
  49.             'frontend.account.profile.email.save',
  50.             'frontend.account.profile.password.save',
  51.             'frontend.checkout.finish.page',
  52.             'frontend.checkout.finish.order'
  53.         ];
  54.         if (in_array($route$forbiddenRoutes)) {
  55.             throw new AccessDeniedHttpException('This page isn\'t allowed during an OCI session.');
  56.         }
  57.         $disallowedRoutes = ['frontend.checkout.confirm.page'];
  58.         if (in_array($route$disallowedRoutes)) {
  59.             $event->setResponse(new RedirectResponse($this->router->generate('frontend.checkout.cart.page')));
  60.         }
  61.     }
  62. }