validate([ 'installation_key' => ['required', 'string', 'max:255', new ValidateInstallationKeyRule], ]); try { WebsiteInstallation::firstOrFail()->update([ 'step' => 1, 'user_ip' => $request->ip(), ]); } catch (\Exception $e) { return back()->withErrors(['message' => 'Installation record not found. Please restart.']); } return to_route('installation.show-step', 1); } public function showStep(int $currentStep): View { $settings = $this->getSettingsForStep($currentStep); return view('installation.step-' . $currentStep, [ 'settings' => $settings, ]); } public function saveStepSettings(Request $request): RedirectResponse { $this->updateSettings($request); $installation = WebsiteInstallation::firstOrFail(); $installation->increment('step'); return to_route('installation.show-step', $installation->step); } public function previousStep(): RedirectResponse { $installation = WebsiteInstallation::firstOrFail(); $installation->decrement('step'); return to_route('installation.show-step', $installation->step); } public function restartInstallation(): RedirectResponse { try { WebsiteInstallation::firstOrFail()->update([ 'step' => 0, 'installation_key' => Str::uuid(), 'user_ip' => null, ]); } catch (\Exception $e) { return to_route('installation.index'); } WebsiteSetting::where('key', 'theme')->update([ 'value' => 'atom', ]); return to_route('installation.index'); } public function completeInstallation(): RedirectResponse { Cache::forget('website_permissions'); Cache::forget('website_settings'); try { WebsiteInstallation::latest()->firstOrFail()->update([ 'completed' => true, ]); } catch (\Exception $e) { return to_route('installation.index'); } return to_route('welcome'); } private function updateSettings(Request $request): void { $data = $request->except(['_token', '_method']); foreach ($data as $key => $value) { if (! in_array($key, self::ALLOWED_SETTINGS)) { continue; } WebsiteSetting::where('key', $key)->update([ 'value' => is_array($value) ? json_encode($value) : (string) $value, ]); } } private function getSettingsForStep(int $step): Collection { $allKeys = WebsiteSetting::pluck('key')->toArray(); $settingsData = array_chunk($allKeys, (int) ceil(count($allKeys) / 4)); $settings = match ($step) { 1 => $settingsData[0] ?? [], 2 => $settingsData[1] ?? [], 3 => $settingsData[2] ?? [], 4 => $settingsData[3] ?? [], 5 => [], default => throw new Exception('Step does not exist'), }; return WebsiteSetting::query() ->whereIn('key', $settings) ->select(['key', 'value', 'comment']) ->get(); } }