custom/plugins/CioGuestRedirect/src/Subscriber/RedirectSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. namespace CioGuestRedirect\Subscriber;
  3. use CioImports\Event\ImportStockMissingProductNotificationEvent;
  4. use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
  5. use Shopware\Core\Checkout\Customer\Event\CustomerDeletedEvent;
  6. use Shopware\Core\Framework\Event\BusinessEvent;
  7. use Shopware\Core\Framework\Event\BusinessEventCollectorEvent;
  8. use Shopware\Core\Framework\Event\BusinessEventDefinition;
  9. use Shopware\Storefront\Controller\AuthController;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Shopware\Storefront\Event\StorefrontRenderEvent;
  16. class RedirectSubscriber implements EventSubscriberInterface
  17. {
  18.     CONST WHITELIST_ROUTES = [
  19.         '/account/login',
  20.         '/widgets/checkout/info',
  21.         '/account/recover',
  22.         '/account/recover/password',
  23.         '/checkout/offcanvas',
  24.         '/cookie/offcanvas',
  25.         '/impressum'
  26.     ];
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  30.         return [
  31.             'Shopware\Storefront\Event\StorefrontRenderEvent' => 'onStorefrontRenderEvent'
  32.         ];
  33.     }
  34.     public function onStorefrontRenderEvent(StorefrontRenderEvent $event)
  35.     {
  36.         if($event->getSalesChannelContext()->getSalesChannel()->isMaintenance()) {
  37.             return;
  38.         }
  39.         if (in_array($event->getRequest()->getPathInfo(), self::WHITELIST_ROUTES)) {
  40.             return;
  41.         }
  42.         if ($event->getSalesChannelContext()->getCustomer() === null) {
  43.             throw new CustomerNotLoggedInException();
  44.         }
  45.     }
  46. }