diff --git a/src/Mapping/Transformer/BudgetMapTransformer.php b/src/Mapping/Transformer/BudgetMapTransformer.php index f3f4157..d33f634 100644 --- a/src/Mapping/Transformer/BudgetMapTransformer.php +++ b/src/Mapping/Transformer/BudgetMapTransformer.php @@ -14,10 +14,6 @@ use AutoMapper\Transformer\PropertyTransformer\PropertyTransformerInterface; class BudgetMapTransformer implements PropertyTransformerInterface { - public const MINIMUM = ProjectDeadline::Minimum->value; - public const OPTIMUM = ProjectDeadline::Optimum->value; - public const VALID_SUMMARY_TYPES = [self::MINIMUM, self::OPTIMUM]; - public function __construct( private MoneyService $moneyService, ) {} @@ -31,8 +27,8 @@ class BudgetMapTransformer implements PropertyTransformerInterface $currency = $source->getAccounting()->getCurrency(); $budget = new Budget(); - $budget->minimum = $this->getItemsSummary($items, $currency, self::MINIMUM); - $budget->optimum = $this->getItemsSummary($items, $currency, self::OPTIMUM); + $budget->minimum = $this->getItemsSummary($items, $currency, ProjectDeadline::Minimum); + $budget->optimum = $this->getItemsSummary($items, $currency, ProjectDeadline::Optimum); return $budget; } @@ -51,34 +47,33 @@ class BudgetMapTransformer implements PropertyTransformerInterface */ private function calcItemsTotal(array $items, string $currency): array { - $totals = [ - self::MINIMUM => new Money(0, $currency), - self::OPTIMUM => new Money(0, $currency), - ]; + $totalMinimum = new Money(0, $currency); + $totalOptimum = new Money(0, $currency); foreach ($items as $item) { $itemMoney = $item->getMoney(); - $itemDeadlineValue = $item->getDeadline()->value; + $itemDeadline = $item->getDeadline(); - if (isset($totals[$itemDeadlineValue])) { - $totals[$itemDeadlineValue] = $this->moneyService->add($itemMoney, $totals[$itemDeadlineValue]); + if ($itemDeadline == ProjectDeadline::Minimum) { + $totalMinimum = $this->moneyService->add($itemMoney, $totalMinimum); } + + // Either minimal or optimum is also added to the totalOptimum + $totalOptimum = $this->moneyService->add($itemMoney, $totalOptimum); } - return $totals; + return [ProjectDeadline::Minimum->value => $totalMinimum, ProjectDeadline::Optimum->value => $totalOptimum]; } - private function getItemsSummary(array $items, string $currency, string $type) + private function getItemsSummary(array $items, string $currency, ProjectDeadline $deadline) { - if (!\in_array($type, self::VALID_SUMMARY_TYPES)) { - throw new \Exception(\sprintf('BudgetSummary must be of \'%s\' type.', \join(', ', self::VALID_SUMMARY_TYPES))); - } + $deadlineValue = $deadline->value; $summary = new BudgetSummary(); - $summary->money = $this->calcItemsTotal($items, $currency)[$type]; - $summary->task = $this->calcItemsTotal($this->filterItemsByType($items, BudgetItemType::Task), $currency)[$type]; - $summary->material = $this->calcItemsTotal($this->filterItemsByType($items, BudgetItemType::Material), $currency)[$type]; - $summary->infra = $this->calcItemsTotal($this->filterItemsByType($items, BudgetItemType::Infrastructure), $currency)[$type]; + $summary->money = $this->calcItemsTotal($items, $currency)[$deadlineValue]; + $summary->task = $this->calcItemsTotal($this->filterItemsByType($items, BudgetItemType::Task), $currency)[$deadlineValue]; + $summary->material = $this->calcItemsTotal($this->filterItemsByType($items, BudgetItemType::Material), $currency)[$deadlineValue]; + $summary->infra = $this->calcItemsTotal($this->filterItemsByType($items, BudgetItemType::Infrastructure), $currency)[$deadlineValue]; return $summary; }