|
7 | 7 |
|
8 | 8 | namespace Vaened\SwiftCart\Carts;
|
9 | 9 |
|
10 |
| -use Vaened\PriceEngine\AdjustmentManager; |
11 |
| -use Vaened\SwiftCart\Entities\Identifiable; |
12 |
| -use Vaened\SwiftCart\Entities\TradedCommercialTransaction; |
13 |
| -use Vaened\SwiftCart\Items\ImmutableCartItem; |
| 10 | +use Vaened\PriceEngine\Adjustments\Tax\Taxes; |
| 11 | +use Vaened\SwiftCart\Entities\RegisteredCommercialTransaction; |
| 12 | +use Vaened\SwiftCart\Items\CommerceableCartItem; |
14 | 13 | use Vaened\SwiftCart\Items\ImmutableCartItems;
|
15 |
| -use Vaened\SwiftCart\NotFoundItem; |
16 |
| -use Vaened\SwiftCart\Summary; |
17 | 14 |
|
18 |
| -final class SnapshotCart extends SwiftCart |
| 15 | +final class SnapshotCart extends ShoppingCart |
19 | 16 | {
|
20 |
| - private readonly ImmutableCartItems $immutables; |
21 |
| - |
22 |
| - private readonly ImmutableCartItems $items; |
23 |
| - |
24 |
| - private readonly AdjustmentManager $charges; |
25 |
| - |
26 |
| - private readonly AdjustmentManager $discounts; |
27 |
| - |
28 |
| - public function __construct(TradedCommercialTransaction $transaction) |
29 |
| - { |
30 |
| - $this->immutables = $transaction->items()->toImmutables(); |
31 |
| - $this->items = new ImmutableCartItems([]); |
32 |
| - $this->charges = $this->createManagerOf($transaction->charges()); |
33 |
| - $this->discounts = $this->createManagerOf($transaction->discounts()); |
34 |
| - } |
35 |
| - |
36 |
| - public function locate(Identifiable $identifiable): ?ImmutableCartItem |
37 |
| - { |
38 |
| - return $this->staging()->locate($identifiable); |
39 |
| - } |
40 |
| - |
41 |
| - public function pull(Identifiable $item): ImmutableCartItem |
42 |
| - { |
43 |
| - $immutableItem = $this->immutables->locate($item); |
44 |
| - $this->ensureValidItem($immutableItem); |
45 |
| - |
46 |
| - $this->staging()->push($immutableItem); |
47 |
| - |
48 |
| - return $immutableItem; |
49 |
| - } |
50 |
| - |
51 |
| - public function pullAll(): void |
52 |
| - { |
53 |
| - $immutables = $this->immutables->except($this->staging()); |
54 |
| - $this->staging()->combine($immutables); |
55 |
| - } |
56 |
| - |
57 |
| - public function summary(): Summary |
58 |
| - { |
59 |
| - return $this->totalizer()->summary(); |
60 |
| - } |
61 |
| - |
62 |
| - protected function staging(): ImmutableCartItems |
63 |
| - { |
64 |
| - return $this->items; |
65 |
| - } |
66 |
| - |
67 |
| - protected function globalChargesManager(): AdjustmentManager |
| 17 | + public function __construct(RegisteredCommercialTransaction $transaction, Taxes $taxes = new Taxes([])) |
68 | 18 | {
|
69 |
| - return $this->charges; |
| 19 | + parent::__construct($taxes); |
| 20 | + $this->pullAll($transaction->items()->toImmutables()); |
| 21 | + $this->addAsGlobal(...$transaction->charges()->items()); |
| 22 | + $this->applyAsGlobal(...$transaction->discounts()->items()); |
70 | 23 | }
|
71 | 24 |
|
72 |
| - protected function globalDiscountsManager(): AdjustmentManager |
| 25 | + private function pullAll(ImmutableCartItems $items): void |
73 | 26 | {
|
74 |
| - return $this->discounts; |
| 27 | + $items->toCommerceables() |
| 28 | + ->each($this->attachAll()); |
75 | 29 | }
|
76 | 30 |
|
77 |
| - private function ensureValidItem(?ImmutableCartItem $item): void |
| 31 | + private function attachAll(): callable |
78 | 32 | {
|
79 |
| - if (null === $item) { |
80 |
| - throw new NotFoundItem(); |
81 |
| - } |
| 33 | + return fn(CommerceableCartItem $commerceable) => $this->attach($commerceable); |
82 | 34 | }
|
83 | 35 | }
|
0 commit comments