Unsubscribe.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Newsletter\Controller\Subscriber;
  7. use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
  8. /**
  9. * Controller for unsubscribing customers.
  10. */
  11. class Unsubscribe extends \Magento\Newsletter\Controller\Subscriber implements HttpGetActionInterface
  12. {
  13. /**
  14. * Unsubscribe newsletter.
  15. *
  16. * @return \Magento\Backend\Model\View\Result\Redirect
  17. */
  18. public function execute()
  19. {
  20. $id = (int)$this->getRequest()->getParam('id');
  21. $code = (string)$this->getRequest()->getParam('code');
  22. if ($id && $code) {
  23. try {
  24. $this->_subscriberFactory->create()->load($id)->setCheckCode($code)->unsubscribe();
  25. $this->messageManager->addSuccess(__('You unsubscribed.'));
  26. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  27. $this->messageManager->addException($e, $e->getMessage());
  28. } catch (\Exception $e) {
  29. $this->messageManager->addException($e, __('Something went wrong while unsubscribing you.'));
  30. }
  31. }
  32. /** @var \Magento\Backend\Model\View\Result\Redirect $redirect */
  33. $redirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
  34. $redirectUrl = $this->_redirect->getRedirectUrl();
  35. return $redirect->setUrl($redirectUrl);
  36. }
  37. }