Files
laraundfelix/app/Domain/Catalog/CompositionRelease.php
LarsandCursor dd67741f35
Deploy Test / test (push) Failing after 9s
Deploy Test / deploy (push) Skipped
Halte deaktivierte Bestandteile im Aufbau, damit die Freigabe sie anzeigt.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-17 13:34:38 +01:00

479 lines
17 KiB
PHP

<?php
namespace App\Domain\Catalog;
use App\Models\User;
use Illuminate\Support\Facades\DB;
class CompositionRelease
{
/** @var array<int, CompositionVersion> */
private array $publishedDrafts = [];
/**
* @param list<array<string, mixed>> $members
* @return array{
* host_label: string,
* title: string,
* next_label: string,
* ready: list<array{title: string, detail: string, depth: int}>,
* pending: list<array{title: string, detail: string, depth: int}>,
* blocked: list<array{title: string, detail: string, depth: int, edit: array{type: string, id: int}|null}>,
* needs_consent: bool,
* nested_draft: bool
* }
*/
public function review(Composition $host, array $members): array
{
$ready = [];
$pending = [];
$blocked = [];
$nestedDraft = false;
$this->inspectMembers($members, 0, [$host->id], false, $ready, $pending, $blocked, $nestedDraft);
return [
'host_label' => $this->label($host),
'title' => $this->title($host),
'next_label' => 'Fassung '.(((int) $host->versions()->max('number')) + 1),
'ready' => $ready,
'pending' => $pending,
'blocked' => $blocked,
'needs_consent' => $pending !== [],
'nested_draft' => $nestedDraft,
];
}
/**
* @param list<array<string, mixed>> $members
* @param array{learning_goal?: mixed, duration_minutes?: mixed, material_note?: mixed} $didactic
*/
public function publish(
Composition $host,
array $members,
?User $actor,
?string $note,
array $didactic,
bool $consent,
): CompositionVersion {
$review = $this->review($host, $members);
if ($review['blocked'] !== []) {
throw new DomainRuleException($review['blocked'][0]['detail']);
}
if ($review['needs_consent'] && ! $consent) {
throw new DomainRuleException('Noch nicht freigegebene Bestandteile werden nur mit ausdrücklicher Zustimmung freigegeben.');
}
$this->publishedDrafts = [];
return DB::transaction(function () use ($host, $members, $actor, $note, $didactic): CompositionVersion {
return $this->materialize($host, $members, $didactic, $actor, $note, []);
});
}
/**
* @param list<array<string, mixed>> $members
* @param list<int> $stack
* @param list<array{title: string, detail: string, depth: int}> $ready
* @param list<array{title: string, detail: string, depth: int}> $pending
* @param list<array{title: string, detail: string, depth: int, edit: array{type: string, id: int}|null}> $blocked
*/
private function inspectMembers(
array $members,
int $depth,
array $stack,
bool $frozen,
array &$ready,
array &$pending,
array &$blocked,
bool &$nestedDraft,
): void {
foreach ($members as $member) {
if (! is_array($member)) {
continue;
}
$role = trim((string) ($member['role'] ?? ''));
$target = (string) ($member['target'] ?? '');
if ($role === '' || $target === '') {
$blocked[] = $this->block('Bestandteil', 'Jeder Bestandteil braucht eine Rolle und eine vorhandene Fassung.', $depth, null);
continue;
}
$this->inspectTarget($target, $role, $depth, $stack, $frozen, $ready, $pending, $blocked, $nestedDraft);
}
}
/**
* @param list<int> $stack
* @param list<array{title: string, detail: string, depth: int}> $ready
* @param list<array{title: string, detail: string, depth: int}> $pending
* @param list<array{title: string, detail: string, depth: int, edit: array{type: string, id: int}|null}> $blocked
*/
private function inspectTarget(
string $target,
string $role,
int $depth,
array $stack,
bool $frozen,
array &$ready,
array &$pending,
array &$blocked,
bool &$nestedDraft,
): void {
if (preg_match('/^element:(\d+)$/', $target, $match) === 1) {
$this->inspectElement((int) $match[1], $role, $depth, $frozen, $ready, $pending, $blocked);
return;
}
if (preg_match('/^composition:(\d+)$/', $target, $match) === 1) {
$this->inspectReleasedComposition((int) $match[1], $role, $depth, $stack, $ready, $pending, $blocked, $nestedDraft);
return;
}
if (preg_match('/^composition-draft:(\d+)$/', $target, $match) === 1) {
$this->inspectDraftComposition((int) $match[1], $role, $depth, $stack, $frozen, $ready, $pending, $blocked, $nestedDraft);
return;
}
$blocked[] = $this->block('Bestandteil', 'Ein Bestandteil ist nicht mehr vorhanden.', $depth, null);
}
/**
* @param list<array{title: string, detail: string, depth: int}> $ready
* @param list<array{title: string, detail: string, depth: int}> $pending
* @param list<array{title: string, detail: string, depth: int, edit: array{type: string, id: int}|null}> $blocked
*/
private function inspectElement(
int $id,
string $role,
int $depth,
bool $frozen,
array &$ready,
array &$pending,
array &$blocked,
): void {
$version = ContentElementVersion::query()->with('element')->find($id);
$title = $version?->element === null
? 'Medium'
: (filled($version->element->display_title) ? $version->element->display_title : $version->element->working_title);
$edit = $version?->element === null ? null : ['type' => 'element', 'id' => $version->element->id];
if (! $version instanceof ContentElementVersion || $version->element === null) {
$blocked[] = $this->block($title, 'Ein Medium ist nicht mehr vorhanden.', $depth, null);
return;
}
$kind = $this->outputLabel((string) $version->element->output_class);
if ($version->status === ContentVersionStatus::Bereit) {
$ready[] = $this->line($title, $this->kindAndRole($kind, $role).' · Fassung '.$version->number.', bleibt unverändert', $depth);
return;
}
if ($version->status === ContentVersionStatus::Gesperrt) {
$blocked[] = $this->block($title, $this->kindAndRole($kind, $role).'. Diese Fassung ist deaktiviert und kann nicht freigegeben werden.', $depth, $edit);
return;
}
$problem = app(ContentService::class)->elementReleaseProblem($version);
if ($frozen) {
$blocked[] = $this->block($title, $this->kindAndRole($kind, $role).'. Eine bereits freigegebene Fassung verweist auf diesen Entwurf. Sie bleibt unverändert.', $depth, $edit);
return;
}
if ($problem !== null) {
$blocked[] = $this->block($title, $this->kindAndRole($kind, $role).'. '.$problem, $depth, $edit);
return;
}
$pending[] = $this->line($title, $this->kindAndRole($kind, $role).'. Der Entwurf wird freigegeben. Es entsteht keine weitere Fassung.', $depth);
}
/**
* @param list<int> $stack
* @param list<array{title: string, detail: string, depth: int}> $ready
* @param list<array{title: string, detail: string, depth: int}> $pending
* @param list<array{title: string, detail: string, depth: int, edit: array{type: string, id: int}|null}> $blocked
*/
private function inspectReleasedComposition(
int $id,
string $role,
int $depth,
array $stack,
array &$ready,
array &$pending,
array &$blocked,
bool &$nestedDraft,
): void {
$version = CompositionVersion::query()->with('composition', 'members')->find($id);
if (! $version instanceof CompositionVersion || $version->composition === null) {
$blocked[] = $this->block('Modul', 'Ein Bestandteil ist nicht mehr vorhanden.', $depth, null);
return;
}
$title = $this->title($version->composition);
$kind = $this->label($version->composition);
if (in_array($version->composition_id, $stack, true)) {
$blocked[] = $this->block($title, $this->kindAndRole($kind, $role).'. Der Aufbau verweist auf sich selbst. So kann nichts freigegeben werden.', $depth, ['type' => 'composition', 'id' => $version->composition_id]);
return;
}
if ($version->status !== ContentVersionStatus::Bereit) {
$blocked[] = $this->block($title, $this->kindAndRole($kind, $role).'. Diese Fassung ist nicht freigegeben.', $depth, ['type' => 'composition', 'id' => $version->composition_id]);
return;
}
$ready[] = $this->line($title, $this->kindAndRole($kind, $role).' · Fassung '.$version->number.', bleibt unverändert', $depth);
$nested = [];
foreach ($version->members()->orderBy('position')->get() as $member) {
$target = $member->content_element_version_id
? 'element:'.$member->content_element_version_id
: 'composition:'.$member->nested_composition_version_id;
$nested[] = ['role' => (string) $member->role, 'target' => $target, 'note' => (string) ($member->placement_note ?? '')];
}
$this->inspectMembers($nested, $depth + 1, [...$stack, $version->composition_id], true, $ready, $pending, $blocked, $nestedDraft);
}
/**
* @param list<int> $stack
* @param list<array{title: string, detail: string, depth: int}> $ready
* @param list<array{title: string, detail: string, depth: int}> $pending
* @param list<array{title: string, detail: string, depth: int, edit: array{type: string, id: int}|null}> $blocked
*/
private function inspectDraftComposition(
int $id,
string $role,
int $depth,
array $stack,
bool $frozen,
array &$ready,
array &$pending,
array &$blocked,
bool &$nestedDraft,
): void {
$composition = Composition::query()->find($id);
$edit = ['type' => 'composition', 'id' => $id];
if (! $composition instanceof Composition) {
$blocked[] = $this->block('Modul', 'Ein Modul ist nicht mehr vorhanden.', $depth, null);
return;
}
$title = $this->title($composition);
$kind = $this->label($composition);
if ($frozen || in_array($id, $stack, true)) {
$blocked[] = $this->block($title, $this->kindAndRole($kind, $role).'. Der Aufbau verweist auf sich selbst. So kann nichts freigegeben werden.', $depth, $edit);
return;
}
$draft = is_array($composition->working_draft) ? $composition->working_draft : [];
$members = is_array($draft['members'] ?? null) ? $draft['members'] : [];
if ($members === []) {
$blocked[] = $this->block($title, $this->kindAndRole($kind, $role).'. Der gespeicherte Entwurf hat keinen Bestandteil.', $depth, $edit);
return;
}
$minutes = $draft['duration_minutes'] ?? null;
if ($minutes !== null && $minutes !== '' && ! is_int($minutes)) {
$blocked[] = $this->block($title, $this->kindAndRole($kind, $role).'. Der Zeitbedarf ist keine Zahl von Minuten.', $depth, $edit);
return;
}
$nestedDraft = true;
$number = ((int) $composition->versions()->max('number')) + 1;
$pending[] = $this->line($title, $this->kindAndRole($kind, $role).'. Es entsteht Fassung '.$number.'.', $depth);
$this->inspectMembers($members, $depth + 1, [...$stack, $id], false, $ready, $pending, $blocked, $nestedDraft);
}
/**
* @param list<array<string, mixed>> $members
* @param array{learning_goal?: mixed, duration_minutes?: mixed, material_note?: mixed} $didactic
* @param list<int> $stack
*/
private function materialize(
Composition $composition,
array $members,
array $didactic,
?User $actor,
?string $note,
array $stack,
): CompositionVersion {
if (isset($this->publishedDrafts[$composition->id])) {
return $this->publishedDrafts[$composition->id];
}
if (in_array($composition->id, $stack, true)) {
throw new DomainRuleException('Der Aufbau verweist auf sich selbst. So kann nichts freigegeben werden.');
}
$stack[] = $composition->id;
$payload = [];
foreach (array_values($members) as $member) {
$payload[] = [
'role' => trim((string) ($member['role'] ?? '')),
'note' => trim((string) ($member['note'] ?? '')),
'target' => $this->resolveTarget((string) ($member['target'] ?? ''), $actor, $stack),
];
}
$version = app(ContentService::class)->publishComposition(
$composition,
$payload,
$actor,
$note,
[
'learning_goal' => $didactic['learning_goal'] ?? null,
'duration_minutes' => $didactic['duration_minutes'] ?? null,
'material_note' => $didactic['material_note'] ?? null,
],
);
$this->publishedDrafts[$composition->id] = $version;
return $version;
}
/**
* @param list<int> $stack
*/
private function resolveTarget(string $target, ?User $actor, array $stack): ContentElementVersion|CompositionVersion
{
if (preg_match('/^element:(\d+)$/', $target, $match) === 1) {
$version = ContentElementVersion::query()->find($match[1]);
if (! $version instanceof ContentElementVersion) {
throw new DomainRuleException('Ein Medium ist nicht mehr vorhanden.');
}
if ($version->status === ContentVersionStatus::Entwurf) {
$version = app(ContentService::class)->releaseElementVersion($version);
}
if ($version->status !== ContentVersionStatus::Bereit) {
throw new DomainRuleException('Eine deaktivierte Fassung lässt sich nicht freigeben.');
}
return $version;
}
if (preg_match('/^composition:(\d+)$/', $target, $match) === 1) {
$version = CompositionVersion::query()->find($match[1]);
if (! $version instanceof CompositionVersion || $version->status !== ContentVersionStatus::Bereit) {
throw new DomainRuleException('Eine bereits erwartete Fassung ist nicht mehr freigegeben.');
}
return $version;
}
if (preg_match('/^composition-draft:(\d+)$/', $target, $match) !== 1) {
throw new DomainRuleException('Ein Bestandteil ist nicht mehr vorhanden.');
}
$composition = Composition::query()->find($match[1]);
if (! $composition instanceof Composition) {
throw new DomainRuleException('Ein Modul ist nicht mehr vorhanden.');
}
if (isset($this->publishedDrafts[$composition->id])) {
return $this->publishedDrafts[$composition->id];
}
$draft = is_array($composition->working_draft) ? $composition->working_draft : [];
$version = $this->materialize(
$composition,
is_array($draft['members'] ?? null) ? $draft['members'] : [],
[
'learning_goal' => $draft['learning_goal'] ?? null,
'duration_minutes' => $draft['duration_minutes'] ?? null,
'material_note' => $draft['material_note'] ?? null,
],
$actor,
is_string($draft['revision_note'] ?? null) ? $draft['revision_note'] : null,
$stack,
);
app(EditorialWorkingDraft::class)->clear($composition);
return $version;
}
/**
* @return array{title: string, detail: string, depth: int}
*/
private function line(string $title, string $detail, int $depth): array
{
return ['title' => $title, 'detail' => $detail, 'depth' => $depth];
}
/**
* @param array{type: string, id: int}|null $edit
* @return array{title: string, detail: string, depth: int, edit: array{type: string, id: int}|null}
*/
private function block(string $title, string $detail, int $depth, ?array $edit): array
{
return ['title' => $title, 'detail' => $detail, 'depth' => $depth, 'edit' => $edit];
}
private function kindAndRole(string $kind, string $role): string
{
$role = trim($role);
if ($role === '' || mb_strtolower($role) === mb_strtolower($kind)) {
return $kind;
}
return $kind.' · '.$role;
}
private function title(Composition $composition): string
{
return filled($composition->display_title) ? (string) $composition->display_title : (string) $composition->designation;
}
private function outputLabel(string $outputClass): string
{
return match ($outputClass) {
'film' => 'Film',
'audio' => 'Hörstück',
'bild' => 'Bild',
'document', 'pdf' => 'Dokument',
default => 'Text',
};
}
private function label(Composition $composition): string
{
$kind = $composition->kind;
return ($kind instanceof CompositionKind ? $kind : CompositionKind::from((string) $kind))->deskLabel();
}
}