diff --git a/app/Domain/Catalog/CompositionDisplayRelease.php b/app/Domain/Catalog/CompositionDisplayRelease.php new file mode 100644 index 0000000..08b07ab --- /dev/null +++ b/app/Domain/Catalog/CompositionDisplayRelease.php @@ -0,0 +1,67 @@ +latestEvent($version)?->event === CompositionVersionDisplayEvent::RELEASED; + } + + public function highestReleasedVersion(Composition $composition): ?CompositionVersion + { + return $composition->versions() + ->where('status', ContentVersionStatus::Bereit) + ->orderByDesc('number') + ->get() + ->first(fn (CompositionVersion $version): bool => $this->isReleased($version)); + } + + public function release(CompositionVersion $version, User $actor, ?string $note = null): CompositionVersionDisplayEvent + { + $this->assertBereit($version); + + return DB::transaction(function () use ($version, $actor, $note): CompositionVersionDisplayEvent { + return $version->displayEvents()->create([ + 'event' => CompositionVersionDisplayEvent::RELEASED, + 'actor_id' => $actor->id, + 'occurred_at' => now(), + 'note' => $note !== null && trim($note) !== '' ? trim($note) : null, + ]); + }); + } + + public function revoke(CompositionVersion $version, User $actor, ?string $note = null): CompositionVersionDisplayEvent + { + $this->assertBereit($version); + + if (! $this->isReleased($version)) { + throw new DomainRuleException('Diese Fassung ist nicht zur Anzeige freigegeben.'); + } + + return DB::transaction(function () use ($version, $actor, $note): CompositionVersionDisplayEvent { + return $version->displayEvents()->create([ + 'event' => CompositionVersionDisplayEvent::REVOKED, + 'actor_id' => $actor->id, + 'occurred_at' => now(), + 'note' => $note !== null && trim($note) !== '' ? trim($note) : null, + ]); + }); + } + + public function latestEvent(CompositionVersion $version): ?CompositionVersionDisplayEvent + { + return $version->displayEvents()->orderByDesc('occurred_at')->orderByDesc('id')->first(); + } + + private function assertBereit(CompositionVersion $version): void + { + if ($version->status !== ContentVersionStatus::Bereit) { + throw new DomainRuleException('Nur eine bereitgestellte Kompositionsfassung kann freigegeben werden.'); + } + } +} \ No newline at end of file diff --git a/app/Domain/Catalog/CompositionStructureReader.php b/app/Domain/Catalog/CompositionStructureReader.php new file mode 100644 index 0000000..b8e5352 --- /dev/null +++ b/app/Domain/Catalog/CompositionStructureReader.php @@ -0,0 +1,82 @@ + + */ + public function tree(CompositionVersion $version): array + { + $version->loadMissing([ + 'members.contentElementVersion.element.editorialContentKind', + 'members.nestedCompositionVersion.composition.editorialContentKind', + ]); + + return $this->members($version); + } + + /** + * @return list + */ + private function members(CompositionVersion $version, array $stack = []): array + { + if (in_array($version->composition_id, $stack, true)) { + throw new DomainRuleException('Kompositionen dürfen keinen Zyklus bilden.'); + } + + $stack[] = $version->composition_id; + $nodes = []; + + foreach ($version->members as $member) { + if ($member->content_element_version_id !== null) { + $elementVersion = $member->contentElementVersion; + $elementVersion?->loadMissing('element.editorialContentKind'); + $element = $elementVersion?->element; + + $nodes[] = new StructureNode( + role: $member->role, + position: $member->position, + compositionVersionId: null, + compositionId: null, + contentElementVersionId: $elementVersion?->id, + contentElementId: $element?->id, + editorialKindLabel: $element?->editorialContentKind?->label, + technicalKindLabel: $element?->output_class, + outputClass: $element?->output_class, + title: $element?->working_title ?? $member->role, + ); + + continue; + } + + $nested = $member->nestedCompositionVersion; + + if ($nested === null) { + throw new DomainRuleException('Ein Kompositionsmitglied braucht genau ein Ziel.'); + } + + $nested->loadMissing('composition.editorialContentKind', 'composition'); + $composition = $nested->composition; + + $nodes[] = new StructureNode( + role: $member->role, + position: $member->position, + compositionVersionId: $nested->id, + compositionId: $composition->id, + contentElementVersionId: null, + contentElementId: null, + editorialKindLabel: $composition->editorialContentKind?->label, + technicalKindLabel: $composition->kind instanceof CompositionKind + ? $composition->kind->label() + : null, + outputClass: null, + title: $composition->designation, + children: $this->members($nested, $stack), + ); + } + + return $nodes; + } +} \ No newline at end of file diff --git a/app/Domain/Catalog/CompositionVersion.php b/app/Domain/Catalog/CompositionVersion.php index 879c819..ce3d731 100644 --- a/app/Domain/Catalog/CompositionVersion.php +++ b/app/Domain/Catalog/CompositionVersion.php @@ -61,4 +61,9 @@ public function attestations(): HasMany { return $this->hasMany(CompatibilityAttestation::class); } + + public function displayEvents(): HasMany + { + return $this->hasMany(CompositionVersionDisplayEvent::class)->orderBy('occurred_at'); + } } diff --git a/app/Domain/Catalog/CompositionVersionDisplayEvent.php b/app/Domain/Catalog/CompositionVersionDisplayEvent.php new file mode 100644 index 0000000..00cb9e6 --- /dev/null +++ b/app/Domain/Catalog/CompositionVersionDisplayEvent.php @@ -0,0 +1,39 @@ + 'datetime', + ]; + } + + public function version(): BelongsTo + { + return $this->belongsTo(CompositionVersion::class, 'composition_version_id'); + } + + public function actor(): BelongsTo + { + return $this->belongsTo(User::class, 'actor_id'); + } +} \ No newline at end of file diff --git a/app/Domain/Catalog/StructureNode.php b/app/Domain/Catalog/StructureNode.php new file mode 100644 index 0000000..8e2c82d --- /dev/null +++ b/app/Domain/Catalog/StructureNode.php @@ -0,0 +1,58 @@ + + * } + */ +readonly class StructureNode +{ + /** + * @param list $children + */ + public function __construct( + public string $role, + public int $position, + public ?int $compositionVersionId, + public ?int $compositionId, + public ?int $contentElementVersionId, + public ?int $contentElementId, + public ?string $editorialKindLabel, + public ?string $technicalKindLabel, + public ?string $outputClass, + public string $title, + public array $children = [], + ) {} + + /** + * @return array + */ + public function toArray(): array + { + return [ + 'role' => $this->role, + 'position' => $this->position, + 'composition_version_id' => $this->compositionVersionId, + 'composition_id' => $this->compositionId, + 'content_element_version_id' => $this->contentElementVersionId, + 'content_element_id' => $this->contentElementId, + 'editorial_kind' => $this->editorialKindLabel, + 'technical_kind' => $this->technicalKindLabel, + 'output_class' => $this->outputClass, + 'title' => $this->title, + 'children' => array_map(fn (StructureNode $child): array => $child->toArray(), $this->children), + ]; + } +} \ No newline at end of file diff --git a/database/migrations/2026_11_16_120000_composition_version_display_events.php b/database/migrations/2026_11_16_120000_composition_version_display_events.php new file mode 100644 index 0000000..3ddd030 --- /dev/null +++ b/database/migrations/2026_11_16_120000_composition_version_display_events.php @@ -0,0 +1,28 @@ +id(); + $table->foreignId('composition_version_id')->constrained()->cascadeOnDelete(); + $table->string('event'); + $table->foreignId('actor_id')->nullable()->constrained('users')->nullOnDelete(); + $table->timestamp('occurred_at'); + $table->text('note')->nullable(); + $table->timestamps(); + + $table->index(['composition_version_id', 'occurred_at']); + }); + } + + public function down(): void + { + Schema::dropIfExists('composition_version_display_events'); + } +}; \ No newline at end of file diff --git a/tests/Feature/CompositionStructureAndDisplayTest.php b/tests/Feature/CompositionStructureAndDisplayTest.php new file mode 100644 index 0000000..cdc92f2 --- /dev/null +++ b/tests/Feature/CompositionStructureAndDisplayTest.php @@ -0,0 +1,93 @@ +publishElementVersion($content->createElement('film', 'film', 'Film A'), null, ['file' => 'inhalte/a.mp4']); + $programm = EditorialContentKind::query()->where('slug', 'programm')->firstOrFail(); + $composition = $content->publishComposition( + tap($content->createComposition('Programm A'), function ($item) use ($programm): void { + $item->editorial_content_kind_id = $programm->id; + $item->save(); + }), + [['role' => 'Film', 'target' => $film]], + ); + $compositionModel = $composition->composition; + + $composition->load('composition.editorialContentKind'); + $tree = app(CompositionStructureReader::class)->tree($composition); + + $this->assertSame('Programm', $composition->composition->editorialContentKind?->label); + $this->assertCount(1, $tree); + $this->assertSame('Film A', $tree[0]->title); + $this->assertFalse(app(CompositionDisplayRelease::class)->isReleased($composition)); + } + + public function test_display_release_and_revoke_are_version_bound_and_auditable(): void + { + $content = app(ContentService::class); + $composition = $content->publishComposition( + $content->createComposition('Modul A'), + [['role' => 'Text', 'target' => $content->publishElementVersion($content->createElement('text', 'text', 'Text'), 'Inhalt')]], + ); + $actor = User::factory()->create(); + $releases = app(CompositionDisplayRelease::class); + + $releases->release($composition, $actor, 'Erstfreigabe'); + $this->assertTrue($releases->isReleased($composition)); + + $releases->revoke($composition, $actor, 'Korrektur'); + $this->assertFalse($releases->isReleased($composition)); + + $this->assertCount(2, $composition->fresh()->displayEvents); + $this->assertSame($actor->id, $composition->displayEvents()->orderByDesc('id')->first()->actor_id); + } + + public function test_revoke_without_release_fails(): void + { + $content = app(ContentService::class); + $composition = $content->publishComposition( + $content->createComposition('Leer'), + [['role' => 'Text', 'target' => $content->publishElementVersion($content->createElement('text', 'text', 'Text'), 'Inhalt')]], + ); + + $this->expectException(DomainRuleException::class); + app(CompositionDisplayRelease::class)->revoke($composition, User::factory()->create()); + } + + public function test_highest_released_version_picks_latest_number(): void + { + $content = app(ContentService::class); + $element = $content->createComposition('Prog'); + $v1 = $content->publishComposition($element, [ + ['role' => 'Text', 'target' => $content->publishElementVersion($content->createElement('text', 'text', 'Eins'), '1')], + ]); + $actor = User::factory()->create(); + app(CompositionDisplayRelease::class)->release($v1, $actor); + + $v2 = $content->publishComposition($element, [ + ['role' => 'Text', 'target' => $content->publishElementVersion($content->createElement('text', 'text', 'Zwei'), '2')], + ]); + app(CompositionDisplayRelease::class)->release($v2, $actor); + + $highest = app(CompositionDisplayRelease::class)->highestReleasedVersion($element); + + $this->assertSame(2, $highest?->number); + } +} \ No newline at end of file