-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathSessionDriver.php
132 lines (112 loc) · 3.11 KB
/
SessionDriver.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
<?php
namespace Freshbitsweb\LaravelCartManager\Drivers;
use Freshbitsweb\LaravelCartManager\Contracts\Cart;
use Freshbitsweb\LaravelCartManager\Contracts\CartDriver;
class SessionDriver implements CartDriver
{
public $sessionKey = 'lcm_cart';
public $itemsKey = 'lcm_cart.items';
/**
* Returns current cart data.
*
* @return array Cart data (including items)
*/
public function getCartData()
{
return session($this->sessionKey, ['items' => []]);
}
/**
* Stores the cart and cart items data.
*
* @param array Cart data (including items)
* @return void
*/
public function storeNewCartData($cartData)
{
// Set id of the item to be used for updates later
foreach ($cartData['items'] as $key => $item) {
$cartData['items'][$key]['id'] = $key + 1;
}
// We use static number 1 as the cart id
session([
$this->sessionKey => array_merge($cartData, ['id' => 1]),
]);
}
/**
* Updates the cart record with the new data.
*
* @param int Id of the cart
* @param array Cart data (without items)
* @return void
*/
public function updateCart($cartId, $cartData)
{
$items = session($this->itemsKey);
session([
$this->sessionKey => array_merge($cartData, ['items' => $items]),
]);
}
/**
* Adds a new cart item to the cart.
*
* @param int Cart id
* @param array Cart item data
* @return void
*/
public function addCartItem($cartId, $cartItem)
{
$cartItem['id'] = count(session($this->itemsKey)) + 1;
session()->push($this->itemsKey, $cartItem);
}
/**
* Removes a cart item from the cart.
*
* @param int Cart item id
* @return void
*/
public function removeCartItem($cartItemId)
{
$items = collect(session($this->itemsKey));
$items = $items->reject(function ($item) use ($cartItemId) {
return $item['id'] == $cartItemId;
});
session([$this->itemsKey => $items]);
}
/**
* Updates the quantity of the cart item.
*
* @param int Id of the cart item
* @param int New quantity to be set
* @return void
*/
public function setCartItemQuantity($cartItemId, $newQuantity)
{
$items = collect(session($this->itemsKey));
$items = $items->map(function ($item) use ($cartItemId, $newQuantity) {
if ($item['id'] == $cartItemId) {
$item['quantity'] = $newQuantity;
}
return $item;
});
session([$this->itemsKey => $items]);
}
/**
* Updates the details of all the cart items.
*
* @param \Illuminate\Support\Collection Cart items
* @return void
*/
public function updateItemsData($items)
{
session([$this->itemsKey => $items->toArray()]);
}
/**
* Clears all the cart details including cart items.
*
* @return void
*/
public function clearData()
{
session()->forget($this->sessionKey);
}
}