Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Save.php
Go to the documentation of this file.
1 <?php
9 
10 use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
12 
14 {
18  protected $formKeyValidator;
19 
23  protected $storeManager;
24 
29 
33  protected $subscriberFactory;
34 
45  public function __construct(
46  \Magento\Framework\App\Action\Context $context,
47  \Magento\Customer\Model\Session $customerSession,
48  \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
50  CustomerRepository $customerRepository,
51  \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
52  ) {
53  $this->storeManager = $storeManager;
54  $this->formKeyValidator = $formKeyValidator;
55  $this->customerRepository = $customerRepository;
56  $this->subscriberFactory = $subscriberFactory;
57  parent::__construct($context, $customerSession);
58  }
59 
65  public function execute()
66  {
67  if (!$this->formKeyValidator->validate($this->getRequest())) {
68  return $this->_redirect('customer/account/');
69  }
70 
71  $customerId = $this->_customerSession->getCustomerId();
72  if ($customerId === null) {
73  $this->messageManager->addError(__('Something went wrong while saving your subscription.'));
74  } else {
75  try {
76  $customer = $this->customerRepository->getById($customerId);
77  $storeId = $this->storeManager->getStore()->getId();
78  $customer->setStoreId($storeId);
79  $isSubscribedState = $customer->getExtensionAttributes()
80  ->getIsSubscribed();
81  $isSubscribedParam = (boolean)$this->getRequest()
82  ->getParam('is_subscribed', false);
83  if ($isSubscribedParam !== $isSubscribedState) {
84  $this->customerRepository->save($customer);
85  if ($isSubscribedParam) {
86  $subscribeModel = $this->subscriberFactory->create()
87  ->subscribeCustomerById($customerId);
88  $subscribeStatus = $subscribeModel->getStatus();
89  if ($subscribeStatus == Subscriber::STATUS_SUBSCRIBED) {
90  $this->messageManager->addSuccess(__('We have saved your subscription.'));
91  } else {
92  $this->messageManager->addSuccess(__('A confirmation request has been sent.'));
93  }
94  } else {
95  $this->subscriberFactory->create()
96  ->unsubscribeCustomerById($customerId);
97  $this->messageManager->addSuccess(__('We have removed your newsletter subscription.'));
98  }
99  } else {
100  $this->messageManager->addSuccess(__('We have updated your subscription.'));
101  }
102  } catch (\Exception $e) {
103  $this->messageManager->addError(__('Something went wrong while saving your subscription.'));
104  }
105  }
106  $this->_redirect('customer/account/');
107  }
108 }
_redirect($path, $arguments=[])
Definition: Action.php:167
$customer
Definition: customers.php:11
__()
Definition: __.php:13
__construct(\Magento\Framework\App\Action\Context $context, \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator, \Magento\Store\Model\StoreManagerInterface $storeManager, CustomerRepository $customerRepository, \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory)
Definition: Save.php:45