105 lines
4.7 KiB
PHP
105 lines
4.7 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use App\Enums\BookingStatus;
|
||
use App\Models\Booking;
|
||
use App\Process\ProcessCatalogue;
|
||
use App\Process\ProcessPlan;
|
||
use Illuminate\Console\Attributes\Description;
|
||
use Illuminate\Console\Attributes\Signature;
|
||
use Illuminate\Console\Command;
|
||
|
||
/**
|
||
* Checks the process catalogue (docs/Prozessarchitektur/Architektur/05 §2.2) and, with --cases, that every frozen plan
|
||
* of every case (open or finished, every landlord) can still be read: model versions and module versions registered or
|
||
* declared compatible (requirement T3). With --matrix it writes MODULMATRIX.md.
|
||
*/
|
||
#[Signature('process:check {--cases : also check the frozen plans of all cases} {--matrix : write docs/Prozessarchitektur/Architektur/MODULMATRIX.md}')]
|
||
#[Description('Check the process catalogue (modules, models, dependencies, tester-only parts) and the frozen plans of the cases')]
|
||
class ProcessCheck extends Command
|
||
{
|
||
public function handle(ProcessCatalogue $catalogue): int
|
||
{
|
||
$problems = $catalogue->problems();
|
||
|
||
if ($this->option('cases')) {
|
||
$problems = [...$problems, ...$this->cases($catalogue)];
|
||
}
|
||
|
||
if ($this->option('matrix')) {
|
||
$path = base_path('docs/Prozessarchitektur/Architektur/MODULMATRIX.md');
|
||
file_put_contents($path, $this->matrix($catalogue));
|
||
$this->info('Modulmatrix geschrieben: '.$path);
|
||
}
|
||
|
||
foreach ($problems as $problem) {
|
||
$this->error($problem);
|
||
}
|
||
if ($problems === []) {
|
||
$this->info(count($catalogue->modules()).' Module, '.count($catalogue->modelVersions()).' Modelle: ohne Befund.');
|
||
}
|
||
|
||
return $problems === [] ? self::SUCCESS : self::FAILURE;
|
||
}
|
||
|
||
/**
|
||
* @return list<string>
|
||
*/
|
||
private function cases(ProcessCatalogue $catalogue): array
|
||
{
|
||
$problems = [];
|
||
Booking::withoutGlobalScopes()->select(['id', 'reference', 'profile', 'status'])->whereNotNull('profile')->lazyById()->each(function (Booking $booking) use ($catalogue, &$problems): void {
|
||
$plan = (array) (($booking->profile ?? [])['plan'] ?? []);
|
||
if ($plan === []) {
|
||
return;
|
||
}
|
||
$frozen = ProcessPlan::fromArray($plan);
|
||
if (! $catalogue->hasModel($frozen->model)) {
|
||
$problems[] = "Vorgang {$booking->reference}: Modell {$frozen->model} ist nicht mehr registriert.";
|
||
} elseif (! $catalogue->hasModel($frozen->model, $frozen->modelVersion)) {
|
||
$problems[] = "Vorgang {$booking->reference}: Modellversion {$frozen->reference()} fehlt im Register (T3: Versionen nicht entfernen, solange Vorgänge sie tragen).";
|
||
}
|
||
foreach ($frozen->modules as $key => $version) {
|
||
if (! $catalogue->serves($key, $version) && ! in_array($booking->status, [BookingStatus::Cancelled, BookingStatus::Declined, BookingStatus::Expired, BookingStatus::Completed], true)) {
|
||
$problems[] = "Offener Vorgang {$booking->reference}: Modul {$key}@{$version} wird nicht mehr bedient.";
|
||
}
|
||
}
|
||
});
|
||
|
||
return $problems;
|
||
}
|
||
|
||
private function matrix(ProcessCatalogue $catalogue): string
|
||
{
|
||
$models = $catalogue->models();
|
||
$lines = [
|
||
'# Modulmatrix (erzeugt)',
|
||
'',
|
||
'Erzeugt von `php artisan process:check --matrix` aus `config/process.php` – nicht von Hand ändern. Spalten: Modelle (Standardablauf; `V` = nur in einer Variante).',
|
||
'',
|
||
'| Modul | Version | Phase | Aktivierung | Verfügbarkeit | hart abhängig von | '.implode(' | ', array_keys($models)).' | Testgruppe |',
|
||
'|'.str_repeat('---|', 7 + count($models)),
|
||
];
|
||
foreach ($catalogue->modules() as $key => $class) {
|
||
$definition = $class::definition();
|
||
$cells = array_map(function ($model) use ($key): string {
|
||
if (in_array($key, $model->modules('standard'), true)) {
|
||
return '●';
|
||
}
|
||
foreach ($model->variants() as $variant) {
|
||
if (in_array($key, $model->modules($variant->key), true)) {
|
||
return 'V';
|
||
}
|
||
}
|
||
|
||
return '';
|
||
}, $models);
|
||
$hard = implode(', ', array_map(fn ($dependency): string => $dependency->module, array_filter($definition->requires, fn ($dependency): bool => $dependency->hard)));
|
||
$lines[] = "| `{$key}` | {$class::version()} | {$definition->phase} | {$definition->toggle} | {$definition->availability} | {$hard} | ".implode(' | ', $cells)." | `module:{$key}` |";
|
||
}
|
||
|
||
return implode("\n", $lines)."\n";
|
||
}
|
||
}
|