2026-07-01 18:35:39 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Modules\Core\Notification;
|
|
|
|
|
|
2026-08-31 13:16:13 +03:00
|
|
|
use Throwable;
|
2026-07-01 18:35:39 +03:00
|
|
|
use Illuminate\Support\Facades\Event;
|
|
|
|
|
|
|
|
|
|
class NotificationRegistry
|
|
|
|
|
{
|
|
|
|
|
private static ?self $instance = null;
|
|
|
|
|
|
|
|
|
|
private array $notifications = [];
|
|
|
|
|
|
|
|
|
|
private function __construct() {}
|
|
|
|
|
|
|
|
|
|
public static function get(): static
|
|
|
|
|
{
|
|
|
|
|
if (static::$instance == null) {
|
|
|
|
|
static::$instance = new static();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return static::$instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function register(array $notifications): void
|
|
|
|
|
{
|
|
|
|
|
foreach ($notifications as $class) {
|
|
|
|
|
$key = $class::getKey();
|
|
|
|
|
$alreadyRegistered = isset($this->notifications[$key]);
|
|
|
|
|
|
|
|
|
|
$this->notifications[$key] = $class;
|
|
|
|
|
|
|
|
|
|
if ($alreadyRegistered) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Event::listen($class::listensTo(), function (object $event) use ($key) {
|
|
|
|
|
$class = $this->notifications[$key] ?? null;
|
|
|
|
|
if ($class == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
$notification = new $class($event);
|
|
|
|
|
if (isset($event->delaySeconds) && $event->delaySeconds > 0) {
|
|
|
|
|
$notification->delay($event->delaySeconds);
|
|
|
|
|
}
|
|
|
|
|
$notification->notifiable()->notify($notification);
|
2026-08-31 13:16:13 +03:00
|
|
|
} catch (Throwable $e) {
|
2026-07-01 18:35:39 +03:00
|
|
|
report($e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function unregister(string $key): void
|
|
|
|
|
{
|
|
|
|
|
unset($this->notifications[$key]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function all(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->notifications;
|
|
|
|
|
}
|
|
|
|
|
}
|