Skip to content

Commit 0a67f76

Browse files
committed
class refactoring
1 parent cc78567 commit 0a67f76

File tree

1 file changed

+92
-26
lines changed

1 file changed

+92
-26
lines changed

app/code/Magento/Checkout/Controller/Cart/UpdateItemQty.php

Lines changed: 92 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,23 @@
88
namespace Magento\Checkout\Controller\Cart;
99

1010
use Magento\Checkout\Model\Cart\RequestQuantityProcessor;
11+
use Magento\Checkout\Model\Session as CheckoutSession;
1112
use Magento\Framework\App\Action\Context;
13+
use Magento\Framework\Data\Form\FormKey\Validator as FormKeyValidator;
1214
use Magento\Framework\Exception\LocalizedException;
13-
use Magento\Checkout\Model\Session as CheckoutSession;
15+
use Magento\Framework\Exception\NotFoundException;
1416
use Magento\Framework\Serialize\Serializer\Json;
15-
use Magento\Framework\Data\Form\FormKey\Validator as FormKeyValidator;
1617
use Magento\Quote\Model\Quote\Item;
18+
use Magento\Framework\App\Action\Action;
19+
use Magento\Framework\App\Action\HttpPostActionInterface;
1720
use Psr\Log\LoggerInterface;
1821

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
2028
{
2129
/**
2230
* @var RequestQuantityProcessor
@@ -44,13 +52,16 @@ class UpdateItemQty extends \Magento\Framework\App\Action\Action
4452
private $logger;
4553

4654
/**
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
5363
*/
64+
5465
public function __construct(
5566
Context $context,
5667
RequestQuantityProcessor $quantityProcessor,
@@ -60,31 +71,29 @@ public function __construct(
6071
LoggerInterface $logger
6172
) {
6273
$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;
6778
parent::__construct($context);
79+
6880
}
6981

82+
7083
/**
84+
* Controller execute method
85+
*
7186
* @return void
7287
*/
7388
public function execute()
7489
{
7590
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();
8193

8294
$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);
8897

8998
$cartData = $this->quantityProcessor->process($cartData);
9099
$quote = $this->checkoutSession->getQuote();
@@ -109,15 +118,17 @@ public function execute()
109118
/**
110119
* Updates quote item quantity.
111120
*
112-
* @param Item $item
121+
* @param Item $item
113122
* @param float $qty
123+
*
114124
* @throws LocalizedException
125+
*
126+
* @return void
115127
*/
116128
private function updateItemQuantity(Item $item, float $qty)
117129
{
118-
$item->clearMessage();
119-
120130
if ($qty > 0) {
131+
$item->clearMessage();
121132
$item->setQty($qty);
122133

123134
if ($item->getHasError()) {
@@ -130,6 +141,7 @@ private function updateItemQuantity(Item $item, float $qty)
130141
* JSON response builder.
131142
*
132143
* @param string $error
144+
*
133145
* @return void
134146
*/
135147
private function jsonResponse(string $error = '')
@@ -143,6 +155,7 @@ private function jsonResponse(string $error = '')
143155
* Returns response data.
144156
*
145157
* @param string $error
158+
*
146159
* @return array
147160
*/
148161
private function getResponseData(string $error = ''): array
@@ -160,4 +173,57 @@ private function getResponseData(string $error = ''): array
160173

161174
return $response;
162175
}
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+
}
163229
}

0 commit comments

Comments
 (0)