Führe Marken an der Filmfassung und einen Arbeitsstand am Element ein
CI Feature / test (push) Successful in 5m40s

Die Marken eines Films liegen als metadata['markers'] an der Fassung. Bis eine
Fassung entsteht, gibt es einen Arbeitsstand in content_elements.marker_draft
(neue, leere Spalte, rückwärtskompatibel): { base_version, markers, saved_at }.
Mitglieder sehen ihn nie, und er wird nicht gesondert protokolliert.

- FilmMarkers prüft und vereinheitlicht (Zeit auf 0,1 s, Titel, Mindestabstand
  1 s, höchstens 100) und liest gespeicherte Daten reparierend. Der Altwert
  decision_seconds gilt nur, solange der Schlüssel markers fehlt; eine
  gelöschte Liste bringt ihn nicht zurück.
- MarkerDraft speichert den Arbeitsstand gegen gleichzeitiges Speichern
  (saved_at als Marke) und räumt nur den Stand ab, der gelesen wurde.
- ContentService übernimmt die Marken der bisherigen Fassung, wenn eine Fassung
  mit Datei ohne Marken angelegt wird. Vorher ging jede Fassung aus anderem
  Anlass (Text, Notiz, neue Datei) ohne Marken hervor, weil die Aufrufer die
  Metadaten selbst zusammenstellen.
- ElementRevision wendet den Arbeitsstand an. Eine Änderung nur an den Marken
  zählt als Änderung, ohne Datei entsteht keine Fassung nur aus Marken.
  Das Formularfeld decision_seconds entfällt.

Bewusste Änderung eines Tests: test_an_uploaded_file_becomes_a_ready_version
setzte decision_seconds über das Formular; der Wert kommt jetzt als Marke aus
dem Arbeitsstand.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
StommerandClaude Sonnet 5.5 committed 2026-12-01 21:02:35 +01:00
1 parent e91c7f7ebb
commit 3c809b6cc8
10 files changed
+972 -22

No files matched your search

