Skip to content

Instantly share code, notes, and snippets.

@samijnih
Created July 3, 2021 20:48
Show Gist options
  • Save samijnih/b4b3e68f92e48512b5e88fb7de1c50da to your computer and use it in GitHub Desktop.
Save samijnih/b4b3e68f92e48512b5e88fb7de1c50da to your computer and use it in GitHub Desktop.
Command and handler for creating a recipe.
<?php
declare(strict_types=1);
namespace Application\UseCases\Recipes;
final class CreateRecipe
{
public function __construct(
private string $id,
private string $name,
private array $ingredients,
private array $steps,
) {
}
public function getId(): string
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
public function getIngredients(): array
{
return $this->ingredients;
}
public function getSteps(): array
{
return $this->steps;
}
}
<?php
declare(strict_types=1);
namespace Application\UseCases\Recipes;
use Domain\Model\Recipes\Recipe;
use Domain\Model\Recipes\Recipe\ValueObject\Ingredients;
use Domain\Model\Recipes\Recipe\ValueObject\Name;
use Domain\Model\Recipes\Recipe\ValueObject\RecipeId;
use Domain\Model\Recipes\Recipe\ValueObject\Steps;
use Domain\Model\Recipes\RecipeRepository;
final class HandleCreateRecipe
{
public function __construct(
private RecipeRepository $recipeRepository,
) {
}
public function execute(CreateRecipe $createRecipe): void
{
$recipe = Recipe::create(
RecipeId::fromString($createRecipe->getId()),
Name::fromString($createRecipe->getName()),
Ingredients::fromArray($createRecipe->getIngredients()),
Steps::fromArray($createRecipe->getSteps())
);
$this->recipeRepository->add($recipe);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment