, settings?: array, capabilities?: array} $draft * @param bool $acknowledgeConflicts the landlord confirmed that stored settings the model overrides are removed * * @throws ValidationException */ public function handle(Channel $channel, IntakeKind $intake, array $draft, ?string $by = null, bool $acknowledgeConflicts = false): Channel { $problems = $this->configuration->problems($channel, $intake, $draft); if ($problems !== []) { throw ValidationException::withMessages(['process' => $problems]); } $conflicts = $this->configuration->conflicts($channel, $intake, (string) $draft['model']); if ($conflicts !== [] && ! $acknowledgeConflicts) { throw ValidationException::withMessages(['process' => [ 'Bisherige Einstellungen widersprechen dem gewählten Modell und würden still überstimmt. Bitte ausdrücklich bestätigen, dass sie entfernt werden:', ...array_column($conflicts, 'text'), ]]); } return DB::transaction(function () use ($channel, $intake, $draft, $by, $conflicts): Channel { $channel = Channel::query()->lockForUpdate()->findOrFail($channel->id); $by ??= auth()->user()?->name; $overrides = (array) ($channel->process_overrides ?? []); $current = (array) ($overrides[$intake->value] ?? []); $removed = array_map(fn (array $conflict): string => $conflict['setting']->value, $conflicts); $settings = []; foreach (ProcessSetting::cases() as $setting) { if (in_array($setting->value, $removed, true)) { continue; } $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, ]; $channelSettings = (array) ($channel->settings ?? []); $changes = []; if (! $channel->isWebsite() && filled($draft['capabilities'] ?? null)) { $confirmed = $this->capabilities->confirm($channel, (array) $draft['capabilities'], $by); $channelSettings['capabilities'] = $confirmed['capabilities']; $changes = $confirmed['changes']; } $channelSettings['process_history'] = array_slice([...(array) ($channelSettings['process_history'] ?? []), [ 'at' => now()->toIso8601String(), 'by' => $by, 'intake' => $intake->value, 'model' => $draft['model'], 'variant' => $draft['variant'] ?? 'standard', 'capabilities' => $changes, 'removed_settings' => array_map(fn (array $conflict): string => $conflict['text'], $conflicts), ]], -self::HISTORY_LIMIT); $channel->update(['process_overrides' => $overrides, 'settings' => $channelSettings]); return $channel; }); } }