From 852fcb3cdb128711bb2b3a121a67cc8a06903a33 Mon Sep 17 00:00:00 2001 From: philippe Date: Tue, 3 Feb 2026 15:43:41 +0100 Subject: [PATCH] Refactor form controller flow --- src/Http/Controllers/FormController.php | 209 ++++++++++++++---------- 1 file changed, 119 insertions(+), 90 deletions(-) diff --git a/src/Http/Controllers/FormController.php b/src/Http/Controllers/FormController.php index 7bb7ea42f..6695f95ba 100644 --- a/src/Http/Controllers/FormController.php +++ b/src/Http/Controllers/FormController.php @@ -24,9 +24,7 @@ public function __construct( public function create(string $globalFilter, string $parentUri, EntityKey $entityKey) { - $entity = $this->entityManager->entityFor($entityKey); - - $form = $entity->getFormOrFail($entityKey->multiformKey()); + [$entity, $form] = $this->resolveEntityAndForm($entityKey); if ($form instanceof SharpSingleForm) { // There is no creation in SingleForms @@ -34,35 +32,28 @@ public function create(string $globalFilter, string $parentUri, EntityKey $entit } $this->authorizationManager->check('create', $entityKey); - $form->buildFormConfig(); - $formData = $form->newInstance(); - - if (app()->environment('testing')) { - Inertia::share('_rawData', $formData); - } - - return Inertia::render('Form/Form', [ - 'form' => FormData::from([ - ...$this->buildFormData($form, $formData, $entityKey), - 'title' => $form->getCreateTitle() ?: trans('sharp::breadcrumb.form.create_entity', [ - 'entity' => $entity->getLabelOrFail($entityKey->multiformKey()), - ]), - ]), - 'breadcrumb' => BreadcrumbData::from([ - 'items' => sharp()->context()->breadcrumb()->allSegments(), + $formData = $this->buildFormInstanceData($form, null); + + return $this->renderFormView( + form: $form, + formData: $formData, + entityKey: $entityKey, + instanceId: null, + title: $form->getCreateTitle() ?: trans('sharp::breadcrumb.form.create_entity', [ + 'entity' => $entity->getLabelOrFail($entityKey->multiformKey()), ]), - 'cancelUrl' => sharp()->context()->breadcrumb()->getPreviousSegmentUrl(), - 'endpointUrl' => route('code16.sharp.form.store', [ + cancelUrl: sharp()->context()->breadcrumb()->getPreviousSegmentUrl(), + endpointUrl: route('code16.sharp.form.store', [ 'parentUri' => $parentUri, 'entityKey' => $entityKey, 'previous_page_url' => $this->getPreviousPageUrlFromReferer(), ]), - ]); + ); } public function edit(string $globalFilter, string $parentUri, EntityKey $entityKey, ?string $instanceId = null) { - $entity = $this->entityManager->entityFor($entityKey); + [$entity, $form] = $this->resolveEntityAndForm($entityKey); $this->authorizationManager->check( $entity->hasShow() ? 'update' : 'view', @@ -70,80 +61,40 @@ public function edit(string $globalFilter, string $parentUri, EntityKey $entityK $instanceId ); - $form = $entity->getFormOrFail($entityKey->multiformKey()); + $this->ensureInstanceIdMatchesForm($form, $instanceId); - abort_if( - (! $instanceId && ! $form instanceof SharpSingleForm) - || ($instanceId && $form instanceof SharpSingleForm), - 404, - ); - - $form->buildFormConfig(); + $formData = $this->buildFormInstanceData($form, $instanceId); + $titleEntityLabel = $this->resolveTitleEntityLabel($form, $formData, $entity, $entityKey); - $formData = $form instanceof SharpSingleForm || $instanceId !== null - ? $form->instance($instanceId) - : $form->newInstance(); - - if ($breadcrumbLabel = $form->getBreadcrumbCustomLabel($formData)) { - sharp()->context()->breadcrumb()->setCurrentInstanceLabel($breadcrumbLabel); - $titleEntityLabel = $breadcrumbLabel; - } - - $titleEntityLabel ??= sharp() - ->context() - ->breadcrumb() - ->getParentShowCachedBreadcrumbLabel() ?: $entity->getLabelOrFail($entityKey->multiformKey()); - - if (app()->environment('testing')) { - Inertia::share('_rawData', $formData); - } - - return Inertia::render('Form/Form', [ - 'form' => FormData::from([ - ...$this->buildFormData($form, $formData, $entityKey, $instanceId), - 'title' => $form->getEditTitle() ?: trans('sharp::breadcrumb.form.edit_entity', [ - 'entity' => $titleEntityLabel, - ]), - ]), - 'breadcrumb' => BreadcrumbData::from([ - 'items' => sharp()->context()->breadcrumb()->allSegments(), + return $this->renderFormView( + form: $form, + formData: $formData, + entityKey: $entityKey, + instanceId: $instanceId, + title: $form->getEditTitle() ?: trans('sharp::breadcrumb.form.edit_entity', [ + 'entity' => $titleEntityLabel, ]), - 'cancelUrl' => $this->previousUrlWithHighlightedQuery( + cancelUrl: $this->previousUrlWithHighlightedQuery( sharp()->context()->breadcrumb()->getPreviousSegmentUrl(), $entityKey, $instanceId ), - 'endpointUrl' => route('code16.sharp.form.update', [ + endpointUrl: route('code16.sharp.form.update', [ 'parentUri' => $parentUri, 'entityKey' => $entityKey, 'instanceId' => $instanceId, 'previous_page_url' => $this->getPreviousPageUrlFromReferer(), ]), - ]); + ); } public function update(string $globalFilter, string $parentUri, EntityKey $entityKey, ?string $instanceId = null) { $this->authorizationManager->check('update', $entityKey, $instanceId); - $form = $this->entityManager - ->entityFor($entityKey) - ->getFormOrFail($entityKey->multiformKey()); - - abort_if( - (! $instanceId && ! $form instanceof SharpSingleForm) - || ($instanceId && $form instanceof SharpSingleForm), - 404, - ); - - $formattedData = $form->formatAndValidateRequestData(request()->all(), $instanceId); - $instanceId = $form->update($instanceId, $formattedData); - - if ($instanceId === null && ! $form instanceof SharpSingleForm) { - report(new SharpFormUpdateException('The update() method in '.get_class($form).' must return the newly created instance id')); - } - - $this->uploadManager->dispatchJobs($instanceId); + [, $form] = $this->resolveEntityAndForm($entityKey); + $this->ensureInstanceIdMatchesForm($form, $instanceId); + $instanceId = $this->persistForm($form, $instanceId); $previousUrl = request()->query('previous_page_url') ?: sharp()->context()->breadcrumb()->getPreviousSegmentUrl(); @@ -152,9 +103,7 @@ public function update(string $globalFilter, string $parentUri, EntityKey $entit public function store(string $globalFilter, string $parentUri, EntityKey $entityKey) { - $form = $this->entityManager - ->entityFor($entityKey) - ->getFormOrFail($entityKey->multiformKey()); + [, $form] = $this->resolveEntityAndForm($entityKey); if ($form instanceof SharpSingleForm) { // There is no creation in SingleForms @@ -163,14 +112,7 @@ public function store(string $globalFilter, string $parentUri, EntityKey $entity $this->authorizationManager->check('create', $entityKey); $form->buildFormConfig(); - $formattedData = $form->formatAndValidateRequestData(request()->all()); - $instanceId = $form->update(null, $formattedData); - - if ($instanceId === null) { - report(new SharpFormUpdateException('The update() method in '.get_class($form).' must return the newly created instance id')); - } - - $this->uploadManager->dispatchJobs($instanceId); + $instanceId = $this->persistForm($form, null); $previousUrl = sharp()->context()->breadcrumb()->getPreviousSegmentUrl(); @@ -198,6 +140,93 @@ private function previousUrlWithHighlightedQuery(string $url, EntityKey $entityK ]); } + /** + * @return array{0: mixed, 1: SharpForm} + */ + private function resolveEntityAndForm(EntityKey $entityKey): array + { + $entity = $this->entityManager->entityFor($entityKey); + + return [$entity, $entity->getFormOrFail($entityKey->multiformKey())]; + } + + private function ensureInstanceIdMatchesForm(SharpForm $form, ?string $instanceId): void + { + abort_if( + (! $instanceId && ! $form instanceof SharpSingleForm) + || ($instanceId && $form instanceof SharpSingleForm), + 404, + ); + } + + private function buildFormInstanceData(SharpForm $form, ?string $instanceId): array + { + $form->buildFormConfig(); + + return $form instanceof SharpSingleForm || $instanceId !== null + ? $form->instance($instanceId) + : $form->newInstance(); + } + + private function resolveTitleEntityLabel( + SharpForm $form, + array $formData, + mixed $entity, + EntityKey $entityKey, + ): string { + if ($breadcrumbLabel = $form->getBreadcrumbCustomLabel($formData)) { + sharp()->context()->breadcrumb()->setCurrentInstanceLabel($breadcrumbLabel); + + return $breadcrumbLabel; + } + + return sharp() + ->context() + ->breadcrumb() + ->getParentShowCachedBreadcrumbLabel() + ?: $entity->getLabelOrFail($entityKey->multiformKey()); + } + + private function renderFormView( + SharpForm $form, + array $formData, + EntityKey $entityKey, + ?string $instanceId, + string $title, + string $cancelUrl, + string $endpointUrl, + ) { + if (app()->environment('testing')) { + Inertia::share('_rawData', $formData); + } + + return Inertia::render('Form/Form', [ + 'form' => FormData::from([ + ...$this->buildFormData($form, $formData, $entityKey, $instanceId), + 'title' => $title, + ]), + 'breadcrumb' => BreadcrumbData::from([ + 'items' => sharp()->context()->breadcrumb()->allSegments(), + ]), + 'cancelUrl' => $cancelUrl, + 'endpointUrl' => $endpointUrl, + ]); + } + + private function persistForm(SharpForm $form, ?string $instanceId): ?string + { + $formattedData = $form->formatAndValidateRequestData(request()->all(), $instanceId); + $instanceId = $form->update($instanceId, $formattedData); + + if ($instanceId === null && ! $form instanceof SharpSingleForm) { + report(new SharpFormUpdateException('The update() method in '.get_class($form).' must return the newly created instance id')); + } + + $this->uploadManager->dispatchJobs($instanceId); + + return $instanceId; + } + private function getPreviousPageUrlFromReferer(): ?string { if (! request()->header('referer')) {