Skip to content

Commit

Permalink
Store new wishlist token on main request
Browse files Browse the repository at this point in the history
  • Loading branch information
senghe committed Oct 23, 2023
1 parent c34dd8b commit 36456b6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
19 changes: 14 additions & 5 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 @@ -81,14 +86,16 @@ public function onKernelRequest(RequestEvent $event): void
return;
}

$request = $this->requestStack->getMainRequest();

/** @var WishlistInterface[] $wishlists */
$wishlists = $this->wishlistsResolver->resolve();

$wishlistCookieToken = $event->getRequest()->cookies->get($this->wishlistCookieToken);
$wishlistCookieToken = $request->cookies->get($this->wishlistCookieToken);

if (!empty($wishlists)) {
if (null === $wishlistCookieToken) {
$event->getRequest()->attributes->set($this->wishlistCookieToken, reset($wishlists)->getToken());
$request->attributes->set($this->wishlistCookieToken, reset($wishlists)->getToken());
}

return;
Expand All @@ -99,7 +106,7 @@ public function onKernelRequest(RequestEvent $event): void
$wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve();
}

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

public function onKernelResponse(ResponseEvent $event): void
Expand All @@ -108,12 +115,14 @@ public function onKernelResponse(ResponseEvent $event): void
return;
}

if ($event->getRequest()->cookies->has($this->wishlistCookieToken)) {
$request = $this->requestStack->getMainRequest();

if ($request->cookies->has($this->wishlistCookieToken)) {
return;
}

$response = $event->getResponse();
$wishlistCookieToken = $event->getRequest()->attributes->get($this->wishlistCookieToken);
$wishlistCookieToken = $request->attributes->get($this->wishlistCookieToken);

if (!$wishlistCookieToken) {
return;
Expand Down
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 36456b6

Please sign in to comment.