@@ -46,16 +46,14 @@ public function withMeta(array $meta): self
4646 return $ self ;
4747 }
4848
49- public function withItem (ProductInterface $ product , int $ quantity = 1 , int |string |null $ price = null ): self
49+ public function withItem (ProductInterface $ product , int $ quantity = 1 , int |string |null $ pricePerItem = null ): self
5050 {
5151 $ self = clone $ this ;
5252
5353 $ productId = $ self ->productId ($ product );
54- if (!isset ($ self ->items [$ productId ])) {
55- $ self ->items [$ productId ] = [];
56- }
5754
58- $ self ->items [$ productId ][] = new ItemDto ($ product , $ quantity , $ price );
55+ $ self ->items [$ productId ] ??= [];
56+ $ self ->items [$ productId ][] = new ItemDto ($ product , $ quantity , $ pricePerItem );
5957
6058 return $ self ;
6159 }
@@ -78,7 +76,7 @@ public function getItems(): array
7876 $ items = [];
7977 foreach ($ this ->items as $ item ) {
8078 foreach ($ item as $ datum ) {
81- $ items [] = $ datum ->items ();
79+ $ items [] = $ datum ->getItems ();
8280 }
8381 }
8482
@@ -91,10 +89,12 @@ public function getTotal(Customer $customer): string
9189 $ prices = [];
9290 foreach ($ this ->items as $ productId => $ _items ) {
9391 foreach ($ _items as $ item ) {
94- $ product = $ item ->product ();
95- $ prices [$ productId ] = $ item ->getPrice ()
92+ $ product = $ item ->getProduct ();
93+ $ prices [$ productId ] = $ item ->getPricePerItem ()
9694 ?? $ prices [$ productId ]
97- ?? $ item ->product ()->getAmountProduct ($ customer );
95+ ?? $ item ->getProduct ()
96+ ->getAmountProduct ($ customer )
97+ ;
9898
9999 $ price = $ this ->math ->mul ($ this ->getQuantity ($ product ), $ prices [$ productId ]);
100100 $ result = $ this ->math ->add ($ result , $ price );
@@ -111,7 +111,6 @@ public function count(): int
111111
112112 public function getQuantity (ProductInterface $ product ): int
113113 {
114- /** @var ItemDtoInterface[] $items */
115114 $ quantity = 0 ;
116115 $ items = $ this ->items [$ this ->productId ($ product )] ?? [];
117116 foreach ($ items as $ item ) {
@@ -126,11 +125,13 @@ public function getQuantity(ProductInterface $product): int
126125 */
127126 public function getBasketDto (): BasketDtoInterface
128127 {
129- if ($ this ->items === []) {
128+ $ items = array_merge (...array_values ($ this ->items ));
129+
130+ if ($ items === []) {
130131 throw new CartEmptyException ('Cart is empty ' , ExceptionInterface::CART_EMPTY );
131132 }
132133
133- return new BasketDto (array_merge (... array_values ( $ this -> items )) , $ this ->getMeta ());
134+ return new BasketDto ($ items , $ this ->getMeta ());
134135 }
135136
136137 private function productId (ProductInterface $ product ): string
0 commit comments