option('limit')); $domains = $this->option('stale') ? $this->staleDomains($limit) : $this->domainsFromContacts($limit); if ($domains->isEmpty()) { $this->info('Nichts zu prüfen.'); return self::SUCCESS; } $counts = []; $bar = $this->output->createProgressBar($domains->count()); foreach ($domains as $domain) { $check = $checker->check($domain, force: (bool) $this->option('refresh') || (bool) $this->option('stale')); $counts[$check->state->value] = ($counts[$check->state->value] ?? 0) + 1; $bar->advance(); } $bar->finish(); $this->newLine(2); foreach ($counts as $state => $count) { $this->line(sprintf(' %-16s %d', $state, $count)); } $broken = EmailDomainCheck::query()->broken()->count(); $this->info(sprintf('%d Domains geprüft, %d ohne Zustellweg im Bestand.', $domains->count(), $broken)); return self::SUCCESS; } /** * @return Collection */ private function staleDomains(int $limit): Collection { return EmailDomainCheck::query() ->where(fn ($q) => $q->whereNull('expires_at')->orWhere('expires_at', '<=', now())) ->orderBy('expires_at') ->limit($limit) ->pluck('domain'); } /** * @return Collection */ private function domainsFromContacts(int $limit): Collection { $months = $this->option('months') !== null ? max(1, (int) $this->option('months')) : null; $companies = Company::query() ->withoutGlobalScopes() ->whereNotNull('email') ->where('email', '!=', '') ->when($months !== null, fn ($q) => $q->whereHas('pressReleases', fn ($r) => $r->withoutGlobalScopes()->where('press_releases.created_at', '>=', now()->subMonths($months)))) ->pluck('email'); $contacts = Contact::query() ->withoutGlobalScopes() ->whereNotNull('email') ->where('email', '!=', '') ->when($months !== null, fn ($q) => $q->whereHas('company', fn ($c) => $c->withoutGlobalScopes()->whereHas('pressReleases', fn ($r) => $r->withoutGlobalScopes()->where('press_releases.created_at', '>=', now()->subMonths($months))))) ->pluck('email'); $known = EmailDomainCheck::query() ->where(fn ($q) => $q->whereNull('expires_at')->orWhere('expires_at', '>', now())) ->pluck('domain') ->flip(); $domains = []; foreach ($companies->merge($contacts) as $email) { $domain = PlausibleEmailDomain::domainOf($email); if ($domain !== null && ! isset($known[$domain])) { $domains[$domain] = true; } } return collect(array_keys($domains))->take($limit)->values(); } }