Delete.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryAdminUi\Controller\Adminhtml\Stock;
  8. use Magento\Backend\App\Action;
  9. use Magento\Backend\App\Action\Context;
  10. use Magento\Framework\Controller\ResultInterface;
  11. use Magento\Framework\Exception\CouldNotDeleteException;
  12. use Magento\InventoryApi\Api\Data\StockInterface;
  13. use Magento\InventoryApi\Api\StockRepositoryInterface;
  14. use Magento\Framework\App\Action\HttpPostActionInterface;
  15. /**
  16. * Delete Controller
  17. */
  18. class Delete extends Action implements HttpPostActionInterface
  19. {
  20. /**
  21. * @see _isAllowed()
  22. */
  23. const ADMIN_RESOURCE = 'Magento_InventoryApi::stock';
  24. /**
  25. * @var StockRepositoryInterface
  26. */
  27. private $stockRepository;
  28. /**
  29. * @param Context $context
  30. * @param StockRepositoryInterface $stockRepository
  31. */
  32. public function __construct(
  33. Context $context,
  34. StockRepositoryInterface $stockRepository
  35. ) {
  36. parent::__construct($context);
  37. $this->stockRepository = $stockRepository;
  38. }
  39. /**
  40. * @inheritdoc
  41. */
  42. public function execute(): ResultInterface
  43. {
  44. $resultRedirect = $this->resultRedirectFactory->create();
  45. $stockId = $this->getRequest()->getPost(StockInterface::STOCK_ID);
  46. if ($stockId === null) {
  47. $this->messageManager->addErrorMessage(__('Wrong request.'));
  48. return $resultRedirect->setPath('*/*');
  49. }
  50. try {
  51. $stockId = (int)$stockId;
  52. $this->stockRepository->deleteById($stockId);
  53. $this->messageManager->addSuccessMessage(__('The Stock has been deleted.'));
  54. $resultRedirect->setPath('*/*');
  55. } catch (CouldNotDeleteException $e) {
  56. $this->messageManager->addErrorMessage($e->getMessage());
  57. $resultRedirect->setPath('*/*/edit', [
  58. StockInterface::STOCK_ID => $stockId,
  59. '_current' => true,
  60. ]);
  61. }
  62. return $resultRedirect;
  63. }
  64. }