31 lines
925 B
PHP
31 lines
925 B
PHP
<?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());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |