Files
laraundfelix/app/Domain/Communication/Publisher.php
T
LarsandClaude Sonnet 5.5 2ee1fc9866 Lege das Datenmodell und den Anfrageservice der Kommunikationsverwaltung an
Anliegen, Feldkatalog, Feldgruppen, Anfragevorlagen mit unveränderlichen Fassungen,
Anfrage-Einstiege und eingegangene Anfragen. Eine gemeinsame Regelauswertung bedient
Besucher, Vorschau und Anfrageservice; Werte nicht mehr relevanter Felder werden verworfen,
Anliegen und Auswahlwerte serverseitig geprüft, doppeltes Absenden bleibt eine Anfrage.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-27 17:26:42 +01:00

145 lines
5.6 KiB
PHP

<?php
namespace App\Domain\Communication;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
/**
* Veröffentlichung von Anfragevorlagen und Anfrage-Einstiegen (ADR-60).
*
* Eine Vorlage wird als neue, unveränderliche Fassung veröffentlicht; bis dahin gilt die bisherige Fassung. Ein Einstieg
* veröffentlicht seine Konfiguration gegen die aktuell gültige Fassung seiner Vorlage. Wirksam ist immer nur ein
* bewusst veröffentlichter Stand, nie ein Entwurf.
*/
final class Publisher
{
public function __construct(private readonly TemplateCompiler $compiler, private readonly FormFactory $factory) {}
/**
* Einstiege, die von einer neuen Fassung betroffen sind, samt Befund gegen die neue Fassung.
*
* @param array<string, mixed> $definition
* @return list<array{entry: InquiryEntry, problems: list<string>}>
*/
public function affected(InquiryTemplate $template, ?array $definition = null): array
{
$result = [];
foreach ($template->entries()->whereNull('archived_at')->orderBy('name')->get() as $entry) {
$problems = $definition !== null && $entry->published_config !== null
? $this->factory->problems($definition, (array) $entry->published_config)
: [];
$result[] = ['entry' => $entry, 'problems' => $problems];
}
return $result;
}
/**
* Veröffentlicht den Entwurf der Vorlage als neue Fassung.
*
* @throws ValidationException
*/
public function publishTemplate(InquiryTemplate $template, ?User $by = null, ?string $note = null): InquiryTemplateVersion
{
$compiled = $this->compiler->compile((array) $template->draft);
if ($compiled['definition'] === null) {
throw ValidationException::withMessages(['draft' => $compiled['errors']]);
}
$blocking = [];
foreach ($this->affected($template, $compiled['definition']) as $row) {
if ($row['entry']->published_config !== null && $row['problems'] !== []) {
$blocking[] = 'Der veröffentlichte Einstieg „'.$row['entry']->name.'“ passt nicht zur neuen Fassung: '.implode(' ', $row['problems']);
}
}
if ($blocking !== []) {
throw ValidationException::withMessages(['draft' => $blocking]);
}
return DB::transaction(function () use ($template, $compiled, $by, $note): InquiryTemplateVersion {
$template = InquiryTemplate::query()->lockForUpdate()->findOrFail($template->id);
$next = (int) InquiryTemplateVersion::query()->where('template_id', $template->id)->max('version') + 1;
$version = InquiryTemplateVersion::query()->create([
'template_id' => $template->id,
'version' => $next,
'definition' => $compiled['definition'],
'source' => (array) $template->draft,
'note' => $note !== null && trim($note) !== '' ? mb_substr(trim($note), 0, 300) : null,
'published_by' => $by?->id,
'published_at' => now(),
]);
$template->forceFill(['published_version_id' => $version->id])->save();
return $version;
});
}
/**
* Setzt eine frühere Fassung wieder in Kraft (Rückkehr zu einer gültigen Konfiguration). Betroffene Einstiege
* müssen zur Fassung passen, sonst wird abgelehnt.
*
* @throws ValidationException
*/
public function restoreVersion(InquiryTemplate $template, InquiryTemplateVersion $version): void
{
abort_unless($version->template_id === $template->id, 404);
$blocking = [];
foreach ($this->affected($template, (array) $version->definition) as $row) {
if ($row['entry']->published_config !== null && $row['problems'] !== []) {
$blocking[] = 'Der veröffentlichte Einstieg „'.$row['entry']->name.'“ passt nicht zu dieser Fassung: '.implode(' ', $row['problems']);
}
}
if ($blocking !== []) {
throw ValidationException::withMessages(['draft' => $blocking]);
}
$template->forceFill(['published_version_id' => $version->id])->save();
}
/**
* Veröffentlicht die Konfiguration eines Einstiegs.
*
* @throws ValidationException
*/
public function publishEntry(InquiryEntry $entry): void
{
$template = $entry->template()->with('publishedVersion')->firstOrFail();
if ($template->publishedVersion === null) {
throw ValidationException::withMessages(['config' => ['Die Anfragevorlage „'.$template->name.'“ ist noch nicht veröffentlicht. Ein Einstieg kann nur eine veröffentlichte Vorlage verwenden.']]);
}
if ($template->archived_at !== null) {
throw ValidationException::withMessages(['config' => ['Die Anfragevorlage „'.$template->name.'“ ist archiviert.']]);
}
$problems = $this->factory->problems((array) $template->publishedVersion->definition, (array) $entry->config);
if ($problems !== []) {
throw ValidationException::withMessages(['config' => $problems]);
}
$entry->forceFill([
'published_config' => $entry->config,
'published_at' => $entry->published_at ?? now(),
'archived_at' => null,
])->save();
}
/** Nimmt einen Einstieg aus dem öffentlichen Angebot; die Konfiguration bleibt erhalten. */
public function withdrawEntry(InquiryEntry $entry): void
{
$entry->forceFill(['published_config' => null, 'published_at' => null])->save();
}
}