-
Notifications
You must be signed in to change notification settings - Fork 17
/
CartManager.php
290 lines (236 loc) · 7.05 KB
/
CartManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
declare(strict_types=1);
/**
* Contains the CartManager class.
*
* @copyright Copyright (c) 2017 Attila Fulop
* @author Attila Fulop
* @license MIT
* @since 2017-10-29
*
*/
namespace Vanilo\Cart;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Event;
use Vanilo\Cart\Contracts\Cart as CartContract;
use Vanilo\Cart\Contracts\CartItem;
use Vanilo\Cart\Contracts\CartManager as CartManagerContract;
use Vanilo\Cart\Events\CartCreated;
use Vanilo\Cart\Events\CartDeleted;
use Vanilo\Cart\Events\CartDeleting;
use Vanilo\Cart\Events\CartUpdated;
use Vanilo\Cart\Exceptions\InvalidCartConfigurationException;
use Vanilo\Cart\Models\Cart;
use Vanilo\Cart\Models\CartProxy;
use Vanilo\Contracts\Buyable;
class CartManager implements CartManagerContract
{
public const CONFIG_SESSION_KEY = 'vanilo.cart.session_key';
/** The key in session that holds the cart id */
protected ?string $sessionKey;
protected null|Cart|CartContract $cart = null;
public function __construct()
{
$this->sessionKey = config(self::CONFIG_SESSION_KEY);
if (empty($this->sessionKey)) {
throw new InvalidCartConfigurationException(
sprintf(
'Cart session key (`%s`) is empty. Please provide a valid value.',
self::CONFIG_SESSION_KEY
)
);
}
}
public function __call(string $name, array $arguments): mixed
{
if (null !== $this->model()) {
return call_user_func([$this->model(), $name], ...$arguments);
}
return null;
}
public function getItems(): Collection
{
return $this->exists() ? $this->model()->getItems() : collect();
}
public function itemsTotal(): float
{
return $this->exists() ? $this->model()->itemsTotal() : 0;
}
public function addItem(Buyable $product, int|float $qty = 1, array $params = []): CartItem
{
$cart = $this->findOrCreateCart();
$result = $cart->addItem($product, $qty, $params);
$this->triggerCartUpdatedEvent();
return $result;
}
public function removeItem(CartItem $item): void
{
if ($cart = $this->model()) {
$cart->removeItem($item);
$this->triggerCartUpdatedEvent();
}
}
public function removeProduct(Buyable $product): void
{
if ($cart = $this->model()) {
$cart->removeProduct($product);
$this->triggerCartUpdatedEvent();
}
}
public function clear(): void
{
if ($cart = $this->model()) {
$cart->clear();
$this->triggerCartUpdatedEvent();
}
}
public function itemCount(): int
{
return $this->exists() ? $this->model()->itemCount() : 0;
}
public function total(): float
{
return $this->exists() ? $this->model()->total() : 0;
}
public function exists(): bool
{
return (bool) $this->getCartId() && $this->model();
}
public function doesNotExist(): bool
{
return !$this->exists();
}
public function model(): ?CartContract
{
$id = $this->getCartId();
if ($id && $this->cart) {
return $this->cart;
} elseif ($id) {
$this->cart = CartProxy::find($id);
return $this->cart;
}
return null;
}
public function isEmpty(): bool
{
return 0 == $this->itemCount();
}
public function isNotEmpty(): bool
{
return !$this->isEmpty();
}
public function destroy(): void
{
if ($this->exists()) {
Event::dispatch(new CartDeleting($this->model()));
}
// don't call $this->clear() as the triggered CartUpdated event may cause unwanted side effects
$this->model()->clear();
$this->model()->delete();
$this->forget();
Event::dispatch(new CartDeleted());
}
public function create(bool $forceCreateIfExists = false): void
{
if ($this->exists() && !$forceCreateIfExists) {
return;
}
$this->createCart();
}
public function getUser(): ?Authenticatable
{
return $this->exists() ? $this->model()->getUser() : null;
}
public function setUser(Authenticatable|int|string|null $user): void
{
if ($this->exists()) {
$this->cart->setUser($user);
$this->cart->save();
$this->cart->load('user');
}
}
public function removeUser(): void
{
$this->setUser(null);
}
public function restoreLastActiveCart($user): void
{
$lastActiveCart = CartProxy::ofUser($user)->actives()->latest()->first();
if ($lastActiveCart) {
$this->setCartModel($lastActiveCart);
$this->triggerCartUpdatedEvent();
}
}
public function mergeLastActiveCartWithSessionCart($user): void
{
/** @var Cart $lastActiveCart */
if ($lastActiveCart = CartProxy::ofUser($user)->actives()->latest()->first()) {
/** @var CartItem $item */
foreach ($lastActiveCart->getItems() as $item) {
$this->addItem($item->getBuyable(), $item->getQuantity());
}
$lastActiveCart->delete();
$this->triggerCartUpdatedEvent();
}
}
public function forget(): void
{
$this->cart = null;
session()->forget($this->sessionKey);
}
/**
* Refreshes the underlying cart model from the database
*/
public function fresh(): self
{
if (null !== $this->cart) {
$this->cart = $this->cart->fresh();
}
return $this;
}
/**
* Returns the model id of the cart for the current session
* or null if it does not exist
*
* @return int|null
*/
protected function getCartId()
{
return session($this->sessionKey);
}
/**
* Returns the cart model for the current session by either fetching it or creating one
*/
protected function findOrCreateCart(): Cart|CartContract
{
return $this->model() ?: $this->createCart();
}
/**
* Creates a new cart model and saves its id in the session
*/
protected function createCart(): Cart|CartContract
{
if (config('vanilo.cart.auto_assign_user') && Auth::check()) {
$attributes = [
'user_id' => Auth::user()->id
];
}
$model = $this->setCartModel(CartProxy::create($attributes ?? []));
Event::dispatch(new CartCreated($model));
return $model;
}
protected function setCartModel(CartContract $cart): CartContract
{
$this->cart = $cart;
session([$this->sessionKey => $this->cart->id]);
return $this->cart;
}
protected function triggerCartUpdatedEvent(): void
{
if ($this->exists()) {
Event::dispatch(new CartUpdated($this->model()));
}
}
}