Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
NewAction.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Customer\Api\AccountManagementInterface as CustomerAccountManagement;
11 use Magento\Customer\Model\Url as CustomerUrl;
17 use Magento\Framework\Validator\EmailAddress as EmailValidator;
22 use Magento\Newsletter\Model\SubscriberFactory;
23 
28 {
33 
37  private $emailValidator;
38 
50  public function __construct(
51  Context $context,
52  SubscriberFactory $subscriberFactory,
53  Session $customerSession,
55  CustomerUrl $customerUrl,
56  CustomerAccountManagement $customerAccountManagement,
57  EmailValidator $emailValidator = null
58  ) {
59  $this->customerAccountManagement = $customerAccountManagement;
60  $this->emailValidator = $emailValidator ?: ObjectManager::getInstance()->get(EmailValidator::class);
61  parent::__construct(
62  $context,
63  $subscriberFactory,
64  $customerSession,
67  );
68  }
69 
77  protected function validateEmailAvailable($email)
78  {
79  $websiteId = $this->_storeManager->getStore()->getWebsiteId();
80  if ($this->_customerSession->getCustomerDataObject()->getEmail() !== $email
81  && !$this->customerAccountManagement->isEmailAvailable($email, $websiteId)
82  ) {
83  throw new LocalizedException(
84  __('This email address is already assigned to another user.')
85  );
86  }
87  }
88 
95  protected function validateGuestSubscription()
96  {
97  if ($this->_objectManager->get(ScopeConfigInterface::class)
98  ->getValue(
99  Subscriber::XML_PATH_ALLOW_GUEST_SUBSCRIBE_FLAG,
100  ScopeInterface::SCOPE_STORE
101  ) != 1
102  && !$this->_customerSession->isLoggedIn()
103  ) {
104  throw new LocalizedException(
105  __(
106  'Sorry, but the administrator denied subscription for guests. Please <a href="%1">register</a>.',
107  $this->_customerUrl->getRegisterUrl()
108  )
109  );
110  }
111  }
112 
120  protected function validateEmailFormat($email)
121  {
122  if (!$this->emailValidator->isValid($email)) {
123  throw new LocalizedException(__('Please enter a valid email address.'));
124  }
125  }
126 
132  public function execute()
133  {
134  if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')) {
135  $email = (string)$this->getRequest()->getPost('email');
136 
137  try {
138  $this->validateEmailFormat($email);
139  $this->validateGuestSubscription();
141 
142  $subscriber = $this->_subscriberFactory->create()->loadByEmail($email);
143  if ($subscriber->getId()
144  && (int) $subscriber->getSubscriberStatus() === Subscriber::STATUS_SUBSCRIBED
145  ) {
146  throw new LocalizedException(
147  __('This email address is already subscribed.')
148  );
149  }
150 
151  $status = (int) $this->_subscriberFactory->create()->subscribe($email);
152  $this->messageManager->addSuccessMessage($this->getSuccessMessage($status));
153  } catch (LocalizedException $e) {
154  $this->messageManager->addExceptionMessage(
155  $e,
156  __('There was a problem with the subscription: %1', $e->getMessage())
157  );
158  } catch (\Exception $e) {
159  $this->messageManager->addExceptionMessage($e, __('Something went wrong with the subscription.'));
160  }
161  }
162  $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
163  }
164 
169  private function getSuccessMessage(int $status): Phrase
170  {
171  if ($status === Subscriber::STATUS_NOT_ACTIVE) {
172  return __('The confirmation request has been sent.');
173  }
174 
175  return __('Thank you for your subscription.');
176  }
177 }
_redirect($path, $arguments=[])
Definition: Action.php:167
$email
Definition: details.phtml:13
$customerUrl
Definition: info.phtml:28
$storeManager
__()
Definition: __.php:13
$status
Definition: order_status.php:8
$subscriber
Definition: subscribers.php:20
__construct(Context $context, SubscriberFactory $subscriberFactory, Session $customerSession, StoreManagerInterface $storeManager, CustomerUrl $customerUrl, CustomerAccountManagement $customerAccountManagement, EmailValidator $emailValidator=null)
Definition: NewAction.php:50