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