Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
senghe committed Oct 24, 2023
1 parent 73b6f54 commit f2c647c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
33 changes: 19 additions & 14 deletions src/EventSubscriber/CreateNewWishlistSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,23 @@
use BitBag\SyliusWishlistPlugin\Resolver\WishlistsResolverInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Webmozart\Assert\Assert;

final class CreateNewWishlistSubscriber implements EventSubscriberInterface
{
private const ALLOWED_ENDPOINTS_PREFIX = '/wishlist';
private string $wishlistCookieToken;

private WishlistsResolverInterface $wishlistsResolver;

private WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver;

private RequestStack $requestStack;
private Request $mainRequest;

public function __construct(
string $wishlistCookieToken,
Expand All @@ -39,7 +42,11 @@ public function __construct(
$this->wishlistCookieToken = $wishlistCookieToken;
$this->wishlistsResolver = $wishlistsResolver;
$this->wishlistCookieTokenResolver = $wishlistCookieTokenResolver;
$this->requestStack = $requestStack;

$mainRequest = $requestStack->getMainRequest();
Assert::notNull($mainRequest, 'The class has to be used in HTTP context only');

$this->mainRequest = $mainRequest;
}

public static function getSubscribedEvents(): array
Expand All @@ -56,20 +63,19 @@ public function onKernelRequest(RequestEvent $event): void
return;
}

$request = $this->requestStack->getMainRequest();
$currentPath = $request->getPathInfo();
if (!str_starts_with($currentPath, '/wishlist')) {
$currentPath = $this->mainRequest->getPathInfo();
if (!str_starts_with($currentPath, self::ALLOWED_ENDPOINTS_PREFIX)) {
return;
}

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

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

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

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

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

public function onKernelResponse(ResponseEvent $event): void
Expand All @@ -88,18 +94,17 @@ public function onKernelResponse(ResponseEvent $event): void
return;
}

$request = $this->requestStack->getMainRequest();
$currentPath = $request->getPathInfo();
if (!str_starts_with($currentPath, '/wishlist')) {
$currentPath = $this->mainRequest->getPathInfo();
if (!str_starts_with($currentPath, self::ALLOWED_ENDPOINTS_PREFIX)) {
return;
}

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

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

if (!$wishlistCookieToken) {
return;
Expand All @@ -108,6 +113,6 @@ public function onKernelResponse(ResponseEvent $event): void
$cookie = new Cookie($this->wishlistCookieToken, $wishlistCookieToken, strtotime('+1 year'));
$response->headers->setCookie($cookie);

$request->attributes->remove($this->wishlistCookieToken);
$this->mainRequest->attributes->remove($this->wishlistCookieToken);
}
}
4 changes: 2 additions & 2 deletions src/Resolver/WishlistCookieTokenResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public function resolve(): string
{
$wishlistCookieToken = $this->requestStack->getMainRequest()->cookies->get($this->wishlistCookieToken);

if ($wishlistCookieToken !== null) {
if (null !== $wishlistCookieToken) {
return $wishlistCookieToken;
}

$wishlistCookieToken = $this->requestStack->getMainRequest()->attributes->get($this->wishlistCookieToken);
if ($wishlistCookieToken !== null) {
if (null !== $wishlistCookieToken) {
return $wishlistCookieToken;
}

Expand Down

0 comments on commit f2c647c

Please sign in to comment.