Ergänze das U1-Fachmodell für Inhalt, Paket und Angebot.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
2035c46edd
commit
2ded4b7fad
35 files changed
+2115
No files matched your search
@@ -0,0 +1,196 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Catalog;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ContentService
|
||||
{
|
||||
public function __construct(private readonly ContentResolver $resolver) {}
|
||||
|
||||
public function createElement(string $kind, string $outputClass, string $workingTitle): ContentElement
|
||||
{
|
||||
return ContentElement::query()->create([
|
||||
'kind' => $this->required($kind, 'Die Art des Inhaltselements fehlt.'),
|
||||
'output_class' => $this->required($outputClass, 'Die Ausgabeklasse fehlt.'),
|
||||
'working_title' => $this->required($workingTitle, 'Der Arbeitstitel fehlt.'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function publishElementVersion(
|
||||
ContentElement $element,
|
||||
?string $body = null,
|
||||
array $metadata = [],
|
||||
?string $changeNote = null,
|
||||
): ContentElementVersion {
|
||||
if (($body === null || $body === '') && $metadata === []) {
|
||||
throw new DomainRuleException('Eine Inhaltsfassung braucht einen Inhalt.');
|
||||
}
|
||||
|
||||
$number = ((int) $element->versions()->max('number')) + 1;
|
||||
|
||||
return $element->versions()->create([
|
||||
'number' => $number,
|
||||
'status' => ContentVersionStatus::Bereit,
|
||||
'content_hash' => hash('sha256', json_encode([
|
||||
'body' => $body,
|
||||
'metadata' => $metadata,
|
||||
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE)),
|
||||
'body' => $body,
|
||||
'metadata' => $metadata === [] ? null : $metadata,
|
||||
'change_note' => $changeNote,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array{role: string, target: ContentElementVersion|CompositionVersion}> $members
|
||||
*/
|
||||
public function publishComposition(
|
||||
Composition $composition,
|
||||
array $members,
|
||||
?User $confirmedBy = null,
|
||||
?string $note = null,
|
||||
): CompositionVersion {
|
||||
if ($members === []) {
|
||||
throw new DomainRuleException('Eine Komposition braucht mindestens ein Mitglied.');
|
||||
}
|
||||
|
||||
$this->assertAcyclic($composition->id, $members);
|
||||
|
||||
foreach ($members as $member) {
|
||||
$this->assertReadyMember($member['target']);
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($composition, $members, $confirmedBy, $note): CompositionVersion {
|
||||
$number = ((int) $composition->versions()->max('number')) + 1;
|
||||
$version = $composition->versions()->create([
|
||||
'number' => $number,
|
||||
'status' => ContentVersionStatus::Entwurf,
|
||||
'members_hash' => $this->membersHash($members),
|
||||
'resolved_hash' => 'pending',
|
||||
]);
|
||||
|
||||
foreach (array_values($members) as $index => $member) {
|
||||
$target = $member['target'];
|
||||
$version->members()->create([
|
||||
'position' => $index + 1,
|
||||
'role' => $this->required($member['role'], 'Die Rolle eines Mitglieds fehlt.'),
|
||||
'content_element_version_id' => $target instanceof ContentElementVersion ? $target->id : null,
|
||||
'nested_composition_version_id' => $target instanceof CompositionVersion ? $target->id : null,
|
||||
]);
|
||||
}
|
||||
|
||||
$resolvedHash = $this->resolver->resolvedHash($version);
|
||||
$version->attestations()->create([
|
||||
'resolved_hash' => $resolvedHash,
|
||||
'confirmed_by' => $confirmedBy?->id,
|
||||
'confirmed_at' => now(),
|
||||
'note' => $note,
|
||||
]);
|
||||
$version->resolved_hash = $resolvedHash;
|
||||
$version->status = ContentVersionStatus::Bereit;
|
||||
$version->save();
|
||||
|
||||
return $version->load('members');
|
||||
});
|
||||
}
|
||||
|
||||
public function createComposition(string $designation): Composition
|
||||
{
|
||||
return Composition::query()->create([
|
||||
'designation' => $this->required($designation, 'Die Bezeichnung der Komposition fehlt.'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<int>
|
||||
*/
|
||||
public function containingCompositionIds(ContentElementVersion $version): array
|
||||
{
|
||||
$ids = [];
|
||||
|
||||
foreach (CompositionVersion::query()->where('status', ContentVersionStatus::Bereit)->get() as $compositionVersion) {
|
||||
$contained = array_column($this->resolver->expand($compositionVersion), 'version_id');
|
||||
|
||||
if (in_array($version->id, $contained, true)) {
|
||||
$ids[] = $compositionVersion->id;
|
||||
}
|
||||
}
|
||||
|
||||
return $ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array{role: string, target: ContentElementVersion|CompositionVersion}> $members
|
||||
* @param list<int> $stack
|
||||
*/
|
||||
private function assertAcyclic(int $compositionId, array $members, array $stack = []): void
|
||||
{
|
||||
if (in_array($compositionId, $stack, true)) {
|
||||
throw new DomainRuleException('Kompositionen dürfen keinen Zyklus bilden.');
|
||||
}
|
||||
|
||||
$stack[] = $compositionId;
|
||||
|
||||
foreach ($members as $member) {
|
||||
$target = $member['target'];
|
||||
|
||||
if (! $target instanceof CompositionVersion) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$nested = [];
|
||||
|
||||
foreach ($target->members()->orderBy('position')->get() as $row) {
|
||||
$child = $row->contentElementVersion ?? $row->nestedCompositionVersion;
|
||||
|
||||
if ($child === null) {
|
||||
throw new DomainRuleException('Ein Kompositionsmitglied braucht genau ein Ziel.');
|
||||
}
|
||||
|
||||
$nested[] = ['role' => $row->role, 'target' => $child];
|
||||
}
|
||||
|
||||
$this->assertAcyclic($target->composition_id, $nested, $stack);
|
||||
}
|
||||
}
|
||||
|
||||
private function assertReadyMember(ContentElementVersion|CompositionVersion $target): void
|
||||
{
|
||||
if ($target->status !== ContentVersionStatus::Bereit) {
|
||||
throw new DomainRuleException('Eine Komposition darf nur bereitgestellte Fassungen enthalten.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array{role: string, target: ContentElementVersion|CompositionVersion}> $members
|
||||
*/
|
||||
private function membersHash(array $members): string
|
||||
{
|
||||
$list = [];
|
||||
|
||||
foreach (array_values($members) as $index => $member) {
|
||||
$target = $member['target'];
|
||||
$list[] = [
|
||||
'position' => $index + 1,
|
||||
'role' => $member['role'],
|
||||
'element_version_id' => $target instanceof ContentElementVersion ? $target->id : null,
|
||||
'composition_version_id' => $target instanceof CompositionVersion ? $target->id : null,
|
||||
];
|
||||
}
|
||||
|
||||
return hash('sha256', json_encode($list, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
|
||||
private function required(string $value, string $message): string
|
||||
{
|
||||
$value = trim($value);
|
||||
|
||||
if ($value === '') {
|
||||
throw new DomainRuleException($message);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user