custom/plugins/MomoLoginRedirectSW6/src/Redirect/Subscriber/StorefrontSubscriber.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Momocode\MomoLoginRedirectSW6\Redirect\Subscriber;
  3. use Momocode\MomoLoginRedirectSW6\Redirect\Component\ConfigServiceInterface;
  4. use Momocode\MomoLoginRedirectSW6\Redirect\Event\RedirectLoginRoutesEvent;
  5. use Momocode\MomoLoginRedirectSW6\Storefront\Framework\Cookie\CookieProviderDecorator;
  6. use Psr\Log\LoggerInterface;
  7. use Shopware\Storefront\Event\StorefrontRenderEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  10. /**
  11.  * @author Moritz Müller <moritz@momocode.de>
  12.  */
  13. class StorefrontSubscriber implements EventSubscriberInterface
  14. {
  15.     protected ConfigServiceInterface $configService;
  16.     protected EventDispatcherInterface $eventDispatcher;
  17.     protected LoggerInterface $logger;
  18.     public function __construct(
  19.         ConfigServiceInterface $configService,
  20.         EventDispatcherInterface $eventDispatcher,
  21.         LoggerInterface $logger
  22.     ) {
  23.         $this->configService $configService;
  24.         $this->eventDispatcher $eventDispatcher;
  25.         $this->logger $logger;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             StorefrontRenderEvent::class => 'onRenderStorefront',
  31.         ];
  32.     }
  33.     public function onRenderStorefront(StorefrontRenderEvent $event): void
  34.     {
  35.         $request $event->getRequest();
  36.         $currentRoute $request->get('_route');
  37.         $loginRoutesEvent = new RedirectLoginRoutesEvent(['frontend.account.login.page']);
  38.         $this->eventDispatcher->dispatch($loginRoutesEvent);
  39.         // If we are on a login page we load the redirect infos from cookies
  40.         // and pass them to the render parameters
  41.         if (\in_array($currentRoute$loginRoutesEvent->getLoginRoutes(), true)) {
  42.             $cookie $request->cookies->get(CookieProviderDecorator::LOGIN_REDIRECT_COOKIE_KEY);
  43.             if (\is_string($cookie)) {
  44.                 try {
  45.                     $cookieData json_decode($cookietrue512\JSON_THROW_ON_ERROR);
  46.                     $redirectRoute $cookieData['route'] ?? null;
  47.                     $routeParams $cookieData['routeParams'] ?? [];
  48.                     $queryParams $cookieData['queryParams'] ?? [];
  49.                     $redirectParams array_merge($routeParams$queryParams);
  50.                     if ($this->configService->isDebugLoggingActive()) {
  51.                         $this->logger->debug('Redirect cookie is set', [
  52.                             'cookieValue' => $cookie,
  53.                             'redirectRoute' => $redirectRoute,
  54.                             'redirectParams' => $redirectParams,
  55.                         ]);
  56.                     }
  57.                     // We add a random parameter to solve a cache problem with the account menu
  58.                     // It's a similar problem like this: https://issues.shopware.com/issues/NEXT-17181
  59.                     $redirectParams['mlr'] = 1;
  60.                     if ($redirectRoute && $this->configService->isRedirectActive($redirectRoute)) {
  61.                         $event->setParameter('redirectTo'$redirectRoute);
  62.                         $event->setParameter(
  63.                             'redirectParameters',
  64.                             json_encode($redirectParams\JSON_THROW_ON_ERROR)
  65.                         );
  66.                     }
  67.                 } catch (\JsonException $exception) {
  68.                     // Just log it (should only happen if someone edits the cookie data manually)
  69.                     $this->logger->error(
  70.                         'Invalid JSON in cookie: ' CookieProviderDecorator::LOGIN_REDIRECT_COOKIE_KEY,
  71.                         [
  72.                             'cookieValue' => $cookie,
  73.                         ]
  74.                     );
  75.                 }
  76.             } elseif ($this->configService->isDebugLoggingActive()) {
  77.                 $this->logger->debug('Redirect cookie is not set', [
  78.                     'cookieValue' => $cookie,
  79.                 ]);
  80.             }
  81.         }
  82.     }
  83. }