1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lenius\LaravelEcommerce; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
6
|
|
|
use Lenius\Basket\Basket; |
7
|
|
|
use Lenius\Basket\IdentifierInterface; |
8
|
|
|
use Lenius\Basket\Item; |
9
|
|
|
use Lenius\Basket\ItemInterface; |
10
|
|
|
use Lenius\Basket\StorageInterface; |
11
|
|
|
use Lenius\LaravelEcommerce\Events\CartDestroyed; |
12
|
|
|
use Lenius\LaravelEcommerce\Events\CartItemDecreased; |
13
|
|
|
use Lenius\LaravelEcommerce\Events\CartItemIncremented; |
14
|
|
|
use Lenius\LaravelEcommerce\Events\CartItemRemoved; |
15
|
|
|
use Lenius\LaravelEcommerce\Events\CartItemUpdated; |
16
|
|
|
|
17
|
|
|
class Cart extends Basket |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Instance of the event dispatcher. |
21
|
|
|
* |
22
|
|
|
* @var Dispatcher |
23
|
|
|
*/ |
24
|
|
|
private Dispatcher $events; |
25
|
|
|
|
26
|
7 |
|
public function __construct(StorageInterface $store, IdentifierInterface $identifier, Dispatcher $events) |
27
|
|
|
{ |
28
|
7 |
|
$this->events = $events; |
29
|
|
|
|
30
|
7 |
|
parent::__construct($store, $identifier); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Update an item. |
35
|
|
|
* |
36
|
|
|
* @param string $itemIdentifier |
37
|
|
|
* @param mixed $key |
38
|
|
|
* @param mixed $value |
39
|
|
|
*/ |
40
|
|
|
public function update(string $itemIdentifier, $key, $value = null): void |
41
|
|
|
{ |
42
|
|
|
/** @var ItemInterface $item */ |
43
|
|
|
foreach ($this->contents() as $item) { |
44
|
|
|
if ($item->identifier == $itemIdentifier) { |
45
|
|
|
$item->update($key, $value); |
46
|
|
|
$this->events->dispatch(new CartItemUpdated($item)); |
47
|
|
|
|
48
|
|
|
break; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Remove an item from the cart. |
55
|
|
|
* |
56
|
|
|
* @param string $identifier |
57
|
|
|
*/ |
58
|
2 |
|
public function remove(string $identifier): void |
59
|
|
|
{ |
60
|
2 |
|
$this->events->dispatch(new CartItemRemoved($this->item($identifier))); |
61
|
|
|
|
62
|
2 |
|
$this->store->remove($identifier); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Destroy/empty the basket. |
67
|
|
|
*/ |
68
|
4 |
|
public function destroy(): void |
69
|
|
|
{ |
70
|
4 |
|
$this->store->destroy(); |
71
|
|
|
|
72
|
4 |
|
$this->events->dispatch(new CartDestroyed()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Increment an item from the cart. |
77
|
|
|
* |
78
|
|
|
* @param string $itemIdentifier |
79
|
|
|
* |
80
|
|
|
* @return ItemInterface|bool |
81
|
|
|
*/ |
82
|
2 |
|
public function inc(string $itemIdentifier): ItemInterface|bool |
83
|
|
|
{ |
84
|
2 |
|
if ($item = Cart::item($itemIdentifier)) { |
|
|
|
|
85
|
|
|
/** @var ItemInterface $item */ |
86
|
2 |
|
$item->setQuantity($item->getQuantity() + 1); |
87
|
2 |
|
$this->events->dispatch(new CartItemIncremented($item)); |
88
|
|
|
|
89
|
2 |
|
return $item; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Decrement an item from the cart. |
97
|
|
|
* |
98
|
|
|
* @param string $itemIdentifier |
99
|
|
|
* |
100
|
|
|
* @return ItemInterface|bool |
101
|
|
|
*/ |
102
|
2 |
|
public function dec(string $itemIdentifier): ItemInterface|bool |
103
|
|
|
{ |
104
|
2 |
|
if ($item = Cart::item($itemIdentifier)) { |
|
|
|
|
105
|
|
|
/** @var ItemInterface $item */ |
106
|
2 |
|
if ($item->getQuantity() > 1) { |
107
|
1 |
|
$item->setQuantity($item->getQuantity() - 1); |
108
|
1 |
|
$this->events->dispatch(new CartItemDecreased($item)); |
109
|
1 |
|
return $item; |
110
|
|
|
} else { |
111
|
2 |
|
$this->events->dispatch(new CartItemRemoved($item)); |
112
|
2 |
|
Cart::remove($itemIdentifier); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
return false; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|