active()->whereNull('alerted_at')->get() ->filter(fn (Channel $channel): bool => $channel->isStale()); foreach ($channels as $channel) { Mail::to(config('fewo.notification_email'))->send(new ChannelSyncAlert($channel)); $channel->forceFill(['alerted_at' => now()])->save(); } return $channels->count() + $this->monitorMailbox(); } public static function mailboxIsStale(): bool { if (! MailboxReader::isConfigured()) { return false; } $lastSuccess = Cache::get(MailboxReader::LAST_SUCCESS_KEY); return $lastSuccess === null || CarbonImmutable::parse($lastSuccess)->lessThan(now()->subMinutes(config('fewo.mailbox.stale_after_minutes'))); } private function monitorMailbox(): int { if (! self::mailboxIsStale()) { Cache::forget(self::MAILBOX_ALERTED_KEY); return 0; } if (Cache::has(self::MAILBOX_ALERTED_KEY)) { return 0; } Mail::to(config('fewo.notification_email'))->send(new MailboxAlert(Cache::get(MailboxReader::LAST_ERROR_KEY))); Cache::forever(self::MAILBOX_ALERTED_KEY, true); return 1; } }