Refactor the file handling logic in UpdateProductRequest by extracting picture and PDF resolution into separate methods for better readability and maintainability.
70 lines
2 KiB
PHP
70 lines
2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Cqs\Queries\Recipes;
|
|
|
|
use App\Contracts\Directories\RecipeDirectory;
|
|
use App\Contracts\Query;
|
|
use App\Contracts\QueryInput;
|
|
use App\Dto\RecipeDto;
|
|
use App\Lib\PublicS3Disk;
|
|
use Domain\Models\ValueObjects\Pagination;
|
|
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
|
|
{
|
|
private const int PER_PAGE = 100;
|
|
|
|
public function __construct(
|
|
private RecipeDirectory $recipeDirectory,
|
|
private PublicS3Disk $publicS3Disk,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @return LengthAwarePaginator<array-key, \App\Dto\RecipeDto>
|
|
*/
|
|
public function __invoke(QueryInput $input): LengthAwarePaginator
|
|
{
|
|
Assert::isAOf($input, GetFrontRecipesQueryInput::class);
|
|
|
|
// * Load all recipes for client-side display
|
|
$allRecipes = [];
|
|
$page = 1;
|
|
|
|
do {
|
|
$pagination = Pagination::ofPage($page, self::PER_PAGE);
|
|
|
|
$paginatedRecipes = $this->recipeDirectory->getPaginatedRecipes(
|
|
pagination: $pagination,
|
|
productRanges: $input->categories,
|
|
consumptionTimes: $input->consumptionTimes,
|
|
inspirations: $input->inspirations,
|
|
seasons: $input->seasons,
|
|
search: $input->search
|
|
);
|
|
|
|
foreach ($paginatedRecipes->items() as $recipe) {
|
|
$allRecipes[] = RecipeDto::fromEntity($recipe, $this->publicS3Disk);
|
|
}
|
|
|
|
$page++;
|
|
} while ($page <= $paginatedRecipes->lastPage());
|
|
|
|
return new LengthAwarePaginator(
|
|
items: $allRecipes,
|
|
total: count($allRecipes),
|
|
perPage: max(1, count($allRecipes)),
|
|
currentPage: 1,
|
|
);
|
|
}
|
|
}
|