runningUnitTests()) { return $callback(); } return Cache::memo()->remember($key, $seconds, $callback); } public function flush(): void { foreach ($this->keys() as $key) { Cache::memo()->forget($key); } } public function forgetPressReleaseStats(): void { Cache::memo()->forget(self::PressReleaseStats); Cache::memo()->forget(self::PressReleaseReviewCount); } /** * Aktiv-/Inaktiv-Kennzahlen einer `is_active`-Tabelle in einer einzigen * Aggregat-Abfrage. Single Source of Truth für die identischen Stat-Blöcke * der Benutzer- und Firmen-Liste. * * @param Builder $query * @return array{total: int, active: int, inactive: int} */ public function activeInactiveStats(Builder $query): array { $stats = $query->toBase() ->selectRaw('COUNT(*) as total') ->selectRaw('SUM(CASE WHEN is_active = ? THEN 1 ELSE 0 END) as active', [true]) ->selectRaw('SUM(CASE WHEN is_active = ? THEN 1 ELSE 0 END) as inactive', [false]) ->first(); return [ 'total' => (int) ($stats->total ?? 0), 'active' => (int) ($stats->active ?? 0), 'inactive' => (int) ($stats->inactive ?? 0), ]; } public function pressReleaseReviewCount(): int { return (int) $this->remember(self::PressReleaseReviewCount, self::StatsTtl, function (): int { return PressRelease::query() ->withoutGlobalScopes() ->where('status', PressReleaseStatus::Review->value) ->count(); }); } public function companiesStatsKey(?string $portal): string { return 'admin.companies.stats.'.($portal ?? 'all'); } public function contactsStatsKey(?string $portal): string { return 'admin.contacts.stats.'.($portal ?? 'all'); } public function categoriesStatsKey(?string $portal): string { return 'admin.categories.stats.'.($portal ?? 'all'); } public function permissionGroupsKey(string $guard): string { return "admin.permissions.groups.{$guard}"; } /** * @return list */ private function keys(): array { $portalKeys = collect([null, ...array_map( static fn (Portal $portal): string => $portal->value, Portal::cases(), )]); return [ self::DashboardStats, self::PressReleaseStats, self::PressReleaseReviewCount, self::PressReleaseCategoryOptions, self::ActiveCategoryOptions, self::RoleOptions, self::UserStats, self::NewsletterStats, self::PresetAreas, self::PresetTypes, $this->permissionGroupsKey('web'), ...$portalKeys->map(fn (?string $portal): string => $this->companiesStatsKey($portal))->all(), ...$portalKeys->map(fn (?string $portal): string => $this->contactsStatsKey($portal))->all(), ...$portalKeys->map(fn (?string $portal): string => $this->categoriesStatsKey($portal))->all(), ]; } }