Files
fewo-stommer/app/Process/ConfigurationImpact.php
T
LarsandClaude Sonnet 5.5 221ce8ca81 Anfrage-Basisprozess inquiry.request mit Schablonen für Antwort und Gastkommunikation
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>
2026-11-05 12:40:22 +01:00

172 lines
9.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Process;
use App\Enums\Execution;
use App\Enums\IntakeKind;
use App\Enums\ProcessSetting;
use App\Models\Channel;
use App\Process\Modules\ModuleDefinition;
use App\Process\Templates\TemplateComposer;
/**
* Rule-based comparison of a configuration draft (docs/Prozessarchitektur/MODUL_KONFIGURATIONSMATRIX.md §5): for every
* step the landlord switched off it names the steps that depend on it, what stays required, the admissible ways to
* achieve the result otherwise and what the configurator suggests. Derived from the module definitions the runner uses –
* no second set of rules. Pure: it reads and explains, it never changes a setting.
*/
final class ConfigurationImpact
{
public function __construct(private ProcessCatalogue $catalogue, private ChannelProcessConfiguration $configuration, private TemplateComposer $composer) {}
/**
* @param array<string, mixed> $draft
* @return list<array{module: string, label: string, valid: bool, affected: list<string>, relaxed: list<string>, remains: list<string>, alternatives: list<string>, suggestion: string}>
*/
public function of(Channel $channel, IntakeKind $intake, array $draft): array
{
$rows = collect($this->configuration->preview($channel, $intake, $draft));
$active = $rows->where('on', true)->pluck('module')->all();
$labels = $rows->mapWithKeys(fn (array $row): array => [$row['module'] => $row['label']])->all();
$impacts = [];
foreach ($rows as $row) {
$definition = $this->catalogue->modules()[$row['module']]::definition();
if (($draft['steps'][$row['module']]['on'] ?? true) !== false) {
continue;
}
$affected = $relaxed = [];
foreach ($active as $other) {
foreach ($this->catalogue->modules()[$other]::definition()->requires as $dependency) {
if ($dependency->module !== $row['module']) {
continue;
}
if ($dependency->hard) {
$affected[] = $labels[$other];
} else {
$relaxed[] = $labels[$other];
}
}
}
$alternatives = array_values(array_map(fn (Execution $execution): string => $execution->getLabel(), array_filter($definition->executions, fn (Execution $execution): bool => $execution !== Execution::NotRequired)));
$valid = $definition->isOptional();
$impacts[] = [
'module' => $row['module'], 'label' => $row['label'], 'valid' => $valid,
'affected' => array_values(array_unique($affected)), 'relaxed' => array_values(array_unique($relaxed)),
'remains' => $this->remains($definition, $affected, $labels),
'alternatives' => $alternatives,
'suggestion' => $this->suggestion($row['label'], $definition, $valid, $alternatives),
];
}
return $impacts;
}
/**
* What a change of function templates means (docs/Prozessarchitektur/SCHABLONEN.md): per changed area the modules that
* drop out, join or stay, the responsibility that changes, the stored switches and settings that no longer apply and
* what the new template still needs or does not build yet. Compared with the templates in force for new cases. Pure:
* nothing is changed, nothing is dropped silently – the landlord sees every consequence before saving.
*
* @param array<string, mixed> $draft
* @return list<array{area: string, from: ?string, to: string, removed: list<string>, added: list<string>, kept: list<string>, responsibility: ?array{from: string, to: string}, switches_lost: list<string>, settings_replaced: list<string>, needs: list<string>, extensions: list<string>}>
*/
public function ofTemplates(Channel $channel, IntakeKind $intake, array $draft): array
{
$model = $this->catalogue->model((string) ($draft['model'] ?? ''));
if ($model->slots() === []) {
return [];
}
$current = $this->configuration->currentTemplates($channel, $intake);
$target = $this->composer->resolve($model, (array) ($draft['templates'] ?? []));
$stored = (array) (($channel->process_overrides ?? [])[$intake->value] ?? []);
$labelOf = fn (string $module): string => ($this->catalogue->hasModule($module) ? $this->catalogue->modules()[$module]::definition()->label : $module);
$impacts = [];
foreach ($model->slots() as $area) {
$from = $current[$area->value] ?? null;
$to = $target[$area->value] ?? null;
$toClass = $to === null ? null : $this->catalogue->templateClass($to);
if ($toClass === null || $from === $to) {
continue;
}
$fromClass = $from === null ? null : $this->catalogue->templateClass($from);
$before = $fromClass === null ? [] : $fromClass::modules();
$after = $toClass::modules();
$removed = array_values(array_diff($before, $after));
$setting = $area->setting();
$changedFixes = [];
$replaced = [];
foreach ([...array_keys($toClass::fixes()), ...array_keys($toClass::defaults())] as $key) {
$changed = ProcessSetting::tryFrom($key);
$wanted = $toClass::fixes()[$key] ?? $toClass::defaults()[$key];
$previous = $fromClass === null ? null : ($fromClass::fixes()[$key] ?? $fromClass::defaults()[$key] ?? null);
if ($changed === null) {
continue;
}
if ($previous !== null && (string) $changed->normalize($previous) !== (string) $changed->normalize($wanted)) {
$changedFixes[] = $changed->question().' '.$changed->describe($changed->normalize($previous)).' → '.$changed->describe($changed->normalize($wanted)).(array_key_exists($key, $toClass::fixes()) ? '' : ' (nur ein Vorschlag – ausdrücklich gesetzte Werte bleiben)');
}
$storedValue = $changed->normalize($stored[$key] ?? null);
if (array_key_exists($key, $toClass::fixes()) && $storedValue !== null && (string) $storedValue !== (string) $changed->normalize($wanted)) {
$replaced[] = $changed->question().' Bisher gespeichert: „'.$changed->describe($storedValue).'“, künftig durch die Schablone: „'.$changed->describe($changed->normalize($wanted)).'“. Der gespeicherte Wert muss beim Speichern ausdrücklich entfernt werden.';
}
}
$impacts[] = [
'area' => $area->label(),
'from' => $fromClass === null ? null : $fromClass::label(),
'to' => $toClass::label(),
'removed' => array_map($labelOf, $removed),
'added' => array_map($labelOf, array_values(array_diff($after, $before))),
'kept' => array_map($labelOf, array_values(array_intersect($after, $before))),
'responsibility' => $fromClass !== null && $fromClass::responsibility() !== $toClass::responsibility()
? ['from' => $setting->describe($fromClass::responsibility()->value), 'to' => $setting->describe($toClass::responsibility()->value)]
: null,
'changed_settings' => $changedFixes,
'switches_lost' => array_map($labelOf, array_values(array_filter($removed, fn (string $module): bool => ($stored['steps'][$module]['on'] ?? true) === false))),
'settings_replaced' => $replaced,
'needs' => array_map(fn (string $result): string => 'Voraussetzung: '.$result, $toClass::requires()),
'extensions' => $toClass::extensions(),
];
}
return $impacts;
}
/**
* The results that stay necessary although the step is off: what the steps depending on it still need.
*
* @param list<string> $affected
* @param array<string, string> $labels
* @return list<string>
*/
private function remains(ModuleDefinition $definition, array $affected, array $labels): array
{
$remains = [];
if ($affected !== []) {
$remains[] = 'Weiterhin nötig, damit „'.implode('“, „', array_unique($affected)).'“ laufen kann: '.lcfirst($definition->purpose);
}
if ($definition->why !== null) {
$remains[] = $definition->why;
}
return $remains;
}
/**
* @param list<string> $alternatives
*/
private function suggestion(string $label, ModuleDefinition $definition, bool $valid, array $alternatives): string
{
if (! $valid) {
return '„'.$label.'“ ist Pflicht und kann nicht abgeschaltet werden.'.($alternatives !== [] ? ' Zulässig sind stattdessen: '.implode('; ', $alternatives).'.' : '');
}
return $alternatives === []
? '„'.$label.'“ entfällt ersatzlos; es ist nichts weiter einzustellen.'
: 'Statt abzuschalten, können Sie „'.$label.'“ auch so erledigen lassen: '.implode('; ', $alternatives).'. Es wird nichts anderes automatisch geändert.';
}
}