|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace MageOS\AsyncEventsAdminUi\Controller\Adminhtml\Events; |
| 5 | + |
| 6 | +use MageOS\AsyncEvents\Api\Data\AsyncEventInterface; |
| 7 | +use MageOS\AsyncEvents\Api\Data\AsyncEventInterfaceFactory; |
| 8 | +use MageOS\AsyncEventsAdminUi\Command\AsyncEvent\SaveCommand; |
| 9 | +use Magento\Backend\App\Action; |
| 10 | +use Magento\Backend\App\Action\Context; |
| 11 | +use Magento\Framework\App\Action\HttpPostActionInterface; |
| 12 | +use Magento\Framework\App\Request\DataPersistorInterface; |
| 13 | +use Magento\Framework\App\ResponseInterface; |
| 14 | +use Magento\Framework\Controller\ResultInterface; |
| 15 | +use Magento\Framework\Exception\CouldNotSaveException; |
| 16 | + |
| 17 | +/** |
| 18 | + * Save AsyncEvent controller action. |
| 19 | + */ |
| 20 | +class Save extends Action implements HttpPostActionInterface |
| 21 | +{ |
| 22 | + /** |
| 23 | + * Authorization level of a basic admin session |
| 24 | + * |
| 25 | + * @see _isAllowed() |
| 26 | + */ |
| 27 | + public const ADMIN_RESOURCE = 'MageOS_AsyncEvents::async_events_create'; |
| 28 | + |
| 29 | + private DataPersistorInterface $dataPersistor; |
| 30 | + private SaveCommand $saveCommand; |
| 31 | + private AsyncEventInterfaceFactory $asyncEventFactory; |
| 32 | + |
| 33 | + public function __construct( |
| 34 | + Context $context, |
| 35 | + DataPersistorInterface $dataPersistor, |
| 36 | + SaveCommand $saveCommand, |
| 37 | + AsyncEventInterfaceFactory $entityDataFactory |
| 38 | + ) { |
| 39 | + parent::__construct($context); |
| 40 | + $this->dataPersistor = $dataPersistor; |
| 41 | + $this->saveCommand = $saveCommand; |
| 42 | + $this->asyncEventFactory = $entityDataFactory; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Save AsyncEvent Action. |
| 47 | + * |
| 48 | + * @return ResponseInterface|ResultInterface |
| 49 | + */ |
| 50 | + public function execute() |
| 51 | + { |
| 52 | + $resultRedirect = $this->resultRedirectFactory->create(); |
| 53 | + $params = $this->getRequest()->getParams(); |
| 54 | + if (isset($params['general'])) { |
| 55 | + $params = $params['general']; |
| 56 | + } |
| 57 | + if ($params['subscription_id'] === '') { |
| 58 | + unset($params['subscription_id']); |
| 59 | + } |
| 60 | + |
| 61 | + try { |
| 62 | + /** @var AsyncEventInterface $entityModel */ |
| 63 | + $entityModel = $this->asyncEventFactory->create(); |
| 64 | + $entityModel->addData($params); |
| 65 | + $this->saveCommand->execute($entityModel); |
| 66 | + $this->messageManager->addSuccessMessage( |
| 67 | + __('The Asynchronous Event Subscriber data was saved successfully') |
| 68 | + ); |
| 69 | + $this->dataPersistor->clear('entity'); |
| 70 | + } catch (CouldNotSaveException $exception) { |
| 71 | + $this->messageManager->addErrorMessage($exception->getMessage()); |
| 72 | + $this->dataPersistor->set('entity', $params); |
| 73 | + |
| 74 | + return $resultRedirect->setPath('*/*/edit', [ |
| 75 | + 'subscription_id' => $this->getRequest()->getParam('subscription_id') |
| 76 | + ]); |
| 77 | + } |
| 78 | + |
| 79 | + return $resultRedirect->setPath('*/*/'); |
| 80 | + } |
| 81 | +} |
0 commit comments