77
88use Magento \Checkout \Model \Cart \RequestQuantityProcessor ;
99use Magento \Checkout \Model \Sidebar ;
10- use Magento \Framework \App \Action \Action ;
11- use Magento \Framework \App \Action \Context ;
10+ use Magento \Framework \App \Action \HttpPostActionInterface ;
1211use Magento \Framework \App \ObjectManager ;
12+ use Magento \Framework \App \RequestInterface ;
1313use Magento \Framework \App \Response \Http ;
14+ use Magento \Framework \App \ResponseInterface ;
15+ use Magento \Framework \Exception \InputException ;
1416use Magento \Framework \Exception \LocalizedException ;
1517use Magento \Framework \Json \Helper \Data ;
1618use Psr \Log \LoggerInterface ;
1719
18- class UpdateItemQty extends Action
20+ class UpdateItemQty implements HttpPostActionInterface
1921{
2022 /**
2123 * @var Sidebar
@@ -38,38 +40,57 @@ class UpdateItemQty extends Action
3840 private $ quantityProcessor ;
3941
4042 /**
41- * @param Context $context
43+ * @var RequestInterface
44+ */
45+ private $ request ;
46+
47+ /**
48+ * @var ResponseInterface
49+ */
50+ private $ response ;
51+
52+ /**
4253 * @param Sidebar $sidebar
4354 * @param LoggerInterface $logger
4455 * @param Data $jsonHelper
56+ * @param RequestInterface $request
57+ * @param ResponseInterface $response
4558 * @param RequestQuantityProcessor|null $quantityProcessor
4659 * @codeCoverageIgnore
4760 */
4861 public function __construct (
49- Context $ context ,
5062 Sidebar $ sidebar ,
5163 LoggerInterface $ logger ,
5264 Data $ jsonHelper ,
65+ RequestInterface $ request ,
66+ ResponseInterface $ response ,
5367 ?RequestQuantityProcessor $ quantityProcessor = null
5468 ) {
5569 $ this ->sidebar = $ sidebar ;
5670 $ this ->logger = $ logger ;
5771 $ this ->jsonHelper = $ jsonHelper ;
58- parent ::__construct ($ context );
72+ $ this ->request = $ request ;
73+ $ this ->response = $ response ;
5974 $ this ->quantityProcessor = $ quantityProcessor
6075 ?? ObjectManager::getInstance ()->get (RequestQuantityProcessor::class);
6176 }
6277
6378 /**
79+ * Action for Quantity update
80+ *
6481 * @return $this
6582 */
6683 public function execute ()
6784 {
68- $ itemId = (int )$ this ->getRequest ()->getParam ('item_id ' );
69- $ itemQty = $ this ->getRequest ()->getParam ('item_qty ' ) * 1 ;
70- $ itemQty = $ this ->quantityProcessor ->prepareQuantity ($ itemQty );
85+ $ itemId = (int )$ this ->request ->getParam ('item_id ' );
86+ $ itemQty = $ this ->request ->getParam ('item_qty ' );
7187
88+ if (!is_numeric ($ itemQty ) || ($ itemQty <=0 )) {
89+ $ e = new InputException (__ ('A non-numeric value found ' )) ;
90+ return $ this ->jsonResponse ($ e ->getMessage ());
91+ }
7292 try {
93+ $ itemQty = $ this ->quantityProcessor ->prepareQuantity ($ itemQty *1 );
7394 $ this ->sidebar ->checkQuoteItem ($ itemId );
7495 $ this ->sidebar ->updateQuoteItem ($ itemId , $ itemQty );
7596 return $ this ->jsonResponse ();
@@ -89,7 +110,7 @@ public function execute()
89110 */
90111 protected function jsonResponse ($ error = '' )
91112 {
92- return $ this ->getResponse () ->representJson (
113+ return $ this ->response ->representJson (
93114 $ this ->jsonHelper ->jsonEncode ($ this ->sidebar ->getResponseData ($ error ))
94115 );
95116 }
0 commit comments