Files
fewo-stommer/app/Process/ProcessPlan.php
T

227 lines
8.7 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\IntakeKind;
use App\Models\Booking;
use App\Models\Channel;
use App\Process\Models\ProcessModel;
use DomainException;
/**
* The frozen process plan of a case (docs/Prozessarchitektur/Architektur/02 §1, §5.3): model and version, variant,
* the versions of its modules and the step settings of the channel (on/off, mode, performer, parameters) as they were
* when the case came in. Stored in `bookings.profile.plan`; cases without one (all before V2) get the derived plan of
* their profile on reading, which reproduces the v0.11 course (legacy mapping). The plan also keeps the labels of its
* steps, so a finished case stays readable even if a model definition is ever retired (requirement T3).
*/
final class ProcessPlan
{
public const DERIVED = 'derived';
public const CONFIGURED = 'configured';
public const TESTER = 'tester';
/**
* @param array<string, int> $modules module key => frozen version
* @param array<string, array{on?: bool, mode?: ?string, performer?: ?string, params?: array<string, mixed>}> $steps
* @param array<string, string> $labels module key => label at the time of freezing
*/
public function __construct(
public readonly string $model,
public readonly int $modelVersion,
public readonly string $variant,
public readonly array $modules,
public readonly array $steps = [],
public readonly string $source = self::DERIVED,
public readonly array $labels = [],
public readonly ?string $notice = null,
) {}
/**
* The plan of a case: frozen with it, or derived from its profile (never stored by reading).
*/
public static function of(Booking $booking): self
{
$data = (array) (($booking->profile ?? [])['plan'] ?? []);
return $data !== [] ? self::fromArray($data) : self::derive($booking->process());
}
/**
* The plan a case gets from its profile when no model was chosen for its channel.
*/
public static function derive(ProcessProfile $profile): self
{
$catalogue = app(ProcessCatalogue::class);
return self::build($catalogue->model($catalogue->derivedModel($profile)), 'standard', [], self::DERIVED);
}
/**
* The plan frozen for a new case: the model and step settings chosen for its channel and intake kind (Kanal ›
* Ablauf), when admissible for the case's type; otherwise the derived one with a notice. Tester-only variants or
* modules are refused outside the process tester (decision E7).
*
* @param array<string, mixed> $configuration model, variant, steps of channels.process_overrides[intake]
*/
public static function freeze(ProcessProfile $profile, array $configuration, bool $sandbox = false): self
{
$catalogue = app(ProcessCatalogue::class);
// Inside a process tester run (its own process, SandboxBootstrapper) tester-only variants are allowed.
$sandbox = $sandbox || filled(config('sandbox.run'));
$chosen = (string) ($configuration['model'] ?? '');
if ($chosen === '') {
return self::build($catalogue->model($catalogue->derivedModel($profile)), 'standard', (array) ($configuration['steps'] ?? []), self::DERIVED);
}
if (! $catalogue->hasModel($chosen)) {
return self::derive($profile)->withNotice("Gewähltes Modell {$chosen} ist nicht registriert – abgeleitetes Modell verwendet.");
}
$model = $catalogue->model($chosen);
if (! in_array($profile->caseType, $model::caseTypes(), true) || ! in_array($profile->intake, $model::intakes(), true)) {
return self::derive($profile)->withNotice("Gewähltes Modell {$chosen} passt nicht zu Vorgangsart {$profile->caseType->value} – abgeleitetes Modell verwendet.");
}
$variant = (string) ($configuration['variant'] ?? 'standard');
$chosenVariant = $model->variant($variant) ?? throw new DomainException("Modell {$chosen} hat keine Variante {$variant}.");
if (! $sandbox && $chosenVariant->isTesterOnly()) {
throw new DomainException("Die Variante „{$chosenVariant->label}“ ist nur im Prozess-Tester verwendbar.");
}
foreach ($model->modules($variant) as $module) {
if (! $sandbox && $catalogue->module($module)::definition()->isTesterOnly()) {
throw new DomainException("Das Modul {$module} ist nur im Prozess-Tester verwendbar.");
}
}
return self::build($model, $variant, (array) ($configuration['steps'] ?? []), $sandbox ? self::TESTER : self::CONFIGURED);
}
/**
* The configuration of a channel for one intake kind as the plan reads it (model, variant, steps).
*
* @return array<string, mixed>
*/
public static function configurationOf(?Channel $channel, IntakeKind $intake): array
{
if ($channel === null || $intake === IntakeKind::Internal) {
return [];
}
return array_intersect_key((array) (($channel->process_overrides ?? [])[$intake->value] ?? []), array_flip(['model', 'variant', 'steps']));
}
/**
* @param array<string, mixed> $steps
*/
private static function build(ProcessModel $model, string $variant, array $steps, string $source): self
{
$catalogue = app(ProcessCatalogue::class);
$modules = [];
$labels = [];
foreach ($model->modules($variant) as $key) {
$class = $catalogue->modules()[$key];
$modules[$key] = $class::version();
$labels[$key] = $class::definition()->label;
}
return new self($model::key(), $model::version(), $variant, $modules, array_intersect_key($steps, $modules), $source, $labels);
}
private function withNotice(string $notice): self
{
return new self($this->model, $this->modelVersion, $this->variant, $this->modules, $this->steps, $this->source, $this->labels, $notice);
}
public function modelDefinition(): ProcessModel
{
$catalogue = app(ProcessCatalogue::class);
// A retired version (T3): its cases stay readable through the latest definition of the same model.
return $catalogue->hasModel($this->model, $this->modelVersion) ? $catalogue->model($this->model, $this->modelVersion) : $catalogue->model($this->model);
}
public function isHistoric(): bool
{
return ! app(ProcessCatalogue::class)->hasModel($this->model, $this->modelVersion);
}
/**
* The module keys of the case's course, in order.
*
* @return list<string>
*/
public function moduleKeys(): array
{
return array_keys($this->modules);
}
/** Whether a step is switched on (only optional steps can be switched off; mandatory ones are always on). */
public function enabled(string $module): bool
{
if (($this->steps[$module]['on'] ?? true) !== false) {
return true;
}
$catalogue = app(ProcessCatalogue::class);
return $catalogue->hasModule($module) && ! $catalogue->module($module)::definition()->isOptional();
}
public function mode(string $module): ?string
{
return $this->steps[$module]['mode'] ?? null;
}
public function performer(string $module): ?string
{
return $this->steps[$module]['performer'] ?? null;
}
public function parameter(string $module, string $name): mixed
{
return $this->steps[$module]['params'][$name] ?? null;
}
public function reference(): string
{
return $this->model.'@'.$this->modelVersion.($this->variant !== 'standard' ? ':'.$this->variant : '');
}
/**
* @return array<string, mixed>
*/
public function toArray(): array
{
return array_filter([
'model' => $this->model,
'model_version' => $this->modelVersion,
'variant' => $this->variant,
'modules' => $this->modules,
'steps' => $this->steps,
'source' => $this->source,
'labels' => $this->labels,
'notice' => $this->notice,
], fn (mixed $value): bool => $value !== null);
}
/**
* @param array<string, mixed> $data
*/
public static function fromArray(array $data): self
{
return new self(
(string) $data['model'],
(int) $data['model_version'],
(string) ($data['variant'] ?? 'standard'),
array_map('intval', (array) ($data['modules'] ?? [])),
(array) ($data['steps'] ?? []),
(string) ($data['source'] ?? self::DERIVED),
(array) ($data['labels'] ?? []),
isset($data['notice']) ? (string) $data['notice'] : null,
);
}
}