7
7
8
8
use Magento \Checkout \Model \Cart \RequestQuantityProcessor ;
9
9
use 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 ;
12
11
use Magento \Framework \App \ObjectManager ;
12
+ use Magento \Framework \App \RequestInterface ;
13
13
use Magento \Framework \App \Response \Http ;
14
+ use Magento \Framework \App \ResponseInterface ;
15
+ use Magento \Framework \Exception \InputException ;
14
16
use Magento \Framework \Exception \LocalizedException ;
15
17
use Magento \Framework \Json \Helper \Data ;
16
18
use Psr \Log \LoggerInterface ;
17
19
18
- class UpdateItemQty extends Action
20
+ class UpdateItemQty implements HttpPostActionInterface
19
21
{
20
22
/**
21
23
* @var Sidebar
@@ -38,38 +40,57 @@ class UpdateItemQty extends Action
38
40
private $ quantityProcessor ;
39
41
40
42
/**
41
- * @param Context $context
43
+ * @var RequestInterface
44
+ */
45
+ private $ request ;
46
+
47
+ /**
48
+ * @var ResponseInterface
49
+ */
50
+ private $ response ;
51
+
52
+ /**
42
53
* @param Sidebar $sidebar
43
54
* @param LoggerInterface $logger
44
55
* @param Data $jsonHelper
56
+ * @param RequestInterface $request
57
+ * @param ResponseInterface $response
45
58
* @param RequestQuantityProcessor|null $quantityProcessor
46
59
* @codeCoverageIgnore
47
60
*/
48
61
public function __construct (
49
- Context $ context ,
50
62
Sidebar $ sidebar ,
51
63
LoggerInterface $ logger ,
52
64
Data $ jsonHelper ,
65
+ RequestInterface $ request ,
66
+ ResponseInterface $ response ,
53
67
?RequestQuantityProcessor $ quantityProcessor = null
54
68
) {
55
69
$ this ->sidebar = $ sidebar ;
56
70
$ this ->logger = $ logger ;
57
71
$ this ->jsonHelper = $ jsonHelper ;
58
- parent ::__construct ($ context );
72
+ $ this ->request = $ request ;
73
+ $ this ->response = $ response ;
59
74
$ this ->quantityProcessor = $ quantityProcessor
60
75
?? ObjectManager::getInstance ()->get (RequestQuantityProcessor::class);
61
76
}
62
77
63
78
/**
79
+ * Action for Quantity update
80
+ *
64
81
* @return $this
65
82
*/
66
83
public function execute ()
67
84
{
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 ' );
71
87
88
+ if (!is_numeric ($ itemQty ) || ($ itemQty <=0 )) {
89
+ $ e = new InputException (__ ('A non-numeric value found ' )) ;
90
+ return $ this ->jsonResponse ($ e ->getMessage ());
91
+ }
72
92
try {
93
+ $ itemQty = $ this ->quantityProcessor ->prepareQuantity ($ itemQty *1 );
73
94
$ this ->sidebar ->checkQuoteItem ($ itemId );
74
95
$ this ->sidebar ->updateQuoteItem ($ itemId , $ itemQty );
75
96
return $ this ->jsonResponse ();
@@ -89,7 +110,7 @@ public function execute()
89
110
*/
90
111
protected function jsonResponse ($ error = '' )
91
112
{
92
- return $ this ->getResponse () ->representJson (
113
+ return $ this ->response ->representJson (
93
114
$ this ->jsonHelper ->jsonEncode ($ this ->sidebar ->getResponseData ($ error ))
94
115
);
95
116
}
0 commit comments