8
8
class CartService
9
9
{
10
10
/**
11
+ * the cart items
11
12
* @var Collection
12
13
*/
13
14
protected $ cart ;
@@ -19,9 +20,12 @@ class CartService
19
20
protected $ instanceName ="cart " ;
20
21
21
22
/**
23
+ * the cart session
22
24
* @var
23
25
*/
24
26
protected $ session ;
27
+
28
+
25
29
public function __construct ($ instanceName ,$ session )
26
30
{
27
31
$ this ->instanceName =$ instanceName ;
@@ -33,27 +37,24 @@ public function __construct($instanceName,$session)
33
37
/**
34
38
* put data in cart session
35
39
*
36
- * @param array $value
40
+ * @param array $item
37
41
* @param null $model
38
42
* @return $this
39
43
*/
40
- public function put (array $ value , $ model ): CartService
44
+ public function put (array $ item , $ model ): CartService
41
45
{
42
46
if ($ this ->has ($ model )) {
43
47
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
- ];
53
48
}
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 ();
57
58
return $ this ;
58
59
}
59
60
@@ -87,22 +88,19 @@ public function instance($instanceName): CartService
87
88
*/
88
89
public function update ($ value , $ key ): CartService
89
90
{
90
- $ cart =collect ($ this ->get ($ key ,false ));
91
- if ($ cart ->isEmpty ()){
91
+ $ item =collect ($ this ->get ($ key ,false ));
92
+ if ($ item ->isEmpty ()){
92
93
return $ this ;
93
94
}
94
95
if (is_numeric ($ value )){
95
- $ cart ["quantity " ]=$ value ;
96
+ $ item ["quantity " ]=$ value ;
96
97
}
97
98
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 );
102
101
}
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 ();
106
104
return $ this ;
107
105
108
106
}
@@ -127,12 +125,12 @@ public function has($key): bool
127
125
*/
128
126
public function delete ($ key ): CartService
129
127
{
130
- $ model =collect ($ this ->get ($ key ,false ));
131
- if ($ model ->isEmpty ()){
128
+ $ item =collect ($ this ->get ($ key ,false ));
129
+ if ($ item ->isEmpty ()){
132
130
return $ this ;
133
131
}
134
- $ this ->cart ->forget ($ model ["id " ]);
135
- $ this ->session -> put ([ $ this -> instanceName => $ this -> cart ] );
132
+ $ this ->cart ->forget ($ item ["id " ]);
133
+ $ this ->save ( );
136
134
return $ this ;
137
135
}
138
136
@@ -143,7 +141,7 @@ public function delete($key): CartService
143
141
public function flush (): CartService
144
142
{
145
143
$ this ->cart =collect ([]);
146
- $ this ->session -> put ([ $ this -> instanceName => $ this -> cart ] );
144
+ $ this ->save ( );
147
145
return $ this ;
148
146
}
149
147
@@ -207,4 +205,12 @@ public function totalPrice():int
207
205
});
208
206
}
209
207
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
+
210
216
}
0 commit comments