Skip to content

Commit

Permalink
webhooks group by name, and expect name in use of middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
semihkeskindev committed Feb 22, 2024
1 parent b6df3d6 commit 9b0c06c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 15 deletions.
23 changes: 15 additions & 8 deletions config/request-forwarder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
]
],
],
];
8 changes: 8 additions & 0 deletions src/Exceptions/WebhookNameNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Moneo\RequestForwarder\Exceptions;

class WebhookNameNotFoundException extends \Exception
{

}
3 changes: 2 additions & 1 deletion src/ProcessRequestForwarder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ProcessRequestForwarder implements ShouldQueue
public function __construct(
public readonly string $url,
public readonly array $params,
public readonly ?string $webhookName = null,
) {
}

Expand All @@ -27,6 +28,6 @@ public function __construct(
public function handle(): void
{
$requestForwarder = app(RequestForwarder::class);
$requestForwarder->triggerHooks($this->url, $this->params);
$requestForwarder->triggerHooks($this->url, $this->params, $this->webhookName);
}
}
30 changes: 26 additions & 4 deletions src/RequestForwarder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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'];
}
}
4 changes: 2 additions & 2 deletions src/RequestForwarderMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 9b0c06c

Please sign in to comment.