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 block(ContentElementVersion|CompositionVersion $version, string $reason): void { if (trim($reason) === '') { throw new DomainRuleException('Eine Sperre braucht eine Begründung.'); } if ($version->status !== ContentVersionStatus::Bereit) { throw new DomainRuleException('Nur eine bereitgestellte Fassung kann gesperrt werden.'); } $version->status = ContentVersionStatus::Gesperrt; if ($version instanceof ContentElementVersion) { $version->block_reason = trim($reason); } $version->save(); } public function describe(ContentElement $element, string $workingTitle, ?string $description): void { $element->working_title = $this->required($workingTitle, 'Der Arbeitstitel fehlt.'); $description = trim((string) $description); $element->description = $description === '' ? null : $description; $element->save(); } 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; $incoming = $metadata['incoming'] ?? null; $version = $element->versions()->create([ 'number' => $number, 'status' => is_string($incoming) && $incoming !== '' ? ContentVersionStatus::Entwurf : 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, ]); if (is_string($incoming) && $incoming !== '') { StoreContentFile::dispatch($version->id, $incoming); $version->refresh(); } return $version; } /** * @param list $members */ public function publishComposition( Composition $composition, array $members, ?User $confirmedBy = null, ?string $note = null, array $didactic = [], ): 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, $didactic): 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', 'learning_goal' => $this->optionalText($didactic['learning_goal'] ?? null), 'duration_minutes' => isset($didactic['duration_minutes']) ? (int) $didactic['duration_minutes'] : null, 'material_note' => $this->optionalText($didactic['material_note'] ?? null), ]); 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.'), 'placement_note' => $this->optionalText($member['note'] ?? null), '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, CompositionKind $kind = CompositionKind::Uebung): Composition { return Composition::query()->create([ 'designation' => $this->required($designation, 'Die Bezeichnung fehlt.'), 'kind' => $kind, ]); } /** * @return list */ 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 $members * @param list $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 $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'], 'note' => $this->optionalText($member['note'] ?? null), '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 optionalText(mixed $value): ?string { if (! is_string($value)) { return null; } $value = trim($value); return $value === '' ? null : $value; } private function required(string $value, string $message): string { $value = trim($value); if ($value === '') { throw new DomainRuleException($message); } return $value; } }