Files
mareike/app/Domains/Admin/Actions/UpdateDocumentTemplate/UpdateDocumentTemplateAction.php

74 lines
2.5 KiB
PHP

<?php
namespace App\Domains\Admin\Actions\UpdateDocumentTemplate;
use App\Models\DocumentTemplate;
/**
* Speichert die Blockinhalte einer Dokumentvorlage.
*
* Es werden nur bereits angelegte, als editierbar markierte Blöcke geschrieben. Neue Blöcke entstehen
* nicht über das Formular -- welche Blöcke eine Dokumentart hat, gibt die Vorlage vor (das Layout
* verweist mit `{block:...}` auf sie).
*/
class UpdateDocumentTemplateAction
{
public function __construct(private readonly UpdateDocumentTemplateRequest $request)
{
}
public function execute(): UpdateDocumentTemplateResponse
{
$response = new UpdateDocumentTemplateResponse();
$existing = DocumentTemplate::forType($this->request->documentType);
// Erst prüfen, dann schreiben -- sonst bliebe bei einem Fehler ein halb gespeicherter Stand übrig.
$rejection = $this->rejectBareAssetInStyle();
if ($rejection !== null) {
$response->message = $rejection;
return $response;
}
foreach ($this->request->blocks as $block => $content) {
$template = $existing->get($block);
if ($template === null || !$template->editable) {
$response->skipped[] = $block;
continue;
}
$template->update(['content' => (string) $content]);
}
$response->success = true;
$response->message = $response->skipped === []
? 'Die Vorlage wurde gespeichert.'
: 'Die Vorlage wurde gespeichert; nicht editierbare Blöcke blieben unverändert.';
return $response;
}
/**
* Ein Bild-Platzhalter, der im CSS nicht in `url(...)` steht, expandiert zu einem Data-URI, das
* dompdf nicht auslagern kann -- der Parser läuft dann minutenlang und der FPM-Worker stirbt am
* Zeitlimit (502). Solches CSS wird gar nicht erst gespeichert.
*
* @return string|null Fehlermeldung, oder null wenn nichts zu beanstanden ist.
*/
private function rejectBareAssetInStyle(): ?string
{
$style = $this->request->blocks[DocumentTemplate::BLOCK_STYLE] ?? null;
if ($style === null || preg_match('/(?<![("\'])\{asset:([a-z0-9_-]+)}/i', (string) $style, $match) !== 1) {
return null;
}
return sprintf(
'Im CSS-Block steht der Bild-Platzhalter {asset:%1$s} direkt im Text. Dort gehört er in url("{asset:%1$s}").',
$match[1]
);
}
}