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),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user