Skip to content

Commit

Permalink
Fixing issue with multiple wishlist tokens generated randomly
Browse files Browse the repository at this point in the history
  • Loading branch information
senghe committed Oct 23, 2023
1 parent 8968639 commit ede0e4b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/EventSubscriber/CreateNewWishlistSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -47,6 +48,8 @@ final class CreateNewWishlistSubscriber implements EventSubscriberInterface

private TokenUserResolverInterface $tokenUserResolver;

private RequestStack $requestStack;

public function __construct(
string $wishlistCookieToken,
WishlistsResolverInterface $wishlistsResolver,
Expand All @@ -56,6 +59,7 @@ public function __construct(
ChannelContextInterface $channelContext,
WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver,
TokenUserResolverInterface $tokenUserResolver,
RequestStack $requestStack,
) {
$this->wishlistCookieToken = $wishlistCookieToken;
$this->wishlistsResolver = $wishlistsResolver;
Expand All @@ -65,6 +69,7 @@ public function __construct(
$this->channelContext = $channelContext;
$this->wishlistCookieTokenResolver = $wishlistCookieTokenResolver;
$this->tokenUserResolver = $tokenUserResolver;
$this->requestStack = $requestStack;
}

public static function getSubscribedEvents(): array
Expand All @@ -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')) {
Expand All @@ -100,15 +105,14 @@ public function onKernelRequest(RequestEvent $event): void
if (null === $wishlistCookieToken)
{
$wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve();
$this->createNewWishlist($wishlistCookieToken);
}

$request->attributes->set($this->wishlistCookieToken, $wishlistCookieToken);
}

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;
Expand Down
13 changes: 12 additions & 1 deletion src/Resolver/WishlistCookieTokenResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ final class WishlistCookieTokenResolver implements WishlistCookieTokenResolverIn

private string $wishlistCookieToken;

private ?string $generatedToken = null;

public function __construct(
RequestStack $requestStack,
string $wishlistCookieToken
Expand All @@ -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;
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down

0 comments on commit ede0e4b

Please sign in to comment.