pelican-panel-banquise/app/Listeners/DispatchWebhooks.php
JoanFo c5aa8a3980
DiscordWebhooks (#1355)
Co-authored-by: notCharles <charles@pelican.dev>
Co-authored-by: Lance Pioch <lancepioch@gmail.com>
Co-authored-by: Boy132 <mail@boy132.de>
Co-authored-by: RMartinOscar <40749467+RMartinOscar@users.noreply.github.com>
2025-07-05 12:42:34 -04:00

43 lines
1.1 KiB
PHP

<?php
namespace App\Listeners;
use App\Models\WebhookConfiguration;
class DispatchWebhooks
{
/**
* @param array<mixed> $data
*/
public function handle(string $eventName, array $data): void
{
if (!$this->eventIsWatched($eventName)) {
return;
}
$matchingHooks = cache()->rememberForever("webhooks.$eventName", function () use ($eventName) {
return WebhookConfiguration::query()->whereJsonContains('events', $eventName)->get();
});
/** @var WebhookConfiguration $webhookConfig */
foreach ($matchingHooks as $webhookConfig) {
if (in_array($eventName, $webhookConfig->events)) {
$webhookConfig->run($eventName, $data);
}
}
}
protected function eventIsWatched(string $eventName): bool
{
$watchedEvents = cache()->rememberForever('watchedWebhooks', function () {
return WebhookConfiguration::pluck('events')
->flatten()
->unique()
->values()
->all();
});
return in_array($eventName, $watchedEvents);
}
}