From 9b0c06c435b42b96391d8ccfbcb6ca2f121526ab Mon Sep 17 00:00:00 2001 From: semihkeskindev Date: Thu, 22 Feb 2024 22:19:55 +0300 Subject: [PATCH] webhooks group by name, and expect name in use of middleware --- config/request-forwarder.php | 23 +++++++++----- .../WebhookNameNotFoundException.php | 8 +++++ src/ProcessRequestForwarder.php | 3 +- src/RequestForwarder.php | 30 ++++++++++++++++--- src/RequestForwarderMiddleware.php | 4 +-- 5 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 src/Exceptions/WebhookNameNotFoundException.php diff --git a/config/request-forwarder.php b/config/request-forwarder.php index ce76f57..6009ea5 100644 --- a/config/request-forwarder.php +++ b/config/request-forwarder.php @@ -2,15 +2,22 @@ // config for Moneo/RequestForwarder return [ + // decides which webhook to use if no webhook name is specified while use middleware + 'default_webhook_name' => 'default', + 'webhooks' => [ - [ - 'url' => 'https://some-domain.com/webhook', - 'method' => 'POST', - ], - [ - 'url' => 'https://discord.com/api/webhooks/1209955556656291860/LAaczT-Pg785d5OzBmi6ivx2Vl7wAoruOwcVnZpb2eE2x8tf7fMi6R7_sr0IV0WoK83S', - 'method' => 'POST', - 'provider' => \Moneo\RequestForwarder\Providers\Discord::class, + 'default' => [ + 'targets' => [ + [ + 'url' => 'https://some-domain.com/webhook', + 'method' => 'POST', + ], + [ + 'url' => 'https://discord.com/api/webhooks/1209955556656291860/LAaczT-Pg785d5OzBmi6ivx2Vl7wAoruOwcVnZpb2eE2x8tf7fMi6R7_sr0IV0WoK83S', + 'method' => 'POST', + 'provider' => \Moneo\RequestForwarder\Providers\Discord::class, + ], + ] ], ], ]; diff --git a/src/Exceptions/WebhookNameNotFoundException.php b/src/Exceptions/WebhookNameNotFoundException.php new file mode 100644 index 0000000..f84453b --- /dev/null +++ b/src/Exceptions/WebhookNameNotFoundException.php @@ -0,0 +1,8 @@ +triggerHooks($this->url, $this->params); + $requestForwarder->triggerHooks($this->url, $this->params, $this->webhookName); } } diff --git a/src/RequestForwarder.php b/src/RequestForwarder.php index 1c85b68..4ce444a 100755 --- a/src/RequestForwarder.php +++ b/src/RequestForwarder.php @@ -4,6 +4,7 @@ use Illuminate\Http\Client\Factory; use Illuminate\Http\Request; +use Moneo\RequestForwarder\Exceptions\WebhookNameNotFoundException; use Moneo\RequestForwarder\Providers\DefaultProvider; use Moneo\RequestForwarder\Providers\ProviderInterface; @@ -15,14 +16,17 @@ public function __construct( ) { } - public function sendAsync(Request $request) + public function sendAsync(Request $request, ?string $webhookName = null): void { - ProcessRequestForwarder::dispatch($request->url(), $request->toArray()); + ProcessRequestForwarder::dispatch($request->url(), $request->toArray(), $webhookName); } - public function triggerHooks(string $url, array $params) + /** + * @throws WebhookNameNotFoundException + */ + public function triggerHooks(string $url, array $params, string $webhookName = null): void { - foreach ($this->webhooks as $webhook) { + foreach ($this->getWebhookTargets($webhookName) as $webhook) { try { /** @var ProviderInterface $provider */ $providerClass = $webhook['provider'] ?? DefaultProvider::class; @@ -32,4 +36,22 @@ public function triggerHooks(string $url, array $params) } } } + + /** + * @throws WebhookNameNotFoundException + */ + private function getWebhookInfo(?string $webhookName = null): array + { + $webhookName = $webhookName ?? config('request-forwarder.default_webhook_name'); + + return $this->webhooks[$webhookName] ?? throw new WebhookNameNotFoundException('Webhook name called ' . $webhookName . ' is not defined in the config file'); + } + + /** + * @throws WebhookNameNotFoundException + */ + private function getWebhookTargets(?string $webhookName = null): array + { + return $this->getWebhookInfo($webhookName)['targets']; + } } diff --git a/src/RequestForwarderMiddleware.php b/src/RequestForwarderMiddleware.php index 16fa724..09b5deb 100644 --- a/src/RequestForwarderMiddleware.php +++ b/src/RequestForwarderMiddleware.php @@ -18,9 +18,9 @@ public function __construct( * * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next */ - public function handle(Request $request, Closure $next): Response + public function handle(Request $request, Closure $next, ?string $name = null): Response { - $this->requestForwarder->sendAsync($request); + $this->requestForwarder->sendAsync($request, $name); return $next($request); }