Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
13 / 13 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| CartTotals | |
100.00% |
13 / 13 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| grandTotal | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| toArray | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Services\Cart; |
| 4 | |
| 5 | /** |
| 6 | * One immutable snapshot of what a cart costs. Produced by CartService and |
| 7 | * reused by the cart drawer, checkout, order creation and the API so the |
| 8 | * numbers can never drift between them. |
| 9 | */ |
| 10 | class CartTotals |
| 11 | { |
| 12 | public function __construct( |
| 13 | public readonly float $subtotal = 0, |
| 14 | public readonly float $discount = 0, |
| 15 | public readonly float $tax = 0, |
| 16 | public readonly float $shipping = 0, |
| 17 | public readonly int $itemCount = 0, |
| 18 | public readonly ?string $couponCode = null, |
| 19 | public readonly bool $taxInclusive = false, |
| 20 | ) {} |
| 21 | |
| 22 | public function grandTotal(): float |
| 23 | { |
| 24 | // exclusive tax is added on top; inclusive tax is already in the subtotal |
| 25 | $base = $this->subtotal - $this->discount + $this->shipping; |
| 26 | |
| 27 | return round($this->taxInclusive ? $base : $base + $this->tax, 2); |
| 28 | } |
| 29 | |
| 30 | public function toArray(): array |
| 31 | { |
| 32 | return [ |
| 33 | 'subtotal' => round($this->subtotal, 2), |
| 34 | 'discount' => round($this->discount, 2), |
| 35 | 'tax' => round($this->tax, 2), |
| 36 | 'shipping' => round($this->shipping, 2), |
| 37 | 'grand_total' => $this->grandTotal(), |
| 38 | 'item_count' => $this->itemCount, |
| 39 | 'coupon_code' => $this->couponCode, |
| 40 | 'tax_inclusive' => $this->taxInclusive, |
| 41 | ]; |
| 42 | } |
| 43 | } |