+240
View File
@@ -0,0 +1,240 @@
<?php
namespace App\Domain\Media;
use App\Domain\Catalog\ContentElement;
use App\Domain\Catalog\ContentElementVersion;
use App\Domain\Catalog\DomainRuleException;
/**
* Marken eines Films: Sprungmarken und Stopps an der Filmfassung (`metadata['markers']`).
*
* Eine Marke: t (Sekunden, auf 0,1 gerundet), label (Titel), stop (hält die Wiedergabe an), prompt (einfacher Text,
* optional). Kapitel sind Marken ohne stop. Eine Liste je Fassung, kein zweites Feld.
*
* Eine Stelle löst die Altlast auf: Der frühere Einzelwert `decision_seconds` gilt nur, solange die Fassung den
* Schlüssel `markers` nicht hat. Hat sie ihn (auch leer), zählt er allein, damit gelöschte Marken nicht zurückkehren.
*
* Prüfen und Reparieren sind getrennt: `normalize` lehnt Fehler ab (Speichern), `ofVersion` repariert gespeicherte
* Daten, damit eine beschädigte Liste nie eine Seite zerstört (Lesen).
*/
final class FilmMarkers
{
public const LEGACY_LABEL = 'Entscheidung';
public const MIN_GAP = 1.0;
public const MAX_MARKERS = 100;
public const MAX_LABEL = 120;
public const MAX_PROMPT = 1000;
/**
* Prüft und vereinheitlicht eine Liste zum Speichern.
*
* @return list<array{t: float, label: string, stop: bool, prompt: string}>
*
* @throws DomainRuleException
*/
public static function normalize(mixed $raw): array
{
if (! is_array($raw)) {
throw new DomainRuleException('Die Marken müssen als Liste vorliegen.');
}
if (count($raw) > self::MAX_MARKERS) {
throw new DomainRuleException('Ein Film kann höchstens '.self::MAX_MARKERS.' Marken haben.');
}
$list = [];
foreach (array_values($raw) as $index => $entry) {
$list[] = self::one($entry, $index + 1);
}
usort($list, fn (array $a, array $b): int => $a['t'] <=> $b['t']);
for ($i = 1; $i < count($list); $i++) {
if ($list[$i]['t'] - $list[$i - 1]['t'] < self::MIN_GAP - 1e-9) {
throw new DomainRuleException(
'Zwei Marken brauchen mindestens '.self::format(self::MIN_GAP).' Sekunde Abstand ('
.self::format($list[$i - 1]['t']).' s und '.self::format($list[$i]['t']).' s).',
);
}
}
return $list;
}
/**
* Marken, die zu einer Fassung gehören, so wie sie angezeigt und ausgeliefert werden.
*
* @return list<array{t: float, label: string, stop: bool, prompt: string}>
*/
public static function ofVersion(?ContentElementVersion $version): array
{
$metadata = is_array($version?->metadata) ? $version->metadata : [];
if (array_key_exists('markers', $metadata)) {
return is_array($metadata['markers']) ? self::repair($metadata['markers']) : [];
}
$legacy = $metadata['decision_seconds'] ?? null;
if (is_numeric($legacy) && (float) $legacy > 0) {
return [['t' => self::round((float) $legacy), 'label' => self::LEGACY_LABEL, 'stop' => true, 'prompt' => '']];
}
return [];
}
/** Ob die Fassung die Marken ausdrücklich führt (auch als leere Liste). */
public static function hasMarkers(?ContentElementVersion $version): bool
{
return is_array($version?->metadata) && array_key_exists('markers', $version->metadata);
}
/**
* @param list<array<string, mixed>> $a
* @param list<array<string, mixed>> $b
*/
public static function same(array $a, array $b): bool
{
return json_encode(self::repair($a)) === json_encode(self::repair($b));
}
/**
* Hinweis nach einer neuen Datei: Die Fassung hat eine andere Datei als davor, die Marken sind seither aber
* unverändert und nicht leer. Sie stammen dann vom alten Film und müssen geprüft werden. Nichts davon wird
* gespeichert, es folgt aus den Fassungen.
*/
public static function fileChangedWithoutMarkers(ContentElement $element): bool
{
$versions = $element->versions()->orderByDesc('number')->get();
$latest = $versions->first();
if ($latest === null) {
return false;
}
$markers = self::ofVersion($latest);
if ($markers === []) {
return false;
}
foreach ($versions->values() as $position => $version) {
$previous = $versions->get($position + 1);
if ($previous === null) {
return false;
}
if (! self::same(self::ofVersion($version), $markers)) {
return false;
}
if (self::fileOf($version) !== self::fileOf($previous)) {
return self::same(self::ofVersion($previous), $markers);
}
}
return false;
}
/**
* Zeit wie angezeigt: ohne überflüssige Nachkommastelle, mit Komma.
*/
public static function format(float $seconds): string
{
$text = number_format(self::round($seconds), 1, ',', '');
return str_ends_with($text, ',0') ? substr($text, 0, -2) : $text;
}
public static function round(float $seconds): float
{
return round($seconds, 1);
}
private static function fileOf(ContentElementVersion $version): ?string
{
$file = is_array($version->metadata) ? ($version->metadata['file'] ?? null) : null;
return is_string($file) && $file !== '' ? $file : null;
}
/**
* @return array{t: float, label: string, stop: bool, prompt: string}
*/
private static function one(mixed $entry, int $number): array
{
if (! is_array($entry)) {
throw new DomainRuleException('Marke '.$number.' ist keine gültige Marke.');
}
$time = $entry['t'] ?? null;
if (! is_numeric($time) || ! is_finite((float) $time) || (float) $time < 0) {
throw new DomainRuleException('Marke '.$number.': Die Zeit muss eine Zahl ab 0 Sekunden sein.');
}
$label = trim((string) ($entry['label'] ?? ''));
if ($label === '' || mb_strlen($label) > self::MAX_LABEL) {
throw new DomainRuleException('Marke '.$number.': Der Titel braucht 1 bis '.self::MAX_LABEL.' Zeichen.');
}
$prompt = trim((string) ($entry['prompt'] ?? ''));
if (mb_strlen($prompt) > self::MAX_PROMPT) {
throw new DomainRuleException('Marke '.$number.': Der Text darf höchstens '.self::MAX_PROMPT.' Zeichen haben.');
}
return [
't' => self::round((float) $time),
'label' => $label,
'stop' => ($entry['stop'] ?? false) === true,
'prompt' => $prompt,
];
}
/**
* Lesen ohne Ausnahmen: Unbrauchbares entfällt, zu dichte Marken weichen der früheren.
*
* @param array<int|string, mixed> $raw
* @return list<array{t: float, label: string, stop: bool, prompt: string}>
*/
private static function repair(array $raw): array
{
$valid = [];
foreach ($raw as $entry) {
try {
$valid[] = self::one($entry, 0);
} catch (DomainRuleException) {
continue;
}
}
usort($valid, fn (array $a, array $b): int => $a['t'] <=> $b['t']);
$result = [];
foreach ($valid as $marker) {
$last = $result === [] ? null : $result[count($result) - 1];
if ($last !== null && self::MIN_GAP - 1e-9 > $marker['t'] - $last['t']) {
if ($marker['stop'] && ! $last['stop']) {
$result[count($result) - 1] = $marker;
}
continue;
}
$result[] = $marker;
}
return array_slice($result, 0, self::MAX_MARKERS);
}
}