Files
fewo-stommer/app/Process/ProcessCatalogue.php
T
LarsandClaude Opus 5.5 7e22e12cac U-A/U-B: Prozesskern mit Fachmodulen, versionierten Modellen, eingefrorenem Prozessplan, ProcessRunner (harte/weiche Abhängigkeiten), CasePricing, Loyalty nur im Tester
CaseActivities/CaseAutomation sind Fassaden über ProcessRunner. Verhaltensvergleich v0.11:
11 Abläufe und 198 Konstellationen gleich, beabsichtigte Abweichungen I-004/I-005 (F-001/F-002).
Volle Suite vor U-B: 785/785.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 14:49:44 +01:00

215 lines
8.0 KiB
PHP

<?php
namespace App\Process;
use App\Enums\CaseType;
use App\Enums\IntakeKind;
use App\Process\Models\ProcessModel;
use App\Process\Modules\ProcessModule;
use App\Process\Modules\RunsAutomatically;
use InvalidArgumentException;
/**
* The registered modules and models (config/process.php) and the checks that keep the catalogue consistent
* (`php artisan process:check`, ProcessCatalogueTest).
*/
final class ProcessCatalogue
{
/** @var array<string, class-string<ProcessModule>>|null */
private ?array $modules = null;
/**
* @return array<string, class-string<ProcessModule>> key => class
*/
public function modules(): array
{
if ($this->modules === null) {
$this->modules = [];
foreach ((array) config('process.modules', []) as $class) {
$this->modules[$class::key()] = $class;
}
}
return $this->modules;
}
public function hasModule(string $key): bool
{
return array_key_exists($key, $this->modules());
}
public function module(string $key): ProcessModule
{
$class = $this->modules()[$key] ?? throw new InvalidArgumentException("Unknown process module [{$key}].");
return app($class);
}
/**
* Whether the registered class can serve a plan that froze the module in the given version.
*/
public function serves(string $key, int $version): bool
{
$class = $this->modules()[$key] ?? null;
return $class !== null && ($class::version() === $version || in_array($version, (array) config("process.compatible.{$key}", []), true));
}
/**
* @return array<string, array<int, class-string<ProcessModel>>>
*/
public function modelVersions(): array
{
return (array) config('process.models', []);
}
public function hasModel(string $key, ?int $version = null): bool
{
$versions = $this->modelVersions()[$key] ?? [];
return $version === null ? $versions !== [] : array_key_exists($version, $versions);
}
/**
* A model in a version (null: the latest).
*/
public function model(string $key, ?int $version = null): ProcessModel
{
$versions = $this->modelVersions()[$key] ?? throw new InvalidArgumentException("Unknown process model [{$key}].");
$version ??= max(array_keys($versions));
$class = $versions[$version] ?? throw new InvalidArgumentException("Process model [{$key}] has no version {$version}.");
return app($class);
}
/**
* The latest version of every model.
*
* @return array<string, ProcessModel>
*/
public function models(): array
{
$models = [];
foreach (array_keys($this->modelVersions()) as $key) {
$models[$key] = $this->model($key);
}
return $models;
}
/**
* The model a case follows when nobody chose one: derived from its frozen profile exactly as the v0.11 course
* branched (legacy mapping, docs/Prozessarchitektur/Architektur/06 §4).
*/
public function derivedModel(ProcessProfile $profile): string
{
return match ($profile->intake) {
IntakeKind::Internal => 'a6.internal',
IntakeKind::Inquiry => ($profile->caseType === CaseType::A2 ? 'a2' : 'a1').($profile->preReservation() ? '.offer' : '.information'),
IntakeKind::Booking => match ($profile->caseType) {
CaseType::A5 => 'a5.portal_booking',
CaseType::A4 => 'a4.portal_booking',
default => 'a3.portal_booking',
},
};
}
/**
* The models admissible for a case type and intake kind.
*
* @return array<string, ProcessModel>
*/
public function admissible(IntakeKind $intake, ?CaseType $caseType = null): array
{
return array_filter($this->models(), fn (ProcessModel $model): bool => in_array($intake, $model::intakes(), true)
&& ($caseType === null || in_array($caseType, $model::caseTypes(), true)));
}
/**
* Everything that is inconsistent in the catalogue (empty = consistent).
*
* @return list<string>
*/
public function problems(): array
{
$problems = [];
foreach ($this->modules() as $key => $class) {
$definition = $class::definition();
foreach ($definition->requires as $dependency) {
if (! $this->hasModule($dependency->module)) {
$problems[] = "Modul {$key}: Abhängigkeit {$dependency->module} ist nicht registriert.";
}
}
if (! in_array($definition->phase, ['P01', 'P02', 'P03', 'P04', 'P05'], true)) {
$problems[] = "Modul {$key}: unbekannte Phase {$definition->phase}.";
}
if (is_subclass_of($class, RunsAutomatically::class) && $definition->modes === []) {
$problems[] = "Modul {$key}: läuft automatisch, nennt aber keine Ausführungsart.";
}
}
foreach ($this->cycles() as $cycle) {
$problems[] = 'Zyklische harte Abhängigkeit: '.implode(' → ', $cycle);
}
foreach ($this->modelVersions() as $key => $versions) {
foreach ($versions as $version => $class) {
if ($class::key() !== $key || $class::version() !== $version) {
$problems[] = "Modell {$key}@{$version}: Klasse {$class} meldet ".$class::key().'@'.$class::version().'.';
}
$model = app($class);
foreach ($model->variants() as $variant) {
foreach ($model->modules($variant->key) as $module) {
if (! $this->hasModule($module)) {
$problems[] = "Modell {$key}@{$version}, Variante {$variant->key}: Modul {$module} ist nicht registriert.";
}
}
foreach ($variant->inserts() as $point => $modules) {
if (! in_array($point, array_map(fn ($p) => $p->value, $model->points()), true)) {
$problems[] = "Modell {$key}@{$version}, Variante {$variant->key}: Erweiterungspunkt {$point} gibt es im Modell nicht.";
}
foreach ($modules as $module) {
$points = $this->hasModule($module) ? $this->modules()[$module]::definition()->points : [];
if ($points !== [] && ! in_array($point, $points, true)) {
$problems[] = "Modell {$key}@{$version}: Modul {$module} darf nicht an {$point} stehen.";
}
if ($this->hasModule($module) && $this->modules()[$module]::definition()->isTesterOnly() && ! $variant->isTesterOnly()) {
$problems[] = "Modell {$key}@{$version}: Modul {$module} ist nur für den Prozess-Tester, Variante {$variant->key} aber nicht.";
}
}
}
}
}
}
return $problems;
}
/**
* @return list<list<string>>
*/
private function cycles(): array
{
$edges = [];
foreach ($this->modules() as $key => $class) {
$edges[$key] = array_values(array_map(fn ($dependency): string => $dependency->module, array_filter($class::definition()->requires, fn ($dependency): bool => $dependency->hard)));
}
$cycles = [];
$visit = function (string $node, array $path) use (&$visit, &$cycles, $edges): void {
if (in_array($node, $path, true)) {
$cycles[] = [...array_slice($path, (int) array_search($node, $path, true)), $node];
return;
}
foreach ($edges[$node] ?? [] as $next) {
$visit($next, [...$path, $node]);
}
};
foreach (array_keys($edges) as $node) {
$visit($node, []);
}
return array_values(array_unique($cycles, SORT_REGULAR));
}
}