Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CreateNewWishlistSubscriber breaks debug:event-dispatcher command #231

Closed
oliver-schulz opened this issue Nov 3, 2023 · 1 comment · Fixed by #234
Closed

CreateNewWishlistSubscriber breaks debug:event-dispatcher command #231

oliver-schulz opened this issue Nov 3, 2023 · 1 comment · Fixed by #234

Comments

@oliver-schulz
Copy link

when i type bin/console debug:event-dispatcher -vvv

the command is blocked by:

[Webmozart\Assert\InvalidArgumentException]
The class has to be used in HTTP context only
Webmozart\Assert\Assert::notNull() at vendor/bitbag/wishlist-plugin/src/EventSubscriber/CreateNewWishlistSubscriber.php:48

I modified the code by not injecting the Request into constructor and using the request object from the RequestEvent. Also removed the Assert.

public function onKernelRequest(RequestEvent $event): void
{
    if (!$event->isMainRequest()) {
        return;
    }
    $request = $event->getRequest();
    $currentPath = $request->getPathInfo();
    if (!str_starts_with($currentPath, self::ALLOWED_ENDPOINTS_PREFIX)) {
        return;
    }

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

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

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

        return;
    }

    if (null === $wishlistCookieToken) {
        $wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve();
    }

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

public function onKernelResponse(ResponseEvent $event): void
{
    if (!$event->isMainRequest()) {
        return;
    }
    $request = $event->getRequest();
    if (!str_starts_with($request->getPathInfo(), self::ALLOWED_ENDPOINTS_PREFIX)) {
        return;
    }

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

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

    if (!$wishlistCookieToken) {
        return;
    }

    $cookie = new Cookie($this->wishlistCookieToken, $wishlistCookieToken, strtotime('+1 year'));
    $response->headers->setCookie($cookie);

    $request->attributes->remove($this->wishlistCookieToken);
}
@senghe
Copy link
Member

senghe commented Nov 10, 2023

Hi @oliver-schulz, thanks for your issue. I've already fixed this in #234, it should be merged and released today I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants