Delete.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Controller\Adminhtml\Configuration\Carrier;
  6. use Magento\Backend\App\Action;
  7. use Magento\Backend\App\Action\Context;
  8. use Temando\Shipping\Model\ResourceModel\Repository\CarrierRepositoryInterface;
  9. /**
  10. * Temando Delete Carrier Action
  11. *
  12. * @package Temando\Shipping\Controller
  13. * @author Benjamin Heuer <benjamin.heuer@netresearch.de>
  14. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  15. * @link http://www.temando.com/
  16. */
  17. class Delete extends Action
  18. {
  19. const ADMIN_RESOURCE = 'Temando_Shipping::carriers';
  20. /**
  21. * @var CarrierRepositoryInterface
  22. */
  23. private $carrierRepository;
  24. /**
  25. * Temando Carrier Delete Action constructor.
  26. *
  27. * @param Context $context
  28. * @param CarrierRepositoryInterface $carrierRepository
  29. */
  30. public function __construct(Context $context, CarrierRepositoryInterface $carrierRepository)
  31. {
  32. $this->carrierRepository = $carrierRepository;
  33. parent::__construct($context);
  34. }
  35. /**
  36. * @return \Magento\Framework\Controller\Result\Redirect
  37. */
  38. public function execute()
  39. {
  40. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  41. $resultRedirect = $this->resultRedirectFactory->create();
  42. $resultRedirect->setPath('*/*/index');
  43. $carrierConfigurationId = $this->getRequest()->getParam('configuration_id', false);
  44. if (!$carrierConfigurationId) {
  45. $this->messageManager->addErrorMessage(__('Carrier ID missing.'));
  46. return $resultRedirect;
  47. }
  48. try {
  49. $this->carrierRepository->delete($carrierConfigurationId);
  50. $this->messageManager->addSuccessMessage(__('Carrier was deleted successfully.'));
  51. } catch (\Exception $e) {
  52. $message = __('An error occurred while deleting the carrier: %1', $e->getMessage());
  53. $this->messageManager->addExceptionMessage($e, $message);
  54. return $resultRedirect;
  55. }
  56. return $resultRedirect;
  57. }
  58. }