75 lines
2.5 KiB
PHP
75 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Cqs\Actions\Recipes\Update;
|
|
|
|
use App\Contracts\Action;
|
|
use App\Contracts\ActionInput;
|
|
use App\Dto\RecipeDto;
|
|
use App\Lib\PublicS3Disk;
|
|
use App\Models\Recipe;
|
|
use Uwil\Storage\Contracts\StorageService;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/**
|
|
* phpcs:disable Generic.Files.LineLength.TooLong
|
|
*
|
|
* @implements \App\Contracts\Action<\App\Cqs\Actions\Recipes\Update\UpdateRecipeInput, \App\Dto\RecipeDto|\App\Cqs\Actions\Recipes\Update\FailedToUpdateRecipe>
|
|
* phpcs:enable
|
|
*/
|
|
final class UpdateRecipe implements Action
|
|
{
|
|
public function __construct(
|
|
private readonly StorageService $storageService,
|
|
private readonly PublicS3Disk $s3Disk,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @param \App\Cqs\Actions\Recipes\Update\UpdateRecipeInput $input
|
|
*/
|
|
public function __invoke(ActionInput $input): RecipeDto | FailedToUpdateRecipe
|
|
{
|
|
Assert::isInstanceOf($input, UpdateRecipeInput::class);
|
|
|
|
$recipe = Recipe::query()->findOrFail((string) $input->id->value);
|
|
|
|
$oldPicture = $recipe->picture;
|
|
|
|
$recipe->fill([
|
|
'seo_title' => $input->seoTitle,
|
|
'seo_description' => $input->seoDescription,
|
|
'name' => $input->name,
|
|
'is_published' => $input->isPublished,
|
|
'picture_alt' => $input->pictureAlt,
|
|
'picture_title' => $input->pictureTitle,
|
|
'product_category' => $input->productCategory,
|
|
'consomption_time' => $input->consomptionTime,
|
|
'inspiration' => $input->inspiration,
|
|
'season' => $input->season,
|
|
'description' => $input->description,
|
|
'details' => $input->details,
|
|
'ingredients_list' => $input->ingredientsList,
|
|
'preparation_steps' => $input->preparationSteps,
|
|
'tips_and_advices' => $input->tipsAndAdvices,
|
|
]);
|
|
|
|
if ($input->picture !== null) {
|
|
$recipe->picture = $input->picture;
|
|
}
|
|
|
|
if (! $recipe->save()) {
|
|
return new FailedToUpdateRecipe();
|
|
}
|
|
|
|
$recipe->products()->sync($input->products);
|
|
|
|
// Clean up replaced S3 picture
|
|
if ($input->picture !== null && $oldPicture !== null && $oldPicture->value !== $input->picture->value) {
|
|
$this->storageService->removeFromDisk($oldPicture, $this->s3Disk->disk);
|
|
}
|
|
|
|
return RecipeDto::fromModel($recipe, $this->s3Disk);
|
|
}
|
|
}
|