|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Simara\Cart\Domain\Cart; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
6
|
|
|
use Doctrine\Common\Collections\Collection; |
|
7
|
|
|
use Simara\Cart\Domain\Price; |
|
8
|
|
|
use Simara\Cart\Domain\Prices\Prices; |
|
9
|
|
|
|
|
10
|
|
|
final class Cart |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Collection<int, Item> |
|
14
|
|
|
*/ |
|
15
|
|
|
private $items; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(private string $id) |
|
18
|
|
|
{ |
|
19
|
24 |
|
$this->items = new ArrayCollection(); |
|
20
|
|
|
} |
|
21
|
24 |
|
|
|
22
|
24 |
|
public function add(string $productId, int $amount = 1): void |
|
23
|
24 |
|
{ |
|
24
|
|
|
try { |
|
25
|
21 |
|
$item = $this->find($productId); |
|
26
|
|
|
$item->add($amount); |
|
27
|
|
|
} catch (ProductNotInCartException) { |
|
28
|
21 |
|
$this->items->add(new Item($productId, $amount)); |
|
29
|
1 |
|
} |
|
30
|
21 |
|
} |
|
31
|
21 |
|
|
|
32
|
|
|
/** |
|
33
|
21 |
|
* @throws ProductNotInCartException |
|
34
|
|
|
*/ |
|
35
|
|
|
public function remove(string $productId): void |
|
36
|
|
|
{ |
|
37
|
|
|
$key = $this->findKey($productId); |
|
38
|
5 |
|
$this->items->remove($key); |
|
39
|
|
|
} |
|
40
|
5 |
|
|
|
41
|
3 |
|
/** |
|
42
|
3 |
|
* @throws ProductNotInCartException |
|
43
|
|
|
*/ |
|
44
|
|
|
public function changeAmount(string $productId, int $amount): void |
|
45
|
|
|
{ |
|
46
|
|
|
$item = $this->find($productId); |
|
47
|
2 |
|
$item->changeAmount($amount); |
|
48
|
|
|
} |
|
49
|
2 |
|
|
|
50
|
1 |
|
public function calculate(Prices $prices): CartDetail |
|
51
|
1 |
|
{ |
|
52
|
|
|
$detailItems = $this->items->map(fn(Item $item): ItemDetail => $item->toDetail($prices))->getValues(); |
|
53
|
17 |
|
|
|
54
|
|
|
$itemPrices = $this->items->map(fn(Item $item): Price => $item->calculatePrice($prices))->getValues(); |
|
55
|
17 |
|
|
|
56
|
|
|
$totalPrice = Price::sum($itemPrices); |
|
57
|
17 |
|
|
|
58
|
|
|
return new CartDetail($detailItems, $totalPrice); |
|
59
|
17 |
|
} |
|
60
|
|
|
|
|
61
|
17 |
|
/** |
|
62
|
|
|
* @throws ProductNotInCartException |
|
63
|
|
|
*/ |
|
64
|
|
|
private function find(string $productId): Item |
|
65
|
|
|
{ |
|
66
|
|
|
foreach ($this->items as $item) { |
|
67
|
21 |
|
if ($item->getProductId() === $productId) { |
|
68
|
|
|
return $item; |
|
69
|
21 |
|
} |
|
70
|
5 |
|
} |
|
71
|
2 |
|
throw new ProductNotInCartException(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
21 |
|
/** |
|
75
|
|
|
* @throws ProductNotInCartException |
|
76
|
|
|
*/ |
|
77
|
|
|
private function findKey(string $productId): int |
|
78
|
|
|
{ |
|
79
|
|
|
foreach ($this->items as $key => $item) { |
|
80
|
5 |
|
if ($item->getProductId() === $productId) { |
|
81
|
|
|
return $key; |
|
82
|
5 |
|
} |
|
83
|
4 |
|
} |
|
84
|
3 |
|
throw new ProductNotInCartException(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
2 |
|
public function getId(): string |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->id; |
|
90
|
8 |
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|