88namespace Magento \Checkout \Controller \Cart ;
99
1010use Magento \Checkout \Model \Cart \RequestQuantityProcessor ;
11+ use Magento \Checkout \Model \Session as CheckoutSession ;
12+ use Magento \Framework \App \Action \Action ;
1113use Magento \Framework \App \Action \Context ;
14+ use Magento \Framework \App \Action \HttpPostActionInterface ;
15+ use Magento \Framework \Data \Form \FormKey \Validator as FormKeyValidator ;
1216use Magento \Framework \Exception \LocalizedException ;
13- use Magento \Checkout \ Model \ Session as CheckoutSession ;
17+ use Magento \Framework \ Exception \ NotFoundException ;
1418use Magento \Framework \Serialize \Serializer \Json ;
15- use Magento \Framework \Data \Form \FormKey \Validator as FormKeyValidator ;
1619use Magento \Quote \Model \Quote \Item ;
1720use Psr \Log \LoggerInterface ;
1821
19- class UpdateItemQty extends \Magento \Framework \App \Action \Action
22+ /**
23+ * UpdateItemQty ajax request
24+ *
25+ * @package Magento\Checkout\Controller\Cart
26+ */
27+ class UpdateItemQty extends Action implements HttpPostActionInterface
2028{
29+
2130 /**
2231 * @var RequestQuantityProcessor
2332 */
@@ -44,13 +53,16 @@ class UpdateItemQty extends \Magento\Framework\App\Action\Action
4453 private $ logger ;
4554
4655 /**
47- * @param Context $context,
56+ * UpdateItemQty constructor
57+ *
58+ * @param Context $context
4859 * @param RequestQuantityProcessor $quantityProcessor
4960 * @param FormKeyValidator $formKeyValidator
5061 * @param CheckoutSession $checkoutSession
5162 * @param Json $json
5263 * @param LoggerInterface $logger
5364 */
65+
5466 public function __construct (
5567 Context $ context ,
5668 RequestQuantityProcessor $ quantityProcessor ,
@@ -68,30 +80,26 @@ public function __construct(
6880 }
6981
7082 /**
83+ * Controller execute method
84+ *
7185 * @return void
7286 */
7387 public function execute ()
7488 {
7589 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- }
90+ $ this ->validateRequest ();
91+ $ this ->validateFormKey ();
8192
8293 $ 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- }
94+
95+ $ this ->validateCartData ($ cartData );
8896
8997 $ cartData = $ this ->quantityProcessor ->process ($ cartData );
9098 $ quote = $ this ->checkoutSession ->getQuote ();
9199
92100 foreach ($ cartData as $ itemId => $ itemInfo ) {
93101 $ item = $ quote ->getItemById ($ itemId );
94- $ qty = isset ($ itemInfo ['qty ' ]) ? (double )$ itemInfo ['qty ' ] : 0 ;
102+ $ qty = isset ($ itemInfo ['qty ' ]) ? (double ) $ itemInfo ['qty ' ] : 0 ;
95103 if ($ item ) {
96104 $ this ->updateItemQuantity ($ item , $ qty );
97105 }
@@ -111,11 +119,13 @@ public function execute()
111119 *
112120 * @param Item $item
113121 * @param float $qty
122+ * @return void
114123 * @throws LocalizedException
115124 */
116125 private function updateItemQuantity (Item $ item , float $ qty )
117126 {
118127 if ($ qty > 0 ) {
128+ $ item ->clearMessage ();
119129 $ item ->setQty ($ qty );
120130
121131 if ($ item ->getHasError ()) {
@@ -145,9 +155,7 @@ private function jsonResponse(string $error = '')
145155 */
146156 private function getResponseData (string $ error = '' ): array
147157 {
148- $ response = [
149- 'success ' => true ,
150- ];
158+ $ response = ['success ' => true ];
151159
152160 if (!empty ($ error )) {
153161 $ response = [
@@ -158,4 +166,48 @@ private function getResponseData(string $error = ''): array
158166
159167 return $ response ;
160168 }
169+
170+ /**
171+ * Validates the Request HTTP method
172+ *
173+ * @return void
174+ * @throws NotFoundException
175+ */
176+ private function validateRequest ()
177+ {
178+ if ($ this ->getRequest ()->isPost () === false ) {
179+ throw new NotFoundException (__ ('Page Not Found ' ));
180+ }
181+ }
182+
183+ /**
184+ * Validates form key
185+ *
186+ * @return void
187+ * @throws LocalizedException
188+ */
189+ private function validateFormKey ()
190+ {
191+ if (!$ this ->formKeyValidator ->validate ($ this ->getRequest ())) {
192+ throw new LocalizedException (
193+ __ ('Something went wrong while saving the page. Please refresh the page and try again. ' )
194+ );
195+ }
196+ }
197+
198+ /**
199+ * Validates cart data
200+ *
201+ * @param array|null $cartData
202+ * @return void
203+ * @throws LocalizedException
204+ */
205+ private function validateCartData ($ cartData = null )
206+ {
207+ if (!is_array ($ cartData )) {
208+ throw new LocalizedException (
209+ __ ('Something went wrong while saving the page. Please refresh the page and try again. ' )
210+ );
211+ }
212+ }
161213}
0 commit comments