From e6cbdfd12213030004f31186bfc1cb991b2308ed Mon Sep 17 00:00:00 2001 From: Filippo Montani Date: Mon, 2 Oct 2023 11:44:34 +0200 Subject: [PATCH] implementig support for custom notification channels --- src/Config/firewall.php | 17 ++++++++++++++++- src/Notifications/AttackDetected.php | 20 +++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/Config/firewall.php b/src/Config/firewall.php index d94a56a..fa96480 100644 --- a/src/Config/firewall.php +++ b/src/Config/firewall.php @@ -51,7 +51,22 @@ 'channel' => env('FIREWALL_SLACK_CHANNEL', null), // set null to use the default channel of webhook 'queue' => env('FIREWALL_SLACK_QUEUE', 'default'), ], - + + /* + |-------------------------------------------------------------------------- + | CUSTOM CHANNELS + |-------------------------------------------------------------------------- + | + | your applcation notification channels, as documented + | in https://laravel.com/docs/10.x/notifications#custom-channels + | + */ + + // 'custom' => [ + // 'enabled' => true, + // 'channel' => App\Notifications\CustomChannel::class, + // 'queue' => 'customqueue', + // ], ], 'all_middleware' => [ diff --git a/src/Notifications/AttackDetected.php b/src/Notifications/AttackDetected.php index eb1676d..6c1378a 100644 --- a/src/Notifications/AttackDetected.php +++ b/src/Notifications/AttackDetected.php @@ -12,6 +12,14 @@ class AttackDetected extends Notification implements ShouldQueue { use Queueable; + /** + * Channels with built-in support, which don't require a custom handler. + */ + const DEFAULT_CHANNELS = [ + 'mail', + 'slack' + ]; + /** * The log model. * @@ -50,6 +58,8 @@ public function via($notifiable) continue; } + $channel = in_array($channel, self::DEFAULT_CHANNELS) ? $channel : $settings['channel']; + $channels[] = $channel; } @@ -63,7 +73,15 @@ public function via($notifiable) public function viaQueues(): array { - return array_map(fn ($channel) => $channel['queue'] ?? 'default', $this->notifications); + $channels = []; + + foreach ($this->notifications as $channel => $settings) { + + $key = in_array($channel, self::DEFAULT_CHANNELS) ? $channel : $settings['channel']; + + $channels[$key] = $settings['queue'] ?? 'default'; + } + return $channels; } /**