Skip to content

Commit

Permalink
Merge pull request #263 from AOEpeople/feature/255133-change-non-divi…
Browse files Browse the repository at this point in the history
…sible-meals-behavior

Change behavior of one-size meals and the combine meal
  • Loading branch information
hacksch authored Oct 26, 2022
2 parents 1898aed + c7d9339 commit abc6acf
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 20 deletions.
1 change: 1 addition & 0 deletions phpmd.baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<violation rule="PHPMD\Rule\Design\CouplingBetweenObjects" file="src/Mealz/MealBundle/Controller/ParticipantController.php"/>
<violation rule="PHPMD\Rule\Naming\LongVariable" file="src/Mealz/MealBundle/Entity/Day.php"/>
<violation rule="PHPMD\Rule\CyclomaticComplexity" file="src/Mealz/MealBundle/Service/CombinedMealService.php" method="update"/>
<violation rule="PHPMD\Rule\Design\NpathComplexity" file="src/Mealz/MealBundle/Service/CombinedMealService.php" method="update"/>
<violation rule="PHPMD\Rule\Design\CouplingBetweenObjects" file="src/Mealz/MealBundle/Service/GuestParticipationService.php"/>
<violation rule="PHPMD\Rule\CyclomaticComplexity" file="src/Mealz/MealBundle/Service/Link.php" method="linkMeal"/>
<violation rule="PHPMD\Rule\CyclomaticComplexity" file="src/Mealz/MealBundle/Service/ParticipationCountService.php" method="getParticipationByDay"/>
Expand Down
5 changes: 3 additions & 2 deletions src/Mealz/MealBundle/DataFixtures/ORM/LoadDishes.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function load(ObjectManager $manager): void

$this->loadCategories();

$this->addDish('Braaaaaiiinnnzzzzzz', 'Braaaaaiiinnnzzzzzz DE');
$this->addDish('Braaaaaiiinnnzzzzzz', 'Braaaaaiiinnnzzzzzz DE', 'Description', 'Beschreibung', true);
$this->addDish('Tasty Worms', 'Tasty Worms DE');
$this->addDish('Innards', 'Innards DE');
$this->addDish('Fish (so juicy sweat)', 'Fish (so juicy sweat) DE');
Expand Down Expand Up @@ -69,7 +69,7 @@ public function getOrder(): int
return self::ORDER_NUMBER;
}

protected function addDish(string $titleEN, string $titleDE, string $descEN = null, string $descDE = null): void
protected function addDish(string $titleEN, string $titleDE, string $descEN = null, string $descDE = null, bool $oneSize = false): void
{
$dish = new Dish();
$dish->setPrice(3.60);
Expand All @@ -79,6 +79,7 @@ protected function addDish(string $titleEN, string $titleDE, string $descEN = nu
$dish->setDescriptionDe($descDE ?? $descEN ?? 'Beschreibung - ' . $titleDE);
$randomCategory = (0 === count($this->categories)) ? null : $this->categories[array_rand($this->categories, 1)];
$dish->setCategory($randomCategory);
$dish->setOneServingSize($oneSize);
$this->objectManager->persist($dish);
$this->addReference('dish-' . $this->counter++, $dish);
}
Expand Down
25 changes: 19 additions & 6 deletions src/Mealz/MealBundle/DataFixtures/ORM/LoadParticipants.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,16 @@ private function loadCombinedMealParticipants(): void
$days = $this->getDaysWithDishVariations();

foreach ($days as $day) {
$combinedMeal = $this->getCombinedMeal($day);
$combinedMealDishes = $this->getRandomCombinedMealDishes($day);
$profile = $this->getProfile($username);
if (true === $this->hasCombinedMeal($day)) {
$combinedMeal = $this->getCombinedMeal($day);
$combinedMealDishes = $this->getRandomCombinedMealDishes($day);
$profile = $this->getProfile($username);

$participant = new Participant($profile, $combinedMeal);
$participant->setCombinedDishes($combinedMealDishes);
$participant = new Participant($profile, $combinedMeal);
$participant->setCombinedDishes($combinedMealDishes);

$this->objectManager->persist($participant);
$this->objectManager->persist($participant);
}
}
}

Expand All @@ -111,6 +113,17 @@ private function getDaysWithDishVariations(): array
return array_values($days);
}

