@@ -54,11 +54,11 @@ |
||
| 54 | 54 | |
| 55 | 55 | public function calculate(Prices $prices): CartDetail |
| 56 | 56 | { |
| 57 | - $detailItems = $this->items->map(function (Item $item) use ($prices): ItemDetail { |
|
| 57 | + $detailItems = $this->items->map(function(Item $item) use ($prices): ItemDetail { |
|
| 58 | 58 | return $item->toDetail($prices); |
| 59 | 59 | })->getValues(); |
| 60 | 60 | |
| 61 | - $itemPrices = $this->items->map(function (Item $item) use ($prices): Price { |
|
| 61 | + $itemPrices = $this->items->map(function(Item $item) use ($prices): Price { |
|
| 62 | 62 | return $item->calculatePrice($prices); |
| 63 | 63 | })->getValues(); |
| 64 | 64 | |
@@ -5,8 +5,8 @@ |
||
| 5 | 5 | |
| 6 | 6 | interface Prices |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * @throws PriceNotFoundException |
|
| 10 | - */ |
|
| 11 | - public function unitPrice(string $productId): Price; |
|
| 8 | + /** |
|
| 9 | + * @throws PriceNotFoundException |
|
| 10 | + */ |
|
| 11 | + public function unitPrice(string $productId): Price; |
|
| 12 | 12 | } |
@@ -11,41 +11,41 @@ |
||
| 11 | 11 | |
| 12 | 12 | final class CartUseCase |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @var CartRepository |
|
| 16 | - */ |
|
| 17 | - private $repository; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * @var Prices |
|
| 21 | - */ |
|
| 22 | - private $prices; |
|
| 23 | - |
|
| 24 | - public function __construct(CartRepository $repository, Prices $prices) |
|
| 25 | - { |
|
| 26 | - $this->repository = $repository; |
|
| 27 | - $this->prices = $prices; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - public function add(string $cartId, string $productId, int $amount): void |
|
| 31 | - { |
|
| 32 | - $cart = $this->get($cartId); |
|
| 33 | - $cart->add($productId, $amount); |
|
| 34 | - $this->repository->add($cart); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - public function detail(string $cartId): CartDetail |
|
| 38 | - { |
|
| 39 | - return $this->get($cartId)->calculate($this->prices); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - private function get(string $cartId): Cart |
|
| 43 | - { |
|
| 44 | - try { |
|
| 45 | - return $this->repository->get($cartId); |
|
| 46 | - } catch (CartNotFoundException $e) { |
|
| 47 | - return new Cart($cartId); |
|
| 48 | - } |
|
| 49 | - } |
|
| 14 | + /** |
|
| 15 | + * @var CartRepository |
|
| 16 | + */ |
|
| 17 | + private $repository; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * @var Prices |
|
| 21 | + */ |
|
| 22 | + private $prices; |
|
| 23 | + |
|
| 24 | + public function __construct(CartRepository $repository, Prices $prices) |
|
| 25 | + { |
|
| 26 | + $this->repository = $repository; |
|
| 27 | + $this->prices = $prices; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + public function add(string $cartId, string $productId, int $amount): void |
|
| 31 | + { |
|
| 32 | + $cart = $this->get($cartId); |
|
| 33 | + $cart->add($productId, $amount); |
|
| 34 | + $this->repository->add($cart); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + public function detail(string $cartId): CartDetail |
|
| 38 | + { |
|
| 39 | + return $this->get($cartId)->calculate($this->prices); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + private function get(string $cartId): Cart |
|
| 43 | + { |
|
| 44 | + try { |
|
| 45 | + return $this->repository->get($cartId); |
|
| 46 | + } catch (CartNotFoundException $e) { |
|
| 47 | + return new Cart($cartId); |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | 51 | } |
@@ -9,40 +9,40 @@ |
||
| 9 | 9 | |
| 10 | 10 | final class CsvPrices implements Prices |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - private $filename; |
|
| 12 | + /** |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + private $filename; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @var Price[]|array<string, Price> |
|
| 19 | - */ |
|
| 20 | - private $prices = []; |
|
| 17 | + /** |
|
| 18 | + * @var Price[]|array<string, Price> |
|
| 19 | + */ |
|
| 20 | + private $prices = []; |
|
| 21 | 21 | |
| 22 | - public function __construct(string $filename) |
|
| 23 | - { |
|
| 24 | - $this->filename = $filename; |
|
| 25 | - } |
|
| 22 | + public function __construct(string $filename) |
|
| 23 | + { |
|
| 24 | + $this->filename = $filename; |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - public function unitPrice(string $productId): Price |
|
| 28 | - { |
|
| 29 | - $this->loadPrices(); |
|
| 30 | - if (!isset ($this->prices[$productId])) { |
|
| 31 | - throw new PriceNotFoundException(); |
|
| 32 | - } |
|
| 33 | - return $this->prices[$productId]; |
|
| 34 | - } |
|
| 27 | + public function unitPrice(string $productId): Price |
|
| 28 | + { |
|
| 29 | + $this->loadPrices(); |
|
| 30 | + if (!isset ($this->prices[$productId])) { |
|
| 31 | + throw new PriceNotFoundException(); |
|
| 32 | + } |
|
| 33 | + return $this->prices[$productId]; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - private function loadPrices() |
|
| 37 | - { |
|
| 38 | - if ($this->prices === []) { |
|
| 39 | - $handle = \fopen($this->filename, 'r'); |
|
| 40 | - while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { |
|
| 41 | - $id = $data[0]; |
|
| 42 | - $price = new Price($data[1]); |
|
| 43 | - $this->prices[$id] = $price; |
|
| 44 | - } |
|
| 45 | - fclose($handle); |
|
| 46 | - } |
|
| 47 | - } |
|
| 36 | + private function loadPrices() |
|
| 37 | + { |
|
| 38 | + if ($this->prices === []) { |
|
| 39 | + $handle = \fopen($this->filename, 'r'); |
|
| 40 | + while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { |
|
| 41 | + $id = $data[0]; |
|
| 42 | + $price = new Price($data[1]); |
|
| 43 | + $this->prices[$id] = $price; |
|
| 44 | + } |
|
| 45 | + fclose($handle); |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | 48 | } |