Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Save.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
10 use Exception;
23 
27 class Save extends Action implements HttpPostActionInterface
28 {
32  const ADMIN_RESOURCE = 'Magento_InventoryApi::stock';
33 
37  private $stockSaveProcessor;
38 
43  public function __construct(
44  Context $context,
45  StockSaveProcessor $stockSaveProcessor
46  ) {
47  parent::__construct($context);
48  $this->stockSaveProcessor = $stockSaveProcessor;
49  }
50 
54  public function execute(): ResultInterface
55  {
56  $resultRedirect = $this->resultRedirectFactory->create();
57  $request = $this->getRequest();
58  $requestData = $request->getParams();
59  if (!$request->isPost() || empty($requestData['general'])) {
60  $this->messageManager->addErrorMessage(__('Wrong request.'));
61  $this->processRedirectAfterFailureSave($resultRedirect);
62  return $resultRedirect;
63  }
64  return $this->processSave($requestData, $request, $resultRedirect);
65  }
66 
73  private function processSave(
74  array $requestData,
76  Redirect $resultRedirect
77  ): ResultInterface {
78  try {
79  $stockId = isset($requestData['general'][StockInterface::STOCK_ID])
80  ? (int)$requestData['general'][StockInterface::STOCK_ID]
81  : null;
82 
83  $stockId = $this->stockSaveProcessor->process($stockId, $request);
84 
85  $this->messageManager->addSuccessMessage(__('The Stock has been saved.'));
86  $this->processRedirectAfterSuccessSave($resultRedirect, $stockId);
87  } catch (NoSuchEntityException $e) {
88  $this->messageManager->addErrorMessage(__('The Stock does not exist.'));
89  $this->processRedirectAfterFailureSave($resultRedirect);
90  } catch (ValidationException $e) {
91  foreach ($e->getErrors() as $localizedError) {
92  $this->messageManager->addErrorMessage($localizedError->getMessage());
93  }
94  $this->processRedirectAfterFailureSave($resultRedirect, $stockId);
95  } catch (CouldNotSaveException $e) {
96  $this->messageManager->addErrorMessage($e->getMessage());
97  $this->processRedirectAfterFailureSave($resultRedirect, $stockId);
98  } catch (InputException $e) {
99  $this->messageManager->addErrorMessage($e->getMessage());
100  $this->processRedirectAfterFailureSave($resultRedirect, $stockId);
101  } catch (Exception $e) {
102  $this->messageManager->addErrorMessage(__('Could not save Stock.'));
103  $this->processRedirectAfterFailureSave($resultRedirect, $stockId ?? null);
104  }
105  return $resultRedirect;
106  }
107 
114  private function processRedirectAfterSuccessSave(Redirect $resultRedirect, int $stockId)
115  {
116  if ($this->getRequest()->getParam('back')) {
117  $resultRedirect->setPath('*/*/edit', [
118  StockInterface::STOCK_ID => $stockId,
119  '_current' => true,
120  ]);
121  } elseif ($this->getRequest()->getParam('redirect_to_new')) {
122  $resultRedirect->setPath('*/*/new', [
123  '_current' => true,
124  ]);
125  } else {
126  $resultRedirect->setPath('*/*/');
127  }
128  }
129 
136  private function processRedirectAfterFailureSave(Redirect $resultRedirect, int $stockId = null)
137  {
138  if (null === $stockId) {
139  $resultRedirect->setPath('*/*/new');
140  } else {
141  $resultRedirect->setPath('*/*/edit', [
142  StockInterface::STOCK_ID => $stockId,
143  '_current' => true,
144  ]);
145  }
146  }
147 }
__construct(Context $context, StockSaveProcessor $stockSaveProcessor)
Definition: Save.php:43
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13