v0.1.10
This commit is contained in:
31
kernel/modules/notification/NotificationDispatcher.php
Normal file
31
kernel/modules/notification/NotificationDispatcher.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\modules\notification;
|
||||
|
||||
use kernel\Flash;
|
||||
use kernel\modules\notification\contracts\NotificationChannelInterface;
|
||||
use kernel\modules\notification\contracts\NotificationMessage;
|
||||
use kernel\modules\user\models\User;
|
||||
|
||||
class NotificationDispatcher
|
||||
{
|
||||
protected array $channels = [];
|
||||
|
||||
public function addChannel(string $channelName, NotificationChannelInterface $channel): void
|
||||
{
|
||||
$this->channels[$channelName] = $channel;
|
||||
}
|
||||
|
||||
public function dispatch(NotificationMessage $notification, User $user): void
|
||||
{
|
||||
foreach ($notification->via() as $channelName) {
|
||||
if (isset($this->channels[$channelName])) {
|
||||
try {
|
||||
$this->channels[$channelName]->send($notification, $user);
|
||||
} catch (\Exception $e) {
|
||||
Flash::setMessage("error", $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user