|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
| 6 | +declare(strict_types=1); |
| 7 | + |
6 | 8 | namespace Magento\Checkout\Controller\Sidebar;
|
7 | 9 |
|
8 |
| -use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface; |
| 10 | +use Exception; |
| 11 | +use Magento\Checkout\Model\Sidebar; |
| 12 | +use Magento\Framework\App\Action\HttpPostActionInterface; |
| 13 | +use Magento\Framework\App\RequestInterface; |
| 14 | +use Magento\Framework\Controller\Result\JsonFactory as ResultJsonFactory; |
| 15 | +use Magento\Framework\Controller\Result\RedirectFactory as ResultRedirectFactory; |
| 16 | +use Magento\Framework\Data\Form\FormKey\Validator; |
| 17 | +use Magento\Framework\Exception\LocalizedException; |
| 18 | +use Psr\Log\LoggerInterface; |
9 | 19 |
|
10 |
| -class RemoveItem extends \Magento\Framework\App\Action\Action implements HttpPostActionInterface |
| 20 | +class RemoveItem implements HttpPostActionInterface |
11 | 21 | {
|
12 | 22 | /**
|
13 |
| - * @var \Magento\Checkout\Model\Sidebar |
| 23 | + * @var RequestInterface |
14 | 24 | */
|
15 |
| - protected $sidebar; |
| 25 | + private $request; |
16 | 26 |
|
17 | 27 | /**
|
18 |
| - * @var \Psr\Log\LoggerInterface |
| 28 | + * @var ResultJsonFactory |
19 | 29 | */
|
20 |
| - protected $logger; |
| 30 | + private $resultJsonFactory; |
21 | 31 |
|
22 | 32 | /**
|
23 |
| - * @var \Magento\Framework\Json\Helper\Data |
| 33 | + * @var ResultRedirectFactory |
24 | 34 | */
|
25 |
| - protected $jsonHelper; |
| 35 | + private $resultRedirectFactory; |
26 | 36 |
|
27 | 37 | /**
|
28 |
| - * @var \Magento\Framework\View\Result\PageFactory |
| 38 | + * @var Sidebar |
29 | 39 | */
|
30 |
| - protected $resultPageFactory; |
| 40 | + protected $sidebar; |
31 | 41 |
|
32 | 42 | /**
|
33 |
| - * @var \Magento\Framework\Data\Form\FormKey\Validator |
| 43 | + * @var Validator |
34 | 44 | */
|
35 | 45 | private $formKeyValidator;
|
36 | 46 |
|
37 | 47 | /**
|
38 |
| - * @param \Magento\Framework\App\Action\Context $context |
39 |
| - * @param \Magento\Checkout\Model\Sidebar $sidebar |
40 |
| - * @param \Psr\Log\LoggerInterface $logger |
41 |
| - * @param \Magento\Framework\Json\Helper\Data $jsonHelper |
42 |
| - * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory |
| 48 | + * @var LoggerInterface |
| 49 | + */ |
| 50 | + protected $logger; |
| 51 | + |
| 52 | + /** |
| 53 | + * @param RequestInterface $request |
| 54 | + * @param ResultJsonFactory $resultJsonFactory |
| 55 | + * @param ResultRedirectFactory $resultRedirectFactory |
| 56 | + * @param Sidebar $sidebar |
| 57 | + * @param Validator $formKeyValidator |
| 58 | + * @param LoggerInterface $logger |
43 | 59 | */
|
44 | 60 | public function __construct(
|
45 |
| - \Magento\Framework\App\Action\Context $context, |
46 |
| - \Magento\Checkout\Model\Sidebar $sidebar, |
47 |
| - \Psr\Log\LoggerInterface $logger, |
48 |
| - \Magento\Framework\Json\Helper\Data $jsonHelper, |
49 |
| - \Magento\Framework\View\Result\PageFactory $resultPageFactory |
| 61 | + RequestInterface $request, |
| 62 | + ResultJsonFactory $resultJsonFactory, |
| 63 | + ResultRedirectFactory $resultRedirectFactory, |
| 64 | + Sidebar $sidebar, |
| 65 | + Validator $formKeyValidator, |
| 66 | + LoggerInterface $logger |
50 | 67 | ) {
|
| 68 | + $this->request = $request; |
| 69 | + $this->resultJsonFactory = $resultJsonFactory; |
| 70 | + $this->resultRedirectFactory = $resultRedirectFactory; |
51 | 71 | $this->sidebar = $sidebar;
|
| 72 | + $this->formKeyValidator = $formKeyValidator; |
52 | 73 | $this->logger = $logger;
|
53 |
| - $this->jsonHelper = $jsonHelper; |
54 |
| - $this->resultPageFactory = $resultPageFactory; |
55 |
| - parent::__construct($context); |
56 | 74 | }
|
57 | 75 |
|
58 | 76 | /**
|
59 |
| - * @return $this |
| 77 | + * @inheritDoc |
60 | 78 | */
|
61 | 79 | public function execute()
|
62 | 80 | {
|
63 |
| - if (!$this->getFormKeyValidator()->validate($this->getRequest())) { |
64 |
| - return $this->resultRedirectFactory->create()->setPath('*/cart/'); |
| 81 | + if (!$this->formKeyValidator->validate($this->request)) { |
| 82 | + return $this->resultRedirectFactory->create() |
| 83 | + ->setPath('*/cart/'); |
65 | 84 | }
|
66 |
| - $itemId = (int)$this->getRequest()->getParam('item_id'); |
| 85 | + |
| 86 | + $itemId = (int)$this->request->getParam('item_id'); |
| 87 | + $error = ''; |
| 88 | + |
67 | 89 | try {
|
68 | 90 | $this->sidebar->checkQuoteItem($itemId);
|
69 | 91 | $this->sidebar->removeQuoteItem($itemId);
|
70 |
| - return $this->jsonResponse(); |
71 |
| - } catch (\Magento\Framework\Exception\LocalizedException $e) { |
72 |
| - return $this->jsonResponse($e->getMessage()); |
73 |
| - } catch (\Exception $e) { |
| 92 | + } catch (LocalizedException $e) { |
| 93 | + $error = $e->getMessage(); |
| 94 | + } catch (Exception $e) { |
74 | 95 | $this->logger->critical($e);
|
75 |
| - return $this->jsonResponse($e->getMessage()); |
| 96 | + $error = $e->getMessage(); |
76 | 97 | }
|
77 |
| - } |
78 |
| - |
79 |
| - /** |
80 |
| - * Compile JSON response |
81 |
| - * |
82 |
| - * @param string $error |
83 |
| - * @return \Magento\Framework\App\Response\Http |
84 |
| - */ |
85 |
| - protected function jsonResponse($error = '') |
86 |
| - { |
87 |
| - $response = $this->sidebar->getResponseData($error); |
88 | 98 |
|
89 |
| - return $this->getResponse()->representJson( |
90 |
| - $this->jsonHelper->jsonEncode($response) |
91 |
| - ); |
92 |
| - } |
| 99 | + $resultJson = $this->resultJsonFactory->create(); |
| 100 | + $resultJson->setData($this->sidebar->getResponseData($error)); |
93 | 101 |
|
94 |
| - /** |
95 |
| - * @return \Magento\Framework\Data\Form\FormKey\Validator |
96 |
| - * @deprecated 100.0.9 |
97 |
| - */ |
98 |
| - private function getFormKeyValidator() |
99 |
| - { |
100 |
| - if (!$this->formKeyValidator) { |
101 |
| - $this->formKeyValidator = \Magento\Framework\App\ObjectManager::getInstance() |
102 |
| - ->get(\Magento\Framework\Data\Form\FormKey\Validator::class); |
103 |
| - } |
104 |
| - return $this->formKeyValidator; |
| 102 | + return $resultJson; |
105 | 103 | }
|
106 | 104 | }
|
0 commit comments