Skip to content

Commit b0d7559

Browse files
committed
refactor
1 parent 6b238ec commit b0d7559

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

src/CartProvider.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public function register()
1919
public function boot(){
2020
$this->publishes([
2121
__DIR__."/Config/cart.php"=>config_path("cart.php")
22-
],"config");
23-
22+
],"cart-config");
2423
}
2524

2625
}

src/CartService.php

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
class CartService
99
{
1010
/**
11+
* the cart items
1112
* @var Collection
1213
*/
1314
protected $cart;
@@ -19,9 +20,12 @@ class CartService
1920
protected $instanceName="cart";
2021

2122
/**
23+
* the cart session
2224
* @var
2325
*/
2426
protected $session;
27+
28+
2529
public function __construct($instanceName,$session)
2630
{
2731
$this->instanceName=$instanceName;
@@ -33,27 +37,24 @@ public function __construct($instanceName,$session)
3337
/**
3438
* put data in cart session
3539
*
36-
* @param array $value
40+
* @param array $item
3741
* @param null $model
3842
* @return $this
3943
*/
40-
public function put(array $value, $model): CartService
44+
public function put(array $item, $model): CartService
4145
{
4246
if ($this->has($model)) {
4347
return $this->update($this->get($model, false)["quantity"] + 1, $model);
44-
45-
} else {
46-
$value = [
47-
"id" => $value["id"]??Str::random(10),
48-
"price" => $value["price"] ?? 0,
49-
"quantity" => $value["quantity"] ?? 0,
50-
"cartable_id" => $model->id,
51-
"cartable_type" => get_class($model)
52-
];
5348
}
54-
55-
$this->cart->put($value["id"],$value);
56-
$this->session->put([$this->instanceName=>$this->cart]);
49+
$item = [
50+
"id" => $item["id"]??Str::random(10),
51+
"price" => $item["price"] ?? 0,
52+
"quantity" => $item["quantity"] ?? 0,
53+
"cartable_id" => $model->id,
54+
"cartable_type" => get_class($model)
55+
];
56+
$this->cart->put($item["id"], $item);
57+
$this->save();
5758
return $this;
5859
}
5960

@@ -87,22 +88,19 @@ public function instance($instanceName): CartService
8788
*/
8889
public function update($value, $key): CartService
8990
{
90-
$cart=collect($this->get($key,false));
91-
if($cart->isEmpty()){
91+
$item=collect($this->get($key,false));
92+
if($item->isEmpty()){
9293
return $this;
9394
}
9495
if(is_numeric($value)){
95-
$cart["quantity"]=$value;
96+
$item["quantity"]=$value;
9697
}
9798
else{
98-
99-
$this->cart->forget($cart["id"]);
100-
$cart=$cart->merge($value);
101-
99+
$this->cart->forget($item["id"]);
100+
$item=$item->merge($value);
102101
}
103-
104-
$this->cart->put($cart["id"],$cart->toArray());
105-
$this->session->put([$this->instanceName=>$this->cart]);
102+
$this->cart->put($item["id"], $item->toArray());
103+
$this->save();
106104
return $this;
107105

108106
}
@@ -127,12 +125,12 @@ public function has($key): bool
127125
*/
128126
public function delete($key): CartService
129127
{
130-
$model=collect($this->get($key,false));
131-
if ($model->isEmpty()){
128+
$item=collect($this->get($key,false));
129+
if ($item->isEmpty()){
132130
return $this;
133131
}
134-
$this->cart->forget($model["id"]);
135-
$this->session->put([$this->instanceName=>$this->cart]);
132+
$this->cart->forget($item["id"]);
133+
$this->save();
136134
return $this;
137135
}
138136

@@ -143,7 +141,7 @@ public function delete($key): CartService
143141
public function flush(): CartService
144142
{
145143
$this->cart=collect([]);
146-
$this->session->put([$this->instanceName=>$this->cart]);
144+
$this->save();
147145
return $this;
148146
}
149147

@@ -207,4 +205,12 @@ public function totalPrice():int
207205
});
208206
}
209207

208+
/**
209+
* save value in cart and session
210+
*/
211+
protected function save(): void
212+
{
213+
$this->session->put([$this->instanceName => $this->cart]);
214+
}
215+
210216
}

0 commit comments

Comments
 (0)