simara-svatopluk /
cart
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Simara\Cart\Domain\Cart; |
||
| 4 | |||
| 5 | use Simara\Cart\Domain\Price; |
||
| 6 | use Simara\Cart\Domain\Prices\Prices; |
||
| 7 | |||
| 8 | final class Item |
||
| 9 | { |
||
| 10 | private int $generatedId; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 11 | |||
| 12 | /** |
||
| 13 | * @throws AmountMustBePositiveException |
||
| 14 | */ |
||
| 15 | public function __construct( |
||
| 16 | private string $productId, |
||
| 17 | private int $amount |
||
| 18 | ) { |
||
| 19 | 31 | $this->checkAmount($amount); |
|
| 20 | } |
||
| 21 | 31 | ||
| 22 | 29 | public function toDetail(Prices $prices): ItemDetail |
|
| 23 | 29 | { |
|
| 24 | 29 | return new ItemDetail($this->productId, $prices->unitPrice($this->productId), $this->amount); |
|
| 25 | } |
||
| 26 | 17 | ||
| 27 | public function getProductId(): string |
||
| 28 | 17 | { |
|
| 29 | return $this->productId; |
||
| 30 | } |
||
| 31 | 8 | ||
| 32 | /** |
||
| 33 | 8 | * @throws AmountMustBePositiveException |
|
| 34 | */ |
||
| 35 | public function add(int $amount): void |
||
| 36 | { |
||
| 37 | $this->checkAmount($amount); |
||
| 38 | $this->amount = $this->amount + $amount; |
||
| 39 | 4 | } |
|
| 40 | |||
| 41 | 4 | /** |
|
| 42 | 2 | * @throws AmountMustBePositiveException |
|
| 43 | 2 | */ |
|
| 44 | private function checkAmount(int $amount): void |
||
| 45 | { |
||
| 46 | if ($amount <= 0) { |
||
| 47 | throw new AmountMustBePositiveException(); |
||
| 48 | 31 | } |
|
| 49 | } |
||
| 50 | 31 | ||
| 51 | 6 | /** |
|
| 52 | * @throws AmountMustBePositiveException |
||
| 53 | 29 | */ |
|
| 54 | public function changeAmount(int $amount): void |
||
| 55 | { |
||
| 56 | $this->checkAmount($amount); |
||
| 57 | $this->amount = $amount; |
||
| 58 | 4 | } |
|
| 59 | |||
| 60 | 4 | public function calculatePrice(Prices $prices): Price |
|
| 61 | 2 | { |
|
| 62 | 2 | return $prices->unitPrice($this->productId)->multiply($this->amount); |
|
| 63 | } |
||
| 64 | } |
||
| 65 |