| Total Complexity | 8 |
| Total Lines | 59 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 9 | class Item |
||
| 10 | { |
||
| 11 | private $generatedId; |
||
|
|
|||
| 12 | |||
| 13 | private string $productId; |
||
| 14 | |||
| 15 | private int $amount; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @throws AmountMustBePositiveException |
||
| 19 | */ |
||
| 20 | public function __construct(string $productId, int $amount) |
||
| 21 | { |
||
| 22 | $this->checkAmount($amount); |
||
| 23 | $this->productId = $productId; |
||
| 24 | $this->amount = $amount; |
||
| 25 | } |
||
| 26 | |||
| 27 | public function toDetail(Prices $prices): ItemDetail |
||
| 28 | { |
||
| 29 | return new ItemDetail($this->productId, $prices->unitPrice($this->productId), $this->amount); |
||
| 30 | } |
||
| 31 | |||
| 32 | public function getProductId(): string |
||
| 33 | { |
||
| 34 | return $this->productId; |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @throws AmountMustBePositiveException |
||
| 39 | */ |
||
| 40 | public function add(int $amount): void |
||
| 41 | { |
||
| 42 | $this->checkAmount($amount); |
||
| 43 | $this->amount = $this->amount + $amount; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @throws AmountMustBePositiveException |
||
| 48 | */ |
||
| 49 | private function checkAmount(int $amount): void |
||
| 50 | { |
||
| 51 | if ($amount <= 0) { |
||
| 52 | throw new AmountMustBePositiveException(); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @throws AmountMustBePositiveException |
||
| 58 | */ |
||
| 59 | public function changeAmount(int $amount): void |
||
| 63 | } |
||
| 64 | |||
| 65 | public function calculatePrice(Prices $prices): Price |
||
| 66 | { |
||
| 67 | return $prices->unitPrice($this->productId)->multiply($this->amount); |
||
| 68 | } |
||
| 69 | } |
||
| 70 |