Skip to content

Commit ed686d3

Browse files
committed
refactor: improve sumaries
1 parent 0d2d145 commit ed686d3

File tree

4 files changed

+49
-37
lines changed

4 files changed

+49
-37
lines changed

src/Carts/SnapshotCart.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Vaened\SwiftCart\Items\ImmutableCartItem;
1313
use Vaened\SwiftCart\Items\ImmutableCartItems;
1414
use Vaened\SwiftCart\NotFoundItem;
15+
use Vaened\SwiftCart\Summary;
1516

1617
final class SnapshotCart extends SwiftCart
1718
{
@@ -46,6 +47,11 @@ public function pullAll(): void
4647
$this->staging()->combine($immutables);
4748
}
4849

50+
public function summary(): Summary
51+
{
52+
return $this->totalizer()->summary();
53+
}
54+
4955
protected function staging(): ImmutableCartItems
5056
{
5157
return $this->items;

src/Carts/SwiftCart.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@
1212
use Vaened\SwiftCart\Items\CartItem;
1313
use Vaened\SwiftCart\Items\CartItems;
1414
use Vaened\SwiftCart\Summary;
15+
use Vaened\SwiftCart\Totalizer;
1516

1617
abstract class SwiftCart
1718
{
18-
abstract protected function staging(): CartItems;
19+
abstract public function summary(): Summary;
1920

20-
public function summary(): Summary
21-
{
22-
return $this->staging()->summary();
23-
}
21+
abstract protected function staging(): CartItems;
2422

2523
public function locate(Identifiable $identifiable): ?CartItem
2624
{
@@ -43,4 +41,9 @@ public function items(): ArrayList
4341
$this->staging()->items()
4442
);
4543
}
44+
45+
protected function totalizer(): Totalizer
46+
{
47+
return $this->staging()->totalizer();
48+
}
4649
}

src/Items/CartItems.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
use Vaened\Support\Types\InvalidType;
1212
use Vaened\SwiftCart\AlreadyAttachedItem;
1313
use Vaened\SwiftCart\Entities\Identifiable;
14-
use Vaened\SwiftCart\Summary;
15-
use Vaened\SwiftCart\TotalSummaries;
14+
use Vaened\SwiftCart\Totalizer;
1615

1716
use function array_map;
1817
use function Lambdish\Phunctional\each;
@@ -34,13 +33,6 @@ public function ids(): array
3433
return $this->map(static fn(CartItem $item) => $item->uniqueId());
3534
}
3635

37-
public function summary(): Summary
38-
{
39-
return TotalSummaries::of(
40-
$this->map($this->summaries())
41-
)->summary();
42-
}
43-
4436
public function has(Identifiable $identifiable): bool
4537
{
4638
return isset($this->items[$identifiable->uniqueId()]);
@@ -59,6 +51,13 @@ public function map(callable $callback): array
5951
return array_map($callback, $this->items);
6052
}
6153

54+
public function totalizer(): Totalizer
55+
{
56+
return Totalizer::of(
57+
$this->map($this->summaries())
58+
);
59+
}
60+
6261
protected function attach(CartItem $item): void
6362
{
6463
$this->ensureNotAddedBefore($item);

src/TotalSummaries.php renamed to src/Totalizer.php

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,62 +11,66 @@
1111
use Vaened\PriceEngine\TotalSummary;
1212
use Vaened\Support\Types\ArrayObject;
1313

14-
final class TotalSummaries extends ArrayObject
14+
final class Totalizer extends ArrayObject
1515
{
1616
public static function of(array $summaries): self
1717
{
1818
return new self($summaries);
1919
}
2020

21-
public function subtotal(): Money
21+
public function summary(Money $additionalCharges = null, Money $additionalDiscounts = null): Summary
2222
{
23-
return $this->sum(
24-
static fn(TotalSummary $summary) => $summary->subtotal()
23+
$subtotal = $this->subtotal();
24+
25+
return new Summary(
26+
subtotal : $subtotal,
27+
totalTaxes : $this->totalTaxes(),
28+
totalCharges : $this->totalCharges()
29+
->plus(null === $additionalCharges ? 0 : $additionalCharges),
30+
totalDiscounts: $this->totalDiscounts()
31+
->plus(null === $additionalDiscounts ? 0 : $additionalDiscounts),
32+
total : $this->total(),
2533
);
2634
}
2735

28-
public function totalTaxes(): Money
36+
public function total(): Money
2937
{
3038
return $this->sum(
31-
static fn(TotalSummary $summary) => $summary->taxes()->total()
39+
static fn(TotalSummary $summary) => $summary->total()
3240
);
3341
}
3442

35-
public function totalCharges(): Money
43+
protected function type(): string
3644
{
37-
return $this->sum(
38-
static fn(TotalSummary $summary) => $summary->charges()->total()
39-
);
45+
return TotalSummary::class;
4046
}
4147

42-
public function totalDiscounts(): Money
48+
private function subtotal(): Money
4349
{
4450
return $this->sum(
45-
static fn(TotalSummary $summary) => $summary->discounts()->total()
51+
static fn(TotalSummary $summary) => $summary->subtotal()->gross()
4652
);
4753
}
4854

49-
public function total(): Money
55+
private function totalTaxes(): Money
5056
{
5157
return $this->sum(
52-
static fn(TotalSummary $summary) => $summary->total()
58+
static fn(TotalSummary $summary) => $summary->taxes()->total()
5359
);
5460
}
5561

56-
public function summary(): Summary
62+
private function totalCharges(): Money
5763
{
58-
return new Summary(
59-
subtotal : $this->subtotal(),
60-
totalTaxes : $this->totalTaxes(),
61-
totalCharges : $this->totalCharges(),
62-
totalDiscounts: $this->totalDiscounts(),
63-
total : $this->total(),
64+
return $this->sum(
65+
static fn(TotalSummary $summary) => $summary->charges()->total()
6466
);
6567
}
6668

67-
protected function type(): string
69+
private function totalDiscounts(): Money
6870
{
69-
return TotalSummary::class;
71+
return $this->sum(
72+
static fn(TotalSummary $summary) => $summary->discounts()->total()
73+
);
7074
}
7175

7276
private function sum(callable $callback): Money

0 commit comments

Comments
 (0)