-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrderUseCases.php
56 lines (50 loc) · 1.56 KB
/
OrderUseCases.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace Src\Domain\Services;
use Src\Domain\Entities\Merchant;
use Src\Domain\Entities\Order;
use Src\Domain\Entities\StockItem;
use Src\Domain\Types\OrderStatus;
class OrderUseCases
{
/**
* @param Merchant $merchant
* @param Order $order
* @param StockItem[] $stockItems
* @return bool
*/
public function confirmOrder(Merchant $merchant, Order $order, array $stockItems): bool
{
$status = $this->hasEnoughStock($order, $stockItems);
if (!$status) {
$order->setStatus(OrderStatus::CANCELLED);
return false;
}
$orderIngredients = $order->getIngredients();
foreach ($stockItems as $stock) {
$statusBeforeConsuming = $stock->getStockStatus();
$stock->consume($orderIngredients[$stock->getId()], $order->getId());
if ($stock->getStockStatus() != $statusBeforeConsuming) {
$merchant->notifyAboutStock($stock);
}
}
$order->setStatus(OrderStatus::CONFIRMED);
return true;
}
/**
* @param Order $order,
* @param StockItem[] $stockItems
*/
private function hasEnoughStock(Order $order, array $stockItems): bool
{
$status = true;
$orderIngredients = $order->getIngredients();
foreach ($stockItems as $stock) {
$canConsume = $stock->canConsume($orderIngredients[$stock->getId()]);
if (!$canConsume) {
$status = false;
break;
}
}
return $status;
}
}