Schablonenkomposition: Basisprozess external.booking mit Platz für Finanz- und Dokumentenschablonen

Vier Schablonen (Eigenes/Portal-Inkasso, Eigene/Externe Vertragsdokumentation) in
app/Process/Templates, TemplateComposer prüft Ergebnisse, Verantwortlichkeiten, Ausschlüsse
und harte Modulabhängigkeiten. Der Plan friert die Schablonenversionen ein. A3–A5 bleiben
als Voreinstellungen; die vierte Kombination (eigenes Inkasso + externe Unterlagen) ist
darstellbar. Konfigurator-Logik und ConfigureChannelProcess kennen die Schablonenwahl.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-05 10:48:49 +01:00
1 parent 381720763c
commit e95ba4f986
24 files changed
+1666 -37

No files matched your search

+192
View File
@@ -0,0 +1,192 @@
<?php
namespace App\Process\Templates;
use App\Process\Models\ProcessModel;
use App\Process\ProcessCatalogue;
/**
* Checks and resolves a combination of function templates for the slots of a model. Pure: it reads the registered
* definitions and returns what is wrong in plain words (empty = the combination yields a valid plan). It is no second
* engine – the plan it leads to is run by the ProcessRunner with the dependencies the modules declare.
*/
final class TemplateComposer
{
public function __construct(private ProcessCatalogue $catalogue) {}
/**
* The template reference (key@version) every slot of the model gets: the chosen one, otherwise the model's default.
* References that are not registered are kept as given so that problems() can name them.
*
* @param array<string, string> $chosen area value => template key or key@version
* @return array<string, string> area value => key@version
*/
public function resolve(ProcessModel $model, array $chosen): array
{
$resolved = [];
foreach ($model->slots() as $area) {
$reference = (string) ($chosen[$area->value] ?? $model::defaultTemplates()[$area->value] ?? '');
$class = $reference === '' ? null : $this->catalogue->templateClass($reference);
$resolved[$area->value] = $class === null ? $reference : $class::reference();
}
return $resolved;
}
/**
* The template classes behind a resolved combination (unknown references are left out).
*
* @param array<string, string> $resolved
* @return array<string, class-string<FunctionTemplate>>
*/
public function classes(array $resolved): array
{
$classes = [];
foreach ($resolved as $area => $reference) {
if (($class = $this->catalogue->templateClass($reference)) !== null) {
$classes[$area] = $class;
}
}
return $classes;
}
/**
* @param array<string, string> $chosen area value => template key or key@version
* @return list<string>
*/
public function problems(ProcessModel $model, array $chosen, string $variant = 'standard'): array
{
$problems = [];
$slots = array_map(fn (TemplateArea $area): string => $area->value, $model->slots());
foreach (array_keys($chosen) as $area) {
if (! in_array($area, $slots, true)) {
$problems[] = 'Das Modell „'.$model::label().'“ hat keinen Platz für „'.$area.'“.';
}
}
if ($slots === []) {
return $problems;
}
$resolved = $this->resolve($model, array_intersect_key($chosen, array_flip($slots)));
$classes = [];
foreach ($resolved as $area => $reference) {
$class = $this->catalogue->templateClass($reference);
if ($class === null) {
$problems[] = 'Die Schablone „'.$reference.'“ gibt es nicht.';
continue;
}
if ($class::area()->value !== $area) {
$problems[] = 'Die Schablone „'.$class::label().'“ gehört zu „'.$class::area()->label().'“, nicht zu „'.TemplateArea::from($area)->label().'“.';
continue;
}
$classes[$area] = $class;
}
$problems = [...$problems, ...$this->resultProblems($model, $classes), ...$this->pairProblems($classes), ...$this->planProblems($model, $classes, $variant)];
return array_values(array_unique($problems));
}
/**
* Prerequisites and double effects at the level of business results.
*
* @param array<string, class-string<FunctionTemplate>> $classes
* @return list<string>
*/
private function resultProblems(ProcessModel $model, array $classes): array
{
$problems = [];
$providers = [];
foreach ($model::providedResults() as $result) {
$providers[$result][] = 'der Basisprozess';
}
foreach ($classes as $class) {
foreach ($class::provides() as $result) {
$providers[$result][] = '„'.$class::label().'“';
}
}
foreach ($providers as $result => $by) {
if (count($by) > 1) {
$problems[] = 'Das Ergebnis „'.$result.'“ würde doppelt erzeugt: '.implode(' und ', $by).'.';
}
}
foreach ($classes as $class) {
foreach ($class::requires() as $needed) {
if (! isset($providers[$needed])) {
$problems[] = '„'.$class::label().'“ setzt das Ergebnis „'.$needed.'“ voraus, das in dieser Kombination niemand liefert.';
}
}
}
return $problems;
}
/**
* Mutually exclusive templates and contradictory responsibilities.
*
* @param array<string, class-string<FunctionTemplate>> $classes
* @return list<string>
*/
private function pairProblems(array $classes): array
{
$problems = [];
$fixed = [];
foreach ($classes as $class) {
foreach ($class::incompatibleWith() as $other => $reason) {
if (collect($classes)->contains(fn (string $chosen): bool => $chosen::key() === $other)) {
$problems[] = '„'.$class::label().'“ und „'.$this->catalogue->templateClass($other)::label().'“ lassen sich nicht kombinieren: '.$reason;
}
}
foreach ($class::fixes() as $setting => $value) {
if (isset($fixed[$setting]) && $fixed[$setting]['value'] !== $value) {
$problems[] = 'Widersprüchliche Verantwortlichkeit ('.$setting.'): „'.$fixed[$setting]['by'].'“ und „'.$class::label().'“ legen Unterschiedliches fest.';
}
$fixed[$setting] = ['value' => $value, 'by' => $class::label()];
}
}
return $problems;
}
/**
* What the composed course needs from the modules: no module twice, and a hard dependency on a module that one of the
* templates of this model's slots would have supplied must be in the plan.
*
* @param array<string, class-string<FunctionTemplate>> $classes
* @return list<string>
*/
private function planProblems(ProcessModel $model, array $classes, string $variant): array
{
$problems = [];
$modules = $model->modules($variant, array_map(fn (string $class): string => $class::reference(), $classes));
foreach (array_count_values($modules) as $module => $count) {
if ($count > 1) {
$problems[] = 'Das Modul „'.$module.'“ wäre mehrfach im Ablauf.';
}
}
$templateOwned = [];
foreach ($model->slots() as $area) {
foreach ($this->catalogue->templates($area) as $class) {
array_push($templateOwned, ...$class::modules());
}
}
foreach (array_unique($modules) as $key) {
if (! $this->catalogue->hasModule($key)) {
$problems[] = 'Das Modul „'.$key.'“ ist nicht registriert.';
continue;
}
foreach ($this->catalogue->modules()[$key]::definition()->requires as $dependency) {
if ($dependency->hard && ! in_array($dependency->module, $modules, true) && in_array($dependency->module, $templateOwned, true)) {
$problems[] = 'Das Modul „'.$key.'“ braucht zwingend „'.$dependency->module.'“, das in dieser Kombination fehlt.';
}
}
}
return $problems;
}
}