refactor(UpdateProductRequest): simplify file handling logic

Refactor the file handling logic in UpdateProductRequest by extracting
picture and PDF resolution into separate methods for better readability
and maintainability.
This commit is contained in:
Jérémy Munsch 2026-11-05 18:14:37 +01:00
parent aff271570e
commit 779fc59993
Signed by: forge-joniko
GPG key ID: D7EFE2F5CB6CC0E4
3 changed files with 29 additions and 20 deletions

View file

@ -7,7 +7,6 @@ namespace App\Cqs\Queries\Products;
use App\Contracts\Directories\ProductDirectory;
use App\Contracts\Query;
use App\Contracts\QueryInput;
use App\Dto\CategoryProductsDto;
use Domain\Models\ValueObjects\Products\ProductCategory;
use Webmozart\Assert\Assert;
@ -22,7 +21,7 @@ final readonly class GetCategoriesProductsQuery implements Query
}
/**
* @return array<int, CategoryProductsDto>
* @return array<int, \App\Dto\CategoryProductsDto>
*/
public function __invoke(QueryInput $input): array
{

View file

@ -14,7 +14,11 @@ use Illuminate\Pagination\LengthAwarePaginator;
use Webmozart\Assert\Assert;
/**
* phpcs:disable Generic.Files.LineLength.TooLong
*
* @implements \App\Contracts\Query<\App\Cqs\Queries\Recipes\GetFrontRecipesQueryInput, \Illuminate\Pagination\LengthAwarePaginator<array-key, \App\Dto\RecipeDto>>
*
* phpcs:enable
*/
final readonly class GetFrontRecipesQuery implements Query
{

View file

@ -26,24 +26,8 @@ class UpdateProductRequest extends StoreProductRequest
]);
}
$existingPicture = $product->picture !== null ? new StorageFilePath($product->picture) : null;
$existingPdf = $product->pdf !== null ? new StorageFilePath($product->pdf) : null;
$picture = null;
if ($this->hasFile('picture')) {
$picture = $this->storeFile($this->file('picture'));
} elseif ($existingPicture !== null) {
$picture = $existingPicture;
}
$pdf = null;
if ($this->hasFile('pdf')) {
$pdf = $this->storeFile($this->file('pdf'));
} elseif ($this->exists('pdf') && $this->input('pdf') === null) {
$pdf = null;
} elseif ($existingPdf !== null) {
$pdf = $existingPdf;
}
$picture = $this->resolvePicture($product->picture);
$pdf = $this->resolvePdf($product->pdf);
return new UpdateProductInput(
id: $product->getId(),
@ -109,4 +93,26 @@ class UpdateProductRequest extends StoreProductRequest
],
]);
}
private function resolvePicture(?string $existingPicture): ?StorageFilePath
{
if ($this->hasFile('picture')) {
return $this->storeFile($this->file('picture'));
}
return $existingPicture !== null ? new StorageFilePath($existingPicture) : null;
}
private function resolvePdf(?string $existingPdf): ?StorageFilePath
{
if ($this->hasFile('pdf')) {
return $this->storeFile($this->file('pdf'));
}
if ($this->exists('pdf') && $this->input('pdf') === null) {
return null;
}
return $existingPdf !== null ? new StorageFilePath($existingPdf) : null;
}
}