*/ public static function send(User $user, Notification $notification): array { $done = []; foreach ($notification->channels() as $channel) { try { if (self::deliver($channel, $user, $notification)) { $done[] = $channel; } } catch (Throwable $e) { (new Logger('notifications'))->error('Уведомление не доставлено', [ 'channel' => $channel, 'type' => $notification->type(), 'user' => $user->id(), 'error' => $e->getMessage(), ]); } } if ($done !== []) { Events::fire('notification.sent', [ 'user' => $user, 'notification' => $notification, 'channels' => $done, ]); } return $done; } /** * Отправляет нескольким. * * @param array $users */ public static function sendMany(array $users, Notification $notification): int { $count = 0; foreach ($users as $user) { if (self::send($user, $notification) !== []) { $count++; } } return $count; } /** * Отправляет всем, у кого есть право: так уведомляют «тех, кто отвечает * за сервис», не заводя отдельного списка адресатов. */ public static function toPermission(string $permission, Notification $notification): int { return self::sendMany(self::whoCan($permission), $notification); } /** * Действующие пользователи с нужным правом. * * @return array */ public static function whoCan(string $permission): array { if (!Connection::instance()->hasTable('users')) { return []; } $roles = []; foreach (Role::all() as $role) { if (in_array($permission, $role->permissions(), true)) { $roles[] = $role->id(); } } if ($roles === []) { return []; } /** @var array $users */ $users = User::query()->where('active', 1)->whereIn('role_id', $roles)->orderBy('id')->get(); return $users; } /** * Уборка прочитанных уведомлений по сроку — зовётся воркером. */ public static function purge(): int { return UserNotification::purge((int) Config::get('notifications.keep_days', 90)); } /** * Один канал. true — доставлено. */ private static function deliver(string $channel, User $user, Notification $notification): bool { return match ($channel) { self::DATABASE => self::toDatabase($user, $notification), self::MAIL => self::toMail($user, $notification), // Чужой канал — не молчим: опечатка в channels() иначе теряется default => throw new ProtonException('Неизвестный канал уведомлений: ' . $channel), }; } private static function toDatabase(User $user, Notification $notification): bool { if (!Connection::instance()->hasTable('notifications')) { return false; } UserNotification::add( $user->id(), $notification->type(), $notification->title(), $notification->body(), $notification->url() ); return true; } private static function toMail(User $user, Notification $notification): bool { $email = trim((string) $user->email); // Адреса нет или он негодный — письмо не уйдёт, но лента уже сработала if (!Validator::isEmail($email)) { return false; } $notification->mail($user)->queue(); return true; } }