Files
fewo-stommer/app/Actions/ConfigureChannelProcess.php
T

69 lines
2.9 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\Actions;
use App\Enums\IntakeKind;
use App\Enums\ProcessSetting;
use App\Models\Channel;
use App\Process\ChannelProcessConfiguration;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
/**
* Saves the process of a channel for one intake kind (Kanal › Ablauf, docs/Prozessarchitektur/Architektur/03): the
* chosen model and variant, the step switches and the step settings – in channels.process_overrides[intake], next to
* the existing settings, so their inheritance stays as it is. Refuses everything the configuration does not admit
* (mandatory steps off, responsibilities the model does not allow, tester-only variants). Changes apply to new cases
* only (decision E3): running cases keep their frozen plan.
*/
class ConfigureChannelProcess
{
public function __construct(private ChannelProcessConfiguration $configuration) {}
/**
* @param array{model: string, variant?: string, steps?: array<string, array{on?: bool}>, settings?: array<string, mixed>} $draft
*
* @throws ValidationException
*/
public function handle(Channel $channel, IntakeKind $intake, array $draft, ?string $by = null): Channel
{
$problems = $this->configuration->problems($channel, $intake, $draft);
if ($problems !== []) {
throw ValidationException::withMessages(['process' => $problems]);
}
return DB::transaction(function () use ($channel, $intake, $draft, $by): Channel {
$channel = Channel::query()->lockForUpdate()->findOrFail($channel->id);
$overrides = (array) ($channel->process_overrides ?? []);
$current = (array) ($overrides[$intake->value] ?? []);
$settings = [];
foreach (ProcessSetting::cases() as $setting) {
$value = array_key_exists($setting->value, (array) ($draft['settings'] ?? [])) ? $setting->normalize($draft['settings'][$setting->value]) : $setting->normalize($current[$setting->value] ?? null);
if ($value !== null && in_array($intake, $setting->channelIntakes(), true)) {
$settings[$setting->value] = $value;
}
}
$steps = [];
foreach ((array) ($draft['steps'] ?? []) as $module => $step) {
if (array_key_exists('on', $step) && $step['on'] === false) {
$steps[$module] = ['on' => false];
}
}
$overrides[$intake->value] = [
...$settings,
'model' => $draft['model'],
'variant' => $draft['variant'] ?? 'standard',
'steps' => $steps,
'configured_at' => now()->toIso8601String(),
'configured_by' => $by ?? auth()->user()?->name,
];
$channel->update(['process_overrides' => $overrides]);
return $channel;
});
}
}