8
8
namespace Magento \Checkout \Controller \Cart ;
9
9
10
10
use Magento \Checkout \Model \Cart \RequestQuantityProcessor ;
11
+ use Magento \Checkout \Model \Session as CheckoutSession ;
11
12
use Magento \Framework \App \Action \Context ;
13
+ use Magento \Framework \Data \Form \FormKey \Validator as FormKeyValidator ;
12
14
use Magento \Framework \Exception \LocalizedException ;
13
- use Magento \Checkout \ Model \ Session as CheckoutSession ;
15
+ use Magento \Framework \ Exception \ NotFoundException ;
14
16
use Magento \Framework \Serialize \Serializer \Json ;
15
- use Magento \Framework \Data \Form \FormKey \Validator as FormKeyValidator ;
16
17
use Magento \Quote \Model \Quote \Item ;
18
+ use Magento \Framework \App \Action \Action ;
19
+ use Magento \Framework \App \Action \HttpPostActionInterface ;
17
20
use Psr \Log \LoggerInterface ;
18
21
19
- class UpdateItemQty extends \Magento \Framework \App \Action \Action
22
+ /**
23
+ * Class UpdateItemQty
24
+ *
25
+ * @package Magento\Checkout\Controller\Cart
26
+ */
27
+ class UpdateItemQty extends Action implements HttpPostActionInterface
20
28
{
21
29
/**
22
30
* @var RequestQuantityProcessor
@@ -44,13 +52,16 @@ class UpdateItemQty extends \Magento\Framework\App\Action\Action
44
52
private $ logger ;
45
53
46
54
/**
47
- * @param Context $context,
48
- * @param RequestQuantityProcessor $quantityProcessor
49
- * @param FormKeyValidator $formKeyValidator
50
- * @param CheckoutSession $checkoutSession
51
- * @param Json $json
52
- * @param LoggerInterface $logger
55
+ * UpdateItemQty constructor
56
+ *
57
+ * @param Context $context Parent dependency
58
+ * @param RequestQuantityProcessor $quantityProcessor Request quantity
59
+ * @param FormKeyValidator $formKeyValidator Form validator
60
+ * @param CheckoutSession $checkoutSession Session
61
+ * @param Json $json Json serializer
62
+ * @param LoggerInterface $logger Logger
53
63
*/
64
+
54
65
public function __construct (
55
66
Context $ context ,
56
67
RequestQuantityProcessor $ quantityProcessor ,
@@ -60,31 +71,29 @@ public function __construct(
60
71
LoggerInterface $ logger
61
72
) {
62
73
$ this ->quantityProcessor = $ quantityProcessor ;
63
- $ this ->formKeyValidator = $ formKeyValidator ;
64
- $ this ->checkoutSession = $ checkoutSession ;
65
- $ this ->json = $ json ;
66
- $ this ->logger = $ logger ;
74
+ $ this ->formKeyValidator = $ formKeyValidator ;
75
+ $ this ->checkoutSession = $ checkoutSession ;
76
+ $ this ->json = $ json ;
77
+ $ this ->logger = $ logger ;
67
78
parent ::__construct ($ context );
79
+
68
80
}
69
81
82
+
70
83
/**
84
+ * Controller execute method
85
+ *
71
86
* @return void
72
87
*/
73
88
public function execute ()
74
89
{
75
90
try {
76
- if (!$ this ->formKeyValidator ->validate ($ this ->getRequest ())) {
77
- throw new LocalizedException (
78
- __ ('Something went wrong while saving the page. Please refresh the page and try again. ' )
79
- );
80
- }
91
+ $ this ->validateRequest ();
92
+ $ this ->validateFormKey ();
81
93
82
94
$ cartData = $ this ->getRequest ()->getParam ('cart ' );
83
- if (!is_array ($ cartData )) {
84
- throw new LocalizedException (
85
- __ ('Something went wrong while saving the page. Please refresh the page and try again. ' )
86
- );
87
- }
95
+
96
+ $ this ->validateCartData ($ cartData );
88
97
89
98
$ cartData = $ this ->quantityProcessor ->process ($ cartData );
90
99
$ quote = $ this ->checkoutSession ->getQuote ();
@@ -109,15 +118,17 @@ public function execute()
109
118
/**
110
119
* Updates quote item quantity.
111
120
*
112
- * @param Item $item
121
+ * @param Item $item
113
122
* @param float $qty
123
+ *
114
124
* @throws LocalizedException
125
+ *
126
+ * @return void
115
127
*/
116
128
private function updateItemQuantity (Item $ item , float $ qty )
117
129
{
118
- $ item ->clearMessage ();
119
-
120
130
if ($ qty > 0 ) {
131
+ $ item ->clearMessage ();
121
132
$ item ->setQty ($ qty );
122
133
123
134
if ($ item ->getHasError ()) {
@@ -130,6 +141,7 @@ private function updateItemQuantity(Item $item, float $qty)
130
141
* JSON response builder.
131
142
*
132
143
* @param string $error
144
+ *
133
145
* @return void
134
146
*/
135
147
private function jsonResponse (string $ error = '' )
@@ -143,6 +155,7 @@ private function jsonResponse(string $error = '')
143
155
* Returns response data.
144
156
*
145
157
* @param string $error
158
+ *
146
159
* @return array
147
160
*/
148
161
private function getResponseData (string $ error = '' ): array
@@ -160,4 +173,57 @@ private function getResponseData(string $error = ''): array
160
173
161
174
return $ response ;
162
175
}
176
+
177
+ /**
178
+ * Validates the Request HTTP method
179
+ *
180
+ * @throws NotFoundException
181
+ *
182
+ * @return void
183
+ */
184
+ private function validateRequest ()
185
+ {
186
+ if ($ this ->getRequest ()->isPost () === false ) {
187
+ throw new NotFoundException (
188
+ __ ('Page Not Found ' )
189
+ );
190
+ }
191
+
192
+ }
193
+
194
+ /**
195
+ * Validates form key
196
+ *
197
+ * @throws LocalizedException
198
+ *
199
+ * @return void
200
+ */
201
+ private function validateFormKey ()
202
+ {
203
+ if (!$ this ->formKeyValidator ->validate ($ this ->getRequest ())) {
204
+ throw new LocalizedException (
205
+ __ ('Something went wrong while saving the page. Please refresh the page and try again. ' )
206
+ );
207
+ }
208
+
209
+ }
210
+
211
+ /**
212
+ * Validates cart data
213
+ *
214
+ * @param array|null $cartData
215
+ *
216
+ * @throws LocalizedException
217
+ *
218
+ * @return void
219
+ */
220
+ private function validateCartData ($ cartData = null )
221
+ {
222
+ if (!is_array ($ cartData )) {
223
+ throw new LocalizedException (
224
+ __ ('Something went wrong while saving the page. Please refresh the page and try again. ' )
225
+ );
226
+ }
227
+
228
+ }
163
229
}
0 commit comments