|
1
|
|
|
<?php namespace Bedard\Shop\Repositories; |
|
2
|
|
|
|
|
3
|
|
|
use Cookie; |
|
4
|
|
|
use Session; |
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Bedard\Shop\Models\Cart; |
|
7
|
|
|
use Bedard\Shop\Models\CartItem; |
|
8
|
|
|
use Bedard\Shop\Models\Inventory; |
|
9
|
|
|
use Bedard\Shop\Models\CartSettings; |
|
10
|
|
|
|
|
11
|
|
|
class CartRepository |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string Cart persistence key. |
|
15
|
|
|
*/ |
|
16
|
|
|
const CART_KEY = 'bedard_shop_cart'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var \Bedard\Shop\Models\Cart |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $cart = null; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Add an item to the curent cart. |
|
25
|
|
|
* |
|
26
|
|
|
* @param int |
|
27
|
|
|
* @param int |
|
28
|
|
|
* @return \Bedard\Shop\Models\Cart |
|
29
|
|
|
*/ |
|
30
|
|
|
public function addInventory($inventoryId, $quantity) |
|
31
|
|
|
{ |
|
32
|
|
|
$inventory = Inventory::findOrFail($inventoryId); |
|
33
|
|
|
|
|
34
|
|
|
$cart = $this->getCart(); |
|
35
|
|
|
$item = CartItem::firstOrCreate([ |
|
36
|
|
|
'cart_id' => $cart->id, |
|
37
|
|
|
'inventory_id' => $inventory->id, |
|
38
|
|
|
]); |
|
39
|
|
|
|
|
40
|
|
|
$item->quantity += $quantity; |
|
41
|
|
|
if ($item->quantity > $inventory->quantity) { |
|
42
|
|
|
$item->quantity = $inventory->quantity; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$item->save(); |
|
46
|
|
|
|
|
47
|
|
|
$item->load([ |
|
48
|
|
|
'inventory.optionValues.option', |
|
49
|
|
|
'inventory.product' => function ($product) { |
|
50
|
|
|
return $product->joinPrice()->with('thumbnails'); |
|
51
|
|
|
}, |
|
52
|
|
|
]); |
|
53
|
|
|
|
|
54
|
|
|
return $item; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Apply a promotion to the cart. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $code |
|
61
|
|
|
* @return \Bedard\Shop\Models\Cart |
|
62
|
|
|
*/ |
|
63
|
|
|
public function applyPromotion($code) |
|
64
|
|
|
{ |
|
65
|
|
|
$cart = $this->getCart(); |
|
66
|
|
|
|
|
67
|
|
|
$cart->applyPromotion($code); |
|
68
|
|
|
|
|
69
|
|
|
return $this->loadCart(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Create a new cart. |
|
74
|
|
|
* |
|
75
|
|
|
* @return \Bedard\Shop\Models\Cart |
|
76
|
|
|
*/ |
|
77
|
|
|
public function create() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->cart = Cart::create(); |
|
80
|
|
|
|
|
81
|
|
|
Session::put(self::CART_KEY, $this->cart->token); |
|
82
|
|
|
Cookie::queue(self::CART_KEY, $this->cart->token, CartSettings::getLifespan()); |
|
83
|
|
|
|
|
84
|
|
|
return $this->cart; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Delete an inventory from the cart. |
|
89
|
|
|
* |
|
90
|
|
|
* @param int |
|
91
|
|
|
* @return \Bedard\Shop\Models\Cart |
|
92
|
|
|
*/ |
|
93
|
|
|
public function deleteItem($inventoryId) |
|
94
|
|
|
{ |
|
95
|
|
|
$cart = $this->getCart(); |
|
96
|
|
|
|
|
97
|
|
|
$item = $cart->items()->whereInventoryId($inventoryId)->first(); |
|
98
|
|
|
|
|
99
|
|
|
if ($item) { |
|
100
|
|
|
return $item->delete(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return false; |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Determine if a cart exists. |
|
108
|
|
|
* |
|
109
|
|
|
* @return bool |
|
110
|
|
|
*/ |
|
111
|
|
|
public function exists() |
|
112
|
|
|
{ |
|
113
|
|
|
$token = $this->getToken(); |
|
114
|
|
|
|
|
115
|
|
|
return Cart::whereToken($token)->isOpen()->exists(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get the current cart, or create one if none exists. |
|
120
|
|
|
* |
|
121
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
|
122
|
|
|
* @return \Bedard\Shop\Models\Cart |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getCart() |
|
125
|
|
|
{ |
|
126
|
|
|
if ($this->cart !== null) { |
|
127
|
|
|
return $this->cart; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$token = $this->getToken(); |
|
131
|
|
|
|
|
132
|
|
|
if (! $token) { |
|
133
|
|
|
return $this->create(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
try { |
|
137
|
|
|
$this->cart = Cart::whereToken($token) |
|
138
|
|
|
->isOpen() |
|
139
|
|
|
->firstOrFail(); |
|
140
|
|
|
} catch (Exception $e) { |
|
141
|
|
|
$this->cart = $this->create(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $this->cart; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Get the cart token. |
|
149
|
|
|
* |
|
150
|
|
|
* @return string |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getToken() |
|
153
|
|
|
{ |
|
154
|
|
|
$token = Session::get(self::CART_KEY); |
|
155
|
|
|
if (! $token && Cookie::has(self::CART_KEY)) { |
|
156
|
|
|
$token = Cookie::get(self::CART_KEY); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $token; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Load the related cart data. |
|
164
|
|
|
* |
|
165
|
|
|
* @return \Bedard\Shop\Models\Cart |
|
166
|
|
|
*/ |
|
167
|
|
|
public function loadCart() |
|
168
|
|
|
{ |
|
169
|
|
|
$this->getCart()->load([ |
|
170
|
|
|
'items.inventory.product' => function ($product) { |
|
171
|
|
|
$product->joinPrice()->with('thumbnails'); |
|
172
|
|
|
}, |
|
173
|
|
|
'items.inventory.optionValues.option', |
|
174
|
|
|
'promotion', |
|
175
|
|
|
]); |
|
176
|
|
|
|
|
177
|
|
|
return $this->cart; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Set an item's quantity in the current cart. |
|
182
|
|
|
* |
|
183
|
|
|
* @param int |
|
184
|
|
|
* @param int |
|
185
|
|
|
* @return \Bedard\Shop\Models\Cart |
|
186
|
|
|
*/ |
|
187
|
|
|
public function setInventory($inventoryId, $quantity) |
|
188
|
|
|
{ |
|
189
|
|
|
$inventory = Inventory::findOrFail($inventoryId); |
|
190
|
|
|
|
|
191
|
|
|
if ($quantity <= 0) { |
|
192
|
|
|
return $this->deleteItem($inventory->id); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$cart = $this->getCart(); |
|
196
|
|
|
$item = CartItem::firstOrCreate([ |
|
197
|
|
|
'cart_id' => $cart->id, |
|
198
|
|
|
'inventory_id' => $inventory->id, |
|
199
|
|
|
]); |
|
200
|
|
|
|
|
201
|
|
|
$item->quantity = $quantity; |
|
202
|
|
|
|
|
203
|
|
|
return $item->save(); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Update multiple inventories. |
|
208
|
|
|
* |
|
209
|
|
|
* @param array $inventories |
|
210
|
|
|
* @return void |
|
211
|
|
|
*/ |
|
212
|
|
|
public function updateInventories(array $inventories) |
|
213
|
|
|
{ |
|
214
|
|
|
foreach ($inventories as $inventoryId => $quantity) { |
|
215
|
|
|
$this->setInventory($inventoryId, $quantity); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Update a single item quantity. |
|
221
|
|
|
* |
|
222
|
|
|
* @param int $itemId |
|
223
|
|
|
* @param int $quantity |
|
224
|
|
|
* @return \Bedard\Shop\Models\CartItem |
|
225
|
|
|
*/ |
|
226
|
|
|
public function updateItem($itemId, $quantity) |
|
227
|
|
|
{ |
|
228
|
|
|
$cart = $this->getCart(); |
|
229
|
|
|
|
|
230
|
|
|
$item = $cart->items()->find($itemId); |
|
231
|
|
|
|
|
232
|
|
|
if ($item) { |
|
233
|
|
|
$item->quantity = $quantity; |
|
234
|
|
|
$item->save(); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
return $item; |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.