From ede0e4b845b48bf5514d033338d3e8ddb69e4e3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Kukli=C5=84ski?= Date: Mon, 23 Oct 2023 12:10:59 +0200 Subject: [PATCH] Fixing issue with multiple wishlist tokens generated randomly --- src/EventSubscriber/CreateNewWishlistSubscriber.php | 10 +++++++--- src/Resolver/WishlistCookieTokenResolver.php | 13 ++++++++++++- src/Resources/config/services.yml | 1 + 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/EventSubscriber/CreateNewWishlistSubscriber.php b/src/EventSubscriber/CreateNewWishlistSubscriber.php index c3d7a4b0..8ed979d4 100644 --- a/src/EventSubscriber/CreateNewWishlistSubscriber.php +++ b/src/EventSubscriber/CreateNewWishlistSubscriber.php @@ -23,6 +23,7 @@ use Sylius\Component\Core\Model\ShopUserInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\Cookie; +use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\Event\ResponseEvent; @@ -47,6 +48,8 @@ final class CreateNewWishlistSubscriber implements EventSubscriberInterface private TokenUserResolverInterface $tokenUserResolver; + private RequestStack $requestStack; + public function __construct( string $wishlistCookieToken, WishlistsResolverInterface $wishlistsResolver, @@ -56,6 +59,7 @@ public function __construct( ChannelContextInterface $channelContext, WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver, TokenUserResolverInterface $tokenUserResolver, + RequestStack $requestStack, ) { $this->wishlistCookieToken = $wishlistCookieToken; $this->wishlistsResolver = $wishlistsResolver; @@ -65,6 +69,7 @@ public function __construct( $this->channelContext = $channelContext; $this->wishlistCookieTokenResolver = $wishlistCookieTokenResolver; $this->tokenUserResolver = $tokenUserResolver; + $this->requestStack = $requestStack; } public static function getSubscribedEvents(): array @@ -77,7 +82,7 @@ public static function getSubscribedEvents(): array public function onKernelRequest(RequestEvent $event): void { - $request = $event->getRequest(); + $request = $this->requestStack->getMainRequest(); $currentPath = $request->getPathInfo(); if (!$event->isMainRequest() || !str_starts_with($currentPath, '/wishlist')) { @@ -100,7 +105,6 @@ public function onKernelRequest(RequestEvent $event): void if (null === $wishlistCookieToken) { $wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve(); - $this->createNewWishlist($wishlistCookieToken); } $request->attributes->set($this->wishlistCookieToken, $wishlistCookieToken); @@ -108,7 +112,7 @@ public function onKernelRequest(RequestEvent $event): void public function onKernelResponse(ResponseEvent $event): void { - $request = $event->getRequest(); + $request = $this->requestStack->getMainRequest(); $currentPath = $request->getPathInfo(); if (!$event->isMainRequest() || !str_starts_with($currentPath, '/wishlist')) { return; diff --git a/src/Resolver/WishlistCookieTokenResolver.php b/src/Resolver/WishlistCookieTokenResolver.php index 4ebe6324..cec2604e 100644 --- a/src/Resolver/WishlistCookieTokenResolver.php +++ b/src/Resolver/WishlistCookieTokenResolver.php @@ -20,6 +20,8 @@ final class WishlistCookieTokenResolver implements WishlistCookieTokenResolverIn private string $wishlistCookieToken; + private ?string $generatedToken = null; + public function __construct( RequestStack $requestStack, string $wishlistCookieToken @@ -30,12 +32,21 @@ public function __construct( public function resolve(): string { + if (null !== $this->generatedToken) { + return $this->generatedToken; + } + $wishlistCookieToken = $this->requestStack->getMainRequest()->cookies->get($this->wishlistCookieToken); if (!$wishlistCookieToken) { - return (string) new WishlistToken(); + $newToken = (string) new WishlistToken(); + $this->generatedToken = $newToken; + + return $newToken; } + $this->generatedToken = $wishlistCookieToken; + return $wishlistCookieToken; } } diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 5ef8199c..ecac5644 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -207,6 +207,7 @@ services: - "@sylius.context.channel" - '@bitbag_sylius_wishlist_plugin.resolver.wishlist_cookie_token_resolver' - '@bitbag_sylius_wishlist_plugin.resolver.token_user_resolver' + - '@request_stack' tags: - { name: kernel.event_subscriber, event: kernel.exception }