Skip to content

Commit

Permalink
implementig support for custom notification channels
Browse files Browse the repository at this point in the history
  • Loading branch information
Filippo Montani committed Oct 2, 2023
1 parent b01b55b commit e6cbdfd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/Config/firewall.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
20 changes: 19 additions & 1 deletion src/Notifications/AttackDetected.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -50,6 +58,8 @@ public function via($notifiable)
continue;
}

$channel = in_array($channel, self::DEFAULT_CHANNELS) ? $channel : $settings['channel'];

$channels[] = $channel;
}

Expand All @@ -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;
}

/**
Expand Down

0 comments on commit e6cbdfd

Please sign in to comment.