Antwortart (Auskunft/Angebot) und Reservierung sind entkoppelt (neue Einstellung inquiry_answer; pre_reservation bleibt die Reservierungsstrategie). Anlass-bezogene Absender für Anreise-Information und Bewertungsbitte; Schablonen liefern Standardwerte unterhalb aller einstellbaren Ebenen. A1/A2 bleiben Voreinstellungen und Altfassungen. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
198 lines
7.8 KiB
PHP
198 lines
7.8 KiB
PHP
<?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 && isset($model::templateRestrictions()[$class::key()])) {
|
||
$problems[] = '„'.$class::label().'“ passt nicht zu „'.$model::label().'“: '.$model::templateRestrictions()[$class::key()];
|
||
|
||
continue;
|
||
}
|
||
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;
|
||
}
|
||
}
|