Delete.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\CheckoutAgreements\Controller\Adminhtml\Agreement;
  8. use Magento\CheckoutAgreements\Api\CheckoutAgreementsRepositoryInterface;
  9. use Magento\CheckoutAgreements\Controller\Adminhtml\Agreement;
  10. use Magento\Backend\App\Action\Context;
  11. use Magento\Framework\Registry;
  12. use Magento\Framework\App\ObjectManager;
  13. use Magento\Framework\Exception\LocalizedException;
  14. use Magento\Framework\App\Action\HttpPostActionInterface;
  15. class Delete extends Agreement implements HttpPostActionInterface
  16. {
  17. /**
  18. * @var CheckoutAgreementsRepositoryInterface
  19. */
  20. private $agreementRepository;
  21. /**
  22. * @param Context $context
  23. * @param Registry $coreRegistry
  24. * @param CheckoutAgreementsRepositoryInterface $agreementRepository
  25. */
  26. public function __construct(
  27. Context $context,
  28. Registry $coreRegistry,
  29. CheckoutAgreementsRepositoryInterface $agreementRepository = null
  30. ) {
  31. $this->agreementRepository = $agreementRepository ?:
  32. ObjectManager::getInstance()->get(CheckoutAgreementsRepositoryInterface::class);
  33. parent::__construct($context, $coreRegistry);
  34. }
  35. /**
  36. * @return void
  37. */
  38. public function execute()
  39. {
  40. $id = (int)$this->getRequest()->getParam('id');
  41. $agreement = $this->agreementRepository->get($id);
  42. if (!$agreement->getAgreementId()) {
  43. $this->messageManager->addError(__('This condition no longer exists.'));
  44. $this->_redirect('checkout/*/');
  45. return;
  46. }
  47. try {
  48. $this->agreementRepository->delete($agreement);
  49. $this->messageManager->addSuccess(__('You deleted the condition.'));
  50. $this->_redirect('checkout/*/');
  51. return;
  52. } catch (LocalizedException $e) {
  53. $this->messageManager->addError($e->getMessage());
  54. } catch (\Exception $e) {
  55. $this->messageManager->addError(__('Something went wrong while deleting this condition.'));
  56. }
  57. $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl($this->getUrl('*')));
  58. }
  59. }