63 lines
3.1 KiB
PHP
63 lines
3.1 KiB
PHP
<?php
|
||
|
||
namespace App\Process;
|
||
|
||
use App\Process\Modules\ModuleDefinition;
|
||
|
||
/**
|
||
* The course of a resolved plan as the runner executes it: which steps start at once and run in parallel, which wait
|
||
* for others, which are conditional, event-driven or time-driven. Derived only from the same facts the
|
||
* {@see ProcessRunner} uses – the hard dependencies among the steps that are in the plan and apply, `applies()` (as the
|
||
* preview row's reason), the trigger kinds and what a step waits for. No grouping by phase, no hand-written text.
|
||
* Pure: it reads, it never changes anything.
|
||
*/
|
||
final class ProcessLanes
|
||
{
|
||
public function __construct(private ProcessCatalogue $catalogue) {}
|
||
|
||
/**
|
||
* @param list<array{module: string, label: string, on: bool, reason: ?string}> $rows the preview rows of a draft (ChannelProcessConfiguration::preview)
|
||
* @return array{parallel: list<array{module: string, label: string, note: ?string}>, waiting: list<array{module: string, label: string, note: ?string, after: list<string>}>, event: list<array{module: string, label: string, note: ?string}>, later: list<array{module: string, label: string, note: ?string}>, off: list<array{module: string, label: string, note: ?string}>}
|
||
*/
|
||
public function of(array $rows): array
|
||
{
|
||
$active = [];
|
||
foreach ($rows as $row) {
|
||
if ($row['on']) {
|
||
$active[$row['module']] = $row['label'];
|
||
}
|
||
}
|
||
$lanes = ['parallel' => [], 'waiting' => [], 'event' => [], 'later' => [], 'off' => []];
|
||
|
||
foreach ($rows as $row) {
|
||
$definition = $this->catalogue->modules()[$row['module']]::definition();
|
||
if (! $row['on']) {
|
||
$lanes['off'][] = ['module' => $row['module'], 'label' => $row['label'], 'note' => $row['reason']];
|
||
|
||
continue;
|
||
}
|
||
if ($row['module'] === 'intake.record') {
|
||
continue;
|
||
}
|
||
$hard = array_values(array_map(fn ($dependency): string => $active[$dependency->module], array_filter($definition->requires, fn ($dependency): bool => $dependency->hard && $dependency->module !== 'intake.record' && isset($active[$dependency->module]))));
|
||
$item = ['module' => $row['module'], 'label' => $row['label'], 'note' => $this->note($definition)];
|
||
|
||
$lanes[match (true) {
|
||
$hard !== [] => 'waiting',
|
||
$definition->toggle === ModuleDefinition::TOGGLE_EVENT_DRIVEN => 'event',
|
||
$definition->triggers !== [] && collect($definition->triggers)->every(fn (string $trigger): bool => str_starts_with($trigger, 'schedule:')) => 'later',
|
||
default => 'parallel',
|
||
}][] = $hard !== [] ? [...$item, 'after' => $hard] : $item;
|
||
}
|
||
|
||
return $lanes;
|
||
}
|
||
|
||
private function note(ModuleDefinition $definition): ?string
|
||
{
|
||
$waits = array_map(fn (string $wait): string => ['guest' => 'den Gast', 'portal' => 'das Portal', 'landlord' => 'Sie', 'time' => 'eine Frist'][$wait] ?? $wait, $definition->waits);
|
||
|
||
return $waits === [] ? null : 'kann warten auf '.implode(', ', $waits);
|
||
}
|
||
}
|