public function test_recipe_with_description_serialization() { $serializer = new JsonSerializer(); $recipe = new Recipe(new Name("Screwdriver")); $description = new Description(); $description->setText("Lorem ipsum"); $description->markAsIBAOfficial(); $description->setAlcoholContent(25); $taste = (new TasteBuilder())->sour()->spicy()->buildTaste(); $description->changeTaste($taste); $recipe->updateDescription($description); $json = json_encode(["name" => "Screwdriver", "publicationDate" => null, "steps" => [], "glass" => null, "description" => ["text" => "Lorem ipsum", "IBAOfficial" => true, "alcoholContent" => 25, "taste" => [Tastes::SOUR, Tastes::SPICY]]]); $this->assertJsonStringEqualsJsonString($json, $serializer->serialize($recipe)); $this->assertEquals($recipe, $serializer->deserialize($json, Recipe::class)); }
/** * @param UpdateRecipeDescriptionCommand $command * @throws RecipeNotFoundException * @throws \MyDrinks\Domain\Exception\InvalidArgumentException */ public function handle(UpdateRecipeDescriptionCommand $command) { $recipe = $this->recipes->findBySlug($command->slug); if (is_null($recipe)) { throw new RecipeNotFoundException(); } $description = new Description(); if ((bool) $command->IBAOfficial) { $description->markAsIBAOfficial(); } if (strlen($command->text)) { $description->setText($command->text); } if (!is_null($command->alcoholContent)) { $description->setAlcoholContent($command->alcoholContent); } if (!empty($command->taste) && is_array($command->taste)) { $tasteBuilder = new TasteBuilder(); foreach ($command->taste as $tasteName) { switch ($tasteName) { case Tastes::SWEET: $tasteBuilder->sweet(); break; case Tastes::BITTER: $tasteBuilder->bitter(); break; case Tastes::SALTY: $tasteBuilder->salty(); break; case Tastes::SPICY: $tasteBuilder->spicy(); break; case Tastes::SOUR: $tasteBuilder->sour(); break; } } $description->changeTaste($tasteBuilder->buildTaste()); } $recipe->updateDescription($description); }