8000 [Webhook] Multiple consumer for one parser · Issue #52494 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[Webhook] Multiple consumer for one parser #52494
Open
@Elkawaie

Description

@Elkawaie

Description

As a webhook consumer, I need to group my webhooks by category.

For example, I want to have only one entry for a webhook that has several event types ( like Stripe )

To do this, I need to change the RemoteEvent in the messenger stack, so that it no longer depends on a Request parameter

Example

I suggest that you do not use the type parameter from the Request and replace it with the property name of the RemoteEvent.

<?php

namespace Symfony\Component\Webhook\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\RemoteEvent\Messenger\ConsumeRemoteEventMessage;
use Symfony\Component\Webhook\Client\RequestParserInterface;


final class WebhookController
{
    public function __construct(
        /** @var array<string, array{parser: RequestParserInterface, secret: string}> $parsers */
        private readonly array $parsers,
        private readonly MessageBusInterface $bus,
    ) {
    }

    public function handle(string $type, Request $request): Response
    {
        if (!isset($this->parsers[$type])) {
            return new Response(sprintf('No parser found for webhook of type "%s".', $type), 404);
        }
        /** @var RequestParserInterface $parser */
        $parser = $this->parsers[$type]['parser'];


        if (!$event = $parser->parse($request, $this->parsers[$type]['secret'])) {
            return $parser->createRejectedResponse('Unable to parse the webhook payload.');
        }

-        $this->bus->dispatch(new ConsumeRemoteEventMessage($type, $event));

+        $this->bus->dispatch(new ConsumeRemoteEventMessage($event->getName(), $event));

        return $parser->createSuccessfulResponse();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0