Paket 3: Strukturleser und Anzeigefreigabe (G1, G2)
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
1a2b1298b5
commit
5ed01b9d04
7 files changed
+372
No files matched your search
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Catalog;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CompositionDisplayRelease
|
||||
{
|
||||
public function isReleased(CompositionVersion $version): bool
|
||||
{
|
||||
return $this->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.');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Catalog;
|
||||
|
||||
class CompositionStructureReader
|
||||
{
|
||||
/**
|
||||
* @return list<StructureNode>
|
||||
*/
|
||||
public function tree(CompositionVersion $version): array
|
||||
{
|
||||
$version->loadMissing([
|
||||
'members.contentElementVersion.element.editorialContentKind',
|
||||
'members.nestedCompositionVersion.composition.editorialContentKind',
|
||||
]);
|
||||
|
||||
return $this->members($version);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<StructureNode>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Catalog;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class CompositionVersionDisplayEvent extends Model
|
||||
{
|
||||
public const RELEASED = 'freigegeben';
|
||||
|
||||
public const REVOKED = 'zurueckgenommen';
|
||||
|
||||
protected $fillable = [
|
||||
'composition_version_id',
|
||||
'event',
|
||||
'actor_id',
|
||||
'occurred_at',
|
||||
'note',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'occurred_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
public function version(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(CompositionVersion::class, 'composition_version_id');
|
||||
}
|
||||
|
||||
public function actor(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'actor_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Catalog;
|
||||
|
||||
/**
|
||||
* @phpstan-type StructureNodeData array{
|
||||
* role: string,
|
||||
* position: int,
|
||||
* composition_version_id: int|null,
|
||||
* composition_id: int|null,
|
||||
* content_element_version_id: int|null,
|
||||
* content_element_id: int|null,
|
||||
* editorial_kind: string|null,
|
||||
* technical_kind: string|null,
|
||||
* title: string,
|
||||
* output_class: string|null,
|
||||
* children: list<StructureNodeData>
|
||||
* }
|
||||
*/
|
||||
readonly class StructureNode
|
||||
{
|
||||
/**
|
||||
* @param list<StructureNode> $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<string, mixed>
|
||||
*/
|
||||
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),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('composition_version_display_events', function (Blueprint $table) {
|
||||
$table->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');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Domain\Catalog\CompositionDisplayRelease;
|
||||
use App\Domain\Catalog\CompositionStructureReader;
|
||||
use App\Domain\Catalog\ContentService;
|
||||
use App\Domain\Catalog\ContentVersionStatus;
|
||||
use App\Domain\Catalog\DomainRuleException;
|
||||
use App\Domain\Catalog\EditorialContentKind;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CompositionStructureAndDisplayTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_structure_reader_resolves_explicit_version_independent_of_display_release(): void
|
||||
{
|
||||
$content = app(ContentService::class);
|
||||
$film = $content->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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user