Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Delete.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
17 
21 class Delete extends Action implements HttpPostActionInterface
22 {
26  const ADMIN_RESOURCE = 'Magento_InventoryApi::stock';
27 
31  private $stockRepository;
32 
37  public function __construct(
38  Context $context,
39  StockRepositoryInterface $stockRepository
40  ) {
41  parent::__construct($context);
42  $this->stockRepository = $stockRepository;
43  }
44 
48  public function execute(): ResultInterface
49  {
50  $resultRedirect = $this->resultRedirectFactory->create();
51 
52  $stockId = $this->getRequest()->getPost(StockInterface::STOCK_ID);
53  if ($stockId === null) {
54  $this->messageManager->addErrorMessage(__('Wrong request.'));
55  return $resultRedirect->setPath('*/*');
56  }
57 
58  try {
59  $stockId = (int)$stockId;
60  $this->stockRepository->deleteById($stockId);
61  $this->messageManager->addSuccessMessage(__('The Stock has been deleted.'));
62  $resultRedirect->setPath('*/*');
63  } catch (CouldNotDeleteException $e) {
64  $this->messageManager->addErrorMessage($e->getMessage());
65  $resultRedirect->setPath('*/*/edit', [
66  StockInterface::STOCK_ID => $stockId,
67  '_current' => true,
68  ]);
69  }
70 
71  return $resultRedirect;
72  }
73 }
__construct(Context $context, StockRepositoryInterface $stockRepository)
Definition: Delete.php:37
__()
Definition: __.php:13
$stockRepository
Definition: stock.php:19