Esempio n. 1
0
 /**
  * @param Recipe $recipe
  * @param $ingredientsData
  */
 public function addIngredientsToRecipe(Recipe $recipe, $ingredientsData)
 {
     $names = array();
     foreach ($ingredientsData as $ingredient) {
         $names[] = $ingredient['name'];
     }
     $missingIngredients = array();
     $ingredientEntity = $this->ingredientService->getIngredient($names);
     foreach ($ingredientsData as $ingredient) {
         $found = false;
         for ($i = 0; $i < count($ingredientEntity); $i++) {
             if ($ingredient['name'] == $ingredientEntity[$i]->getName()) {
                 $found = true;
             }
         }
         if (!$found) {
             array_push($missingIngredients, $ingredient);
         }
     }
     $ingredients = $this->ingredientService->createIngredients($missingIngredients);
     $ingredients = array_merge($ingredients, $ingredientEntity);
     foreach ($ingredients as $ingredient) {
         $recipe->addIngredient($ingredient);
     }
 }