private function hasCombinedMeal(Day $day): bool
{
foreach ($day->getMeals() as $meal) {
if ($meal->isCombinedMeal()) {
return true;
}
}

return false;
}

private function getCombinedMeal(Day $day): Meal
{
foreach ($day->getMeals() as $meal) {
Expand Down
20 changes: 14 additions & 6 deletions src/Mealz/MealBundle/Service/CombinedMealService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ class CombinedMealService

private Dish $combinedDish;

public function __construct(float $combinedPrice, EntityManagerInterface $entityManager, DishRepository $dishRepo)
{
public function __construct(
float $combinedPrice,
EntityManagerInterface $entityManager,
DishRepository $dishRepo
) {
$this->defaultPrice = $combinedPrice;
$this->entityManager = $entityManager;
$this->combinedDish = $this->createCombinedDish($dishRepo);
Expand All @@ -33,11 +36,12 @@ public function update(Week $week): void
$update = false;
/** @var Day $day */
foreach ($week->getDays() as $day) {
if (empty($day->getMeals())) {
if (true === empty($day->getMeals())) {
continue;
}

$combinedMeal = null;
$dayHasOneSizeMeal = false;
$baseMeals = []; // NOTE: in case of variations, we only need the parent
/** @var Meal $meal */
foreach ($day->getMeals() as $meal) {
Expand All @@ -48,18 +52,22 @@ public function update(Week $week): void
} elseif (null !== $meal->getDish()->getParent()) {
$baseMeals[$meal->getDish()->getParent()->getId()] = $meal->getDish()->getParent();
}

if (true === $meal->getDish()->hasOneServingSize()) {
$dayHasOneSizeMeal = true;
}
}

if (null === $combinedMeal && 1 < count($baseMeals)) {
if (null === $combinedMeal && 1 < count($baseMeals) && false === $dayHasOneSizeMeal) {
$this->createCombinedMeal($day);
$update = true;
} elseif (null !== $combinedMeal && 1 >= count($baseMeals)) {
} elseif (null !== $combinedMeal && (1 >= count($baseMeals) || true === $dayHasOneSizeMeal)) {
$this->removeCombinedMeal($day, $combinedMeal);
$update = true;
}
}

if ($update) {
if (true === $update) {
$this->entityManager->persist($week);
$this->entityManager->flush();
}
Expand Down
6 changes: 1 addition & 5 deletions src/Mealz/MealBundle/Service/ParticipationCountService.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,7 @@ public static function setParticipationForCombinedDish(Meal $meal, array &$parti
$participation[self::PARTICIPATION_TOTAL_COUNT_KEY][$dish->getSlug()][self::COUNT_KEY] = 0.0;
}

if ($dish->hasOneServingSize()) {
$participation[self::PARTICIPATION_TOTAL_COUNT_KEY][$dish->getSlug()][self::COUNT_KEY] += 1.0;
} else {
$participation[self::PARTICIPATION_TOTAL_COUNT_KEY][$dish->getSlug()][self::COUNT_KEY] += 0.5;
}
$participation[self::PARTICIPATION_TOTAL_COUNT_KEY][$dish->getSlug()][self::COUNT_KEY] += 0.5;

if (!array_key_exists($dish->getSlug(), $participation[self::PARTICIPATION_COUNT_KEY][$meal->getId()])) {
$participation[self::PARTICIPATION_COUNT_KEY][$meal->getId()][$dish->getSlug()][self::COUNT_KEY] = 0.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function updateWeek(): void

/** @var Day $day */
foreach ($week->getDays() as $day) {
$dayHasOneSizeMeal = false;
$combinedMeal = null;
$baseMeals = [];
/** @var Meal $meal */
Expand All @@ -92,9 +93,13 @@ public function updateWeek(): void
} elseif (null !== $meal->getDish()->getParent()) {
$baseMeals[$meal->getDish()->getParent()->getId()] = $meal->getDish()->getParent();
}

if (true === $meal->getDish()->hasOneServingSize()) {
$dayHasOneSizeMeal = true;
}
}

if (2 <= count($baseMeals)) {
if (2 <= count($baseMeals) && false === $dayHasOneSizeMeal) {
$this->assertNotNull($combinedMeal);
$this->assertEquals($combinedMeal->getDish()->getId(), $this->combinedDish->getId());
} else {
Expand Down

0 comments on commit abc6acf

Please sign in to comment.