stockRepository = $stockRepository; } /** * @inheritdoc */ public function execute(): ResultInterface { $resultRedirect = $this->resultRedirectFactory->create(); $stockId = $this->getRequest()->getPost(StockInterface::STOCK_ID); if ($stockId === null) { $this->messageManager->addErrorMessage(__('Wrong request.')); return $resultRedirect->setPath('*/*'); } try { $stockId = (int)$stockId; $this->stockRepository->deleteById($stockId); $this->messageManager->addSuccessMessage(__('The Stock has been deleted.')); $resultRedirect->setPath('*/*'); } catch (CouldNotDeleteException $e) { $this->messageManager->addErrorMessage($e->getMessage()); $resultRedirect->setPath('*/*/edit', [ StockInterface::STOCK_ID => $stockId, '_current' => true, ]); } return $resultRedirect; } }