pelican-panel-banquise/app/Listeners/DispatchWebhooks.php
Boy132 d555c42644
Update all dependencies (#712)
* update composer.lock

* run pint

* fix phpstan

* update migrations (sqlite `dropForeign`)

* fix migrations

* Reset these back for now

* Alphabetize the rules

* run `php artisan filament:upgrade`

---------

Co-authored-by: Lance Pioch <git@lance.sh>
2024-11-22 09:27:57 +01:00

41 lines
1.1 KiB
PHP

<?php
namespace App\Listeners;
use App\Jobs\ProcessWebhook;
use App\Models\WebhookConfiguration;
class DispatchWebhooks
{
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)) {
ProcessWebhook::dispatch($webhookConfig, $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);
}
}