<?php declare(strict_types=1);
namespace Momocode\MomoLoginRedirectSW6\Redirect\Subscriber;
use Momocode\MomoLoginRedirectSW6\Redirect\Component\ConfigServiceInterface;
use Momocode\MomoLoginRedirectSW6\Redirect\Event\RedirectLoginRoutesEvent;
use Momocode\MomoLoginRedirectSW6\Storefront\Framework\Cookie\CookieProviderDecorator;
use Psr\Log\LoggerInterface;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/**
* @author Moritz Müller <moritz@momocode.de>
*/
class StorefrontSubscriber implements EventSubscriberInterface
{
protected ConfigServiceInterface $configService;
protected EventDispatcherInterface $eventDispatcher;
protected LoggerInterface $logger;
public function __construct(
ConfigServiceInterface $configService,
EventDispatcherInterface $eventDispatcher,
LoggerInterface $logger
) {
$this->configService = $configService;
$this->eventDispatcher = $eventDispatcher;
$this->logger = $logger;
}
public static function getSubscribedEvents(): array
{
return [
StorefrontRenderEvent::class => 'onRenderStorefront',
];
}
public function onRenderStorefront(StorefrontRenderEvent $event): void
{
$request = $event->getRequest();
$currentRoute = $request->get('_route');
$loginRoutesEvent = new RedirectLoginRoutesEvent(['frontend.account.login.page']);
$this->eventDispatcher->dispatch($loginRoutesEvent);
// If we are on a login page we load the redirect infos from cookies
// and pass them to the render parameters
if (\in_array($currentRoute, $loginRoutesEvent->getLoginRoutes(), true)) {
$cookie = $request->cookies->get(CookieProviderDecorator::LOGIN_REDIRECT_COOKIE_KEY);
if (\is_string($cookie)) {
try {
$cookieData = json_decode($cookie, true, 512, \JSON_THROW_ON_ERROR);
$redirectRoute = $cookieData['route'] ?? null;
$routeParams = $cookieData['routeParams'] ?? [];
$queryParams = $cookieData['queryParams'] ?? [];
$redirectParams = array_merge($routeParams, $queryParams);
if ($this->configService->isDebugLoggingActive()) {
$this->logger->debug('Redirect cookie is set', [
'cookieValue' => $cookie,
'redirectRoute' => $redirectRoute,
'redirectParams' => $redirectParams,
]);
}
// We add a random parameter to solve a cache problem with the account menu
// It's a similar problem like this: https://issues.shopware.com/issues/NEXT-17181
$redirectParams['mlr'] = 1;
if ($redirectRoute && $this->configService->isRedirectActive($redirectRoute)) {
$event->setParameter('redirectTo', $redirectRoute);
$event->setParameter(
'redirectParameters',
json_encode($redirectParams, \JSON_THROW_ON_ERROR)
);
}
} catch (\JsonException $exception) {
// Just log it (should only happen if someone edits the cookie data manually)
$this->logger->error(
'Invalid JSON in cookie: ' . CookieProviderDecorator::LOGIN_REDIRECT_COOKIE_KEY,
[
'cookieValue' => $cookie,
]
);
}
} elseif ($this->configService->isDebugLoggingActive()) {
$this->logger->debug('Redirect cookie is not set', [
'cookieValue' => $cookie,
]);
}
}
}
}