Files
laraundfelix/app/Domain/Commerce/ShopCatalog.php
T
LarsandClaude Sonnet 5.5 0ac70884cc
CI Feature / test (push) Successful in 5m10s
Deploy Test / test (push) In progress
Zeige auf der Shopkachel Titelbild, Saeulen, Materialart mit Symbol und Marker
- ShopCatalog als gemeinsame Datenquelle der Kacheln, shop/kachel als Partial
- Titelbild nur mit Website-Freigabe (PresentationMedia::coverPresentation), schmueckend mit leerem alt
- Symbol je Materialart (Stammdaten), Marker Neu, Aktualisiert, Beliebt mit Einstellung am Artikel
- Migration 2026_12_01_100000, rein ergaenzend
- ADR-54, Plan, DEPLOYMENT.md

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-26 07:44:47 +01:00

158 lines
7.2 KiB
PHP

<?php
namespace App\Domain\Commerce;
use App\Domain\Catalog\Bundle;
use App\Domain\Catalog\Composition;
use App\Domain\Catalog\EditorialProfileReader;
use App\Domain\Catalog\FormattedText;
use App\Domain\Catalog\LicenseArticle;
use App\Domain\Catalog\Pillar;
use App\Domain\Catalog\PresentationMedia;
use App\Domain\Cms\Catalog\PublicCatalogReader;
use App\Support\Brand\SymbolCatalog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
/**
* Die Produkte des Shops als Kacheln: eine Datenquelle für die Shopseite (und später das Widget).
*
* Gelistet wird, was mindestens eine kaufbare Option hat (Artikel, Pakete mit eigener Seite, Altbestand mit
* Programmseite). Jede Kachel trägt nur öffentlich Zeigbares: Titel und Kurztext aus der Redaktion, das Titelbild nur mit
* Website-Freigabe, Säulen nur, wenn sie veröffentlicht sind, die Materialart mit ihrem Symbol, die Marker und den
* niedrigsten Preis. Nichts davon begründet eine Berechtigung (ADR-52, ADR-54).
*
* @phpstan-type Card array{key: string, kind: string, url: string, title: string, teaser: string, from: string, from_cents: int, count: int, image: ?array{url: string, alt: string}, kind_label: string, symbol: ?string, pillars: list<string>, markers: list<array{key: string, label: string}>}
*/
class ShopCatalog
{
public function __construct(
private readonly ProductOptions $products,
private readonly PublicCatalogReader $catalog,
private readonly EditorialProfileReader $profiles,
private readonly PresentationMedia $presentation,
private readonly ShopMarkers $markers,
) {}
/**
* @return list<array<string, mixed>>
*/
public function cards(string $term = ''): array
{
$term = mb_strtolower(trim($term));
$cards = [];
// Altbestand: Angebote mit mehreren Positionen auf einer freigegebenen Programmseite.
$summaries = $this->products->summaries();
foreach (Composition::query()->with(['editorialContentKind', 'pillars'])->whereIn('id', array_keys($summaries))->orderBy('designation')->get() as $composition) {
if ($this->catalog->shownVersion($composition) === null) {
continue;
}
$title = (string) ($composition->display_title ?: $composition->designation);
$teaser = (string) ($composition->teaser ?: FormattedText::plain($composition->summary));
$pillars = $this->pillarNames($composition->pillars);
$card = $this->card('c:'.$composition->id, 'programm', route('public.program', $composition), $title, $teaser, $summaries[$composition->id], 'Programm', null, $pillars, [], $this->image($composition));
if ($this->matches($term, $title.' '.$teaser.' '.FormattedText::plain($composition->summary).' '.$composition->search_terms.' '.implode(' ', $pillars))) {
$cards[] = $card;
}
}
// Lizenzartikel mit kaufbarer Option.
$articleSummaries = $this->products->articleSummaries();
foreach (LicenseArticle::query()->with(['editorialContentKind', 'pillars', 'currentRelease'])->whereIn('id', array_keys($articleSummaries))->whereNull('archived_at')->orderBy('code')->get() as $article) {
$profile = $this->profiles->read($article, 'kachel');
$title = $this->products->articleTitle($article);
$teaser = (string) ($profile->value('teaser') ?: $profile->value('summary'));
$kind = $article->editorialContentKind;
$pillars = $this->pillarNames($article->pillars);
$offerIds = array_map(fn ($offer): int => (int) $offer->id, $this->products->offersOfArticle($article));
$card = $this->card(
'a:'.$article->id, 'artikel', route('shop.article', $article), $title, $teaser, $articleSummaries[$article->id],
(string) ($kind?->label ?? ''), $this->symbol($kind?->symbol), $pillars, $this->markers->visible($article, $offerIds), $this->image($article),
);
if ($this->matches($term, $title.' '.$teaser.' '.$profile->value('summary').' '.$profile->searchTerms.' '.($kind?->label ?? '').' '.implode(' ', $pillars))) {
$cards[] = $card;
}
}
// Pakete mit eigener Produktseite.
$bundleSummaries = $this->products->bundleSummaries();
foreach (Bundle::query()->whereIn('id', array_keys($bundleSummaries))->orderBy('name')->get() as $bundle) {
$contents = $this->products->bundleContents($this->products->forBundle($bundle));
$names = implode(', ', array_column($contents, 'title'));
$teaser = 'Paket mit '.count($contents).' '.(count($contents) === 1 ? 'Inhalt' : 'Inhalten').($names !== '' ? ': '.$names : '');
$card = $this->card('b:'.$bundle->id, 'paket', route('public.package', $bundle), (string) $bundle->name, $teaser, $bundleSummaries[$bundle->id], 'Paket', null, [], [], $this->image($bundle));
if ($this->matches($term, $bundle->name.' '.$names)) {
$cards[] = $card;
}
}
return $cards;
}
/**
* @param array{from_cents: int, count: int} $summary
* @param list<string> $pillars
* @param list<array{key: string, label: string}> $markers
* @param array{url: string, alt: string}|null $image
* @return array<string, mixed>
*/
private function card(string $key, string $kind, string $url, string $title, string $teaser, array $summary, string $kindLabel, ?string $symbol, array $pillars, array $markers, ?array $image): array
{
return [
'key' => $key,
'kind' => $kind,
'url' => $url,
'title' => $title,
'teaser' => $teaser,
'from' => Money::format($summary['from_cents']),
'from_cents' => (int) $summary['from_cents'],
'count' => (int) $summary['count'],
'image' => $image,
'kind_label' => $kindLabel,
'symbol' => $symbol,
'pillars' => $pillars,
'markers' => $markers,
];
}
private function matches(string $term, string $haystack): bool
{
return $term === '' || str_contains(mb_strtolower($haystack), $term);
}
/**
* Nur veröffentlichte Säulen sind öffentlich (Säulenverwaltung).
*
* @param Collection<int, Pillar> $pillars
* @return list<string>
*/
private function pillarNames(Collection $pillars): array
{
return $pillars->filter(fn (Pillar $pillar): bool => $pillar->published_at !== null)->map(fn (Pillar $pillar): string => (string) $pillar->name)->values()->all();
}
/** Ein Symbol zeigt nur, was die Symbolbibliothek kennt (die Auswahl wird an der Materialart geprüft, hier noch einmal). */
private function symbol(?string $key): ?string
{
return is_string($key) && $key !== '' && array_key_exists($key, SymbolCatalog::options()) ? $key : null;
}
/**
* @return array{url: string, alt: string}|null
*/
private function image(Model $subject): ?array
{
$presentation = $this->presentation->coverPresentation($subject);
return $presentation === null ? null : ['url' => $presentation->url, 'alt' => $presentation->alt];
}
}