56 lines
1.9 KiB
PHP
56 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Cqs\Actions\Recipes\Create;
|
|
|
|
use App\Contracts\Action;
|
|
use App\Contracts\ActionInput;
|
|
use App\Models\Recipe;
|
|
use Domain\Models\ValueObjects\Entities\RecipeId;
|
|
use Ramsey\Uuid\Uuid;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/**
|
|
* phpcs:disable Generic.Files.LineLength.TooLong
|
|
*
|
|
* @implements \App\Contracts\Action<\App\Cqs\Actions\Recipes\Create\CreateRecipeInput, \Domain\Models\ValueObjects\Entities\RecipeId|\App\Cqs\Actions\Recipes\Create\FailedToCreateRecipe>
|
|
* phpcs:enable
|
|
*/
|
|
final class CreateRecipe implements Action
|
|
{
|
|
/**
|
|
* @param \App\Cqs\Actions\Recipes\Create\CreateRecipeInput $input
|
|
*/
|
|
public function __invoke(ActionInput $input): RecipeId | FailedToCreateRecipe
|
|
{
|
|
Assert::isInstanceOf($input, CreateRecipeInput::class);
|
|
|
|
$recipe = new Recipe([
|
|
'seo_title' => $input->seoTitle,
|
|
'seo_description' => $input->seoDescription,
|
|
'name' => $input->name,
|
|
'is_published' => $input->isPublished,
|
|
'picture' => $input->picture,
|
|
'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 (! $recipe->save()) {
|
|
return new FailedToCreateRecipe();
|
|
}
|
|
|
|
$recipe->products()->sync($input->products);
|
|
|
|
return new RecipeId(Uuid::fromString($recipe->id));
|
|
}
|
|
}
|