RemoveItem.php 3.1 KB

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