-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
WebhookController.php
54 lines (46 loc) · 1.65 KB
/
WebhookController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
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;
/**
* Receives webhooks from a variety of third-party providers.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @experimental in 6.3
*
* @internal
*/
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('No webhook parser found for the type given in the URL.', 404, ['Content-Type' => 'text/plain']);
}
/** @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));
return $parser->createSuccessfulResponse();
}
}