- Navigations-Chrome (CategoryNavigation), Suche- und Newsroom-Seitendaten 300s gecacht; gemeinsamer WebPageCache-Helper (eine TTL/Key-Konvention). - Cache-Poisoning über ?page= geschlossen (Key an Paginator ausgerichtet, kein withQueryString im Cache); Newsrooms cachet nur den ungefilterten Fall und die ersten Seiten (kein unbegrenzter Key-Raum). - Detailseiten schreiben Hits/View-Events deferred nach der Response (M12); OpenAI-/Markt-/USt-ID-Aufrufe mit Connect-Timeouts und begrenzten Retries; Admin-Memo-Caches konsistent invalidiert. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
160 lines
5.7 KiB
PHP
160 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Web;
|
|
|
|
use App\Models\Category;
|
|
use App\Models\Company;
|
|
use App\Models\PressRelease;
|
|
use App\Scopes\PortalScope;
|
|
use App\Support\WebPageCache;
|
|
use App\Support\WebPortalContext;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Pagination\Paginator;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
/**
|
|
* Datenaufbereitung der Newsroom-Markenseite eines Unternehmens je Ausgabe:
|
|
* alle veröffentlichten Meldungen der Firma plus Kennzahlen. Ohne aktives
|
|
* Unternehmen oder ohne veröffentlichte Meldung gibt es keine öffentliche Seite
|
|
* (404).
|
|
*/
|
|
class NewsroomDetailPageData
|
|
{
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function build(WebPortalContext $context, string $slug): array
|
|
{
|
|
// Seitennummer exakt so auflösen wie der Paginator (Paginator::
|
|
// resolveCurrentPage validiert und fällt bei ungültigen Werten auf 1
|
|
// zurück). Sonst landet Seite-1-Inhalt unter einem anderen Seiten-Key
|
|
// (Cache-Poisoning über z. B. ?page=2.5).
|
|
$page = Paginator::resolveCurrentPage();
|
|
|
|
$cacheKey = WebPageCache::key('newsroom', [
|
|
implode('-', $context->portalValues),
|
|
$context->language,
|
|
$slug,
|
|
'page',
|
|
$page,
|
|
]);
|
|
|
|
return WebPageCache::remember(
|
|
$cacheKey,
|
|
fn (): array => $this->buildUncached($context, $slug),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function buildUncached(WebPortalContext $context, string $slug): array
|
|
{
|
|
$portalValues = $context->portalValues;
|
|
$language = $context->language;
|
|
|
|
$company = Company::query()
|
|
->withoutGlobalScope(PortalScope::class)
|
|
->whereIn('portal', $portalValues)
|
|
->where('is_active', true)
|
|
->where('slug', $slug)
|
|
->with(['contacts'])
|
|
->first();
|
|
|
|
if (! $company) {
|
|
abort(404);
|
|
}
|
|
|
|
$base = static fn (): Builder => PressRelease::query()
|
|
->withoutGlobalScope(PortalScope::class)
|
|
->where('company_id', $company->id)
|
|
->forPortalValues($portalValues)
|
|
->forLanguage($language)
|
|
->visibleInArchive();
|
|
|
|
// Keine öffentliche Newsroom-Seite ohne veröffentlichte Meldung.
|
|
if (! $base()->exists()) {
|
|
abort(404);
|
|
}
|
|
|
|
$latest = $base()->max('published_at');
|
|
$referenceNow = $latest ? Carbon::parse($latest) : now();
|
|
if ($referenceNow->gt(now())) {
|
|
$referenceNow = now();
|
|
}
|
|
$recentSince = $referenceNow->copy()->subDays(30);
|
|
$oldest = $base()->min('published_at');
|
|
|
|
$with = [
|
|
'company',
|
|
'category.translations' => fn ($query) => $query->where('locale', $language),
|
|
'images' => fn ($query) => $query->orderByDesc('is_preview')->orderBy('sort_order')->limit(1),
|
|
];
|
|
|
|
$leadRelease = $base()->with($with)->orderByDesc('published_at')->first();
|
|
|
|
// Nur den Seiten-Parameter an die Pagination-Links hängen (kein
|
|
// ->withQueryString()): sonst backt der erste Besucher seine beliebigen
|
|
// Query-Parameter (utm_* o. Ä.) in die für alle gecachten Links.
|
|
$releases = $base()
|
|
->with($with)
|
|
->when($leadRelease, fn (Builder $query) => $query->whereKeyNot($leadRelease->getKey()))
|
|
->orderByDesc('published_at')
|
|
->paginate(10);
|
|
|
|
$mostReadReleases = $base()
|
|
->orderByDesc('hits')
|
|
->orderByDesc('published_at')
|
|
->limit(5)
|
|
->get(['id', 'slug', 'title', 'hits', 'portal', 'language']);
|
|
|
|
$companyFilter = static function (Builder $query) use ($company, $portalValues, $language): void {
|
|
$query
|
|
->withoutGlobalScope(PortalScope::class)
|
|
->where('company_id', $company->id)
|
|
->forPortalValues($portalValues)
|
|
->forLanguage($language)
|
|
->published();
|
|
};
|
|
|
|
$categories = Category::query()
|
|
->whereIn('portal', $portalValues)
|
|
->whereHas('pressReleases', fn (Builder $query) => $companyFilter($query))
|
|
->with(['translations' => fn ($query) => $query->where('locale', $language)])
|
|
->withCount(['pressReleases as releases_count' => fn (Builder $query) => $companyFilter($query)])
|
|
->orderByDesc('releases_count')
|
|
->limit(8)
|
|
->get()
|
|
->map(function (Category $category): ?array {
|
|
$translation = $category->translations->first();
|
|
|
|
if (! $translation) {
|
|
return null;
|
|
}
|
|
|
|
return [
|
|
'name' => $translation->name,
|
|
'href' => $translation->slug ? route('kategorie', ['slug' => $translation->slug]) : route('web.home'),
|
|
'count' => (int) ($category->releases_count ?? 0),
|
|
];
|
|
})
|
|
->filter()
|
|
->values();
|
|
|
|
return [
|
|
'company' => $company,
|
|
'referenceNow' => $referenceNow,
|
|
'leadRelease' => $leadRelease,
|
|
'releases' => $releases,
|
|
'mostReadReleases' => $mostReadReleases,
|
|
'categories' => $categories,
|
|
'newsroomStats' => [
|
|
'total' => $base()->count(),
|
|
'recent' => $base()->where('published_at', '>=', $recentSince)->count(),
|
|
'today' => $base()->whereBetween('published_at', [$referenceNow->copy()->startOfDay(), $referenceNow->copy()->endOfDay()])->count(),
|
|
'totalHits' => (int) $base()->sum('hits'),
|
|
'sinceYear' => $oldest ? (int) Carbon::parse($oldest)->format('Y') : null,
|
|
],
|
|
];
|
|
}
|
|
}
|