Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Confirm.php
Go to the documentation of this file.
1 <?php
8 
21 
28 {
32  protected $scopeConfig;
33 
37  protected $storeManager;
38 
43 
48 
52  protected $addressHelper;
53 
57  protected $urlModel;
58 
62  protected $session;
63 
67  private $cookieMetadataFactory;
68 
72  private $cookieMetadataManager;
73 
84  public function __construct(
85  Context $context,
86  Session $customerSession,
92  UrlFactory $urlFactory
93  ) {
94  $this->session = $customerSession;
95  $this->scopeConfig = $scopeConfig;
96  $this->storeManager = $storeManager;
97  $this->customerAccountManagement = $customerAccountManagement;
98  $this->customerRepository = $customerRepository;
99  $this->addressHelper = $addressHelper;
100  $this->urlModel = $urlFactory->create();
101  parent::__construct($context);
102  }
103 
110  private function getCookieManager()
111  {
112  if (!$this->cookieMetadataManager) {
113  $this->cookieMetadataManager = \Magento\Framework\App\ObjectManager::getInstance()->get(
114  \Magento\Framework\Stdlib\Cookie\PhpCookieManager::class
115  );
116  }
117  return $this->cookieMetadataManager;
118  }
119 
126  private function getCookieMetadataFactory()
127  {
128  if (!$this->cookieMetadataFactory) {
129  $this->cookieMetadataFactory = \Magento\Framework\App\ObjectManager::getInstance()->get(
130  \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class
131  );
132  }
133  return $this->cookieMetadataFactory;
134  }
135 
141  public function execute()
142  {
144  $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
145 
146  if ($this->session->isLoggedIn()) {
147  $resultRedirect->setPath('*/*/');
148  return $resultRedirect;
149  }
150  try {
151  $customerId = $this->getRequest()->getParam('id', false);
152  $key = $this->getRequest()->getParam('key', false);
153  if (empty($customerId) || empty($key)) {
154  throw new \Exception(__('Bad request.'));
155  }
156 
157  // log in and send greeting email
158  $customerEmail = $this->customerRepository->getById($customerId)->getEmail();
159  $customer = $this->customerAccountManagement->activate($customerEmail, $key);
160  $this->session->setCustomerDataAsLoggedIn($customer);
161  if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
162  $metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
163  $metadata->setPath('/');
164  $this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);
165  }
166  $this->messageManager->addSuccess($this->getSuccessMessage());
167  $resultRedirect->setUrl($this->getSuccessRedirect());
168  return $resultRedirect;
169  } catch (StateException $e) {
170  $this->messageManager->addException($e, __('This confirmation key is invalid or has expired.'));
171  } catch (\Exception $e) {
172  $this->messageManager->addException($e, __('There was an error confirming the account'));
173  }
174 
175  $url = $this->urlModel->getUrl('*/*/index', ['_secure' => true]);
176  return $resultRedirect->setUrl($this->_redirect->error($url));
177  }
178 
184  protected function getSuccessMessage()
185  {
186  if ($this->addressHelper->isVatValidationEnabled()) {
187  if ($this->addressHelper->getTaxCalculationAddressType() == Address::TYPE_SHIPPING) {
188  // @codingStandardsIgnoreStart
189  $message = __(
190  'If you are a registered VAT customer, please click <a href="%1">here</a> to enter your shipping address for proper VAT calculation.',
191  $this->urlModel->getUrl('customer/address/edit')
192  );
193  // @codingStandardsIgnoreEnd
194  } else {
195  // @codingStandardsIgnoreStart
196  $message = __(
197  'If you are a registered VAT customer, please click <a href="%1">here</a> to enter your billing address for proper VAT calculation.',
198  $this->urlModel->getUrl('customer/address/edit')
199  );
200  // @codingStandardsIgnoreEnd
201  }
202  } else {
203  $message = __('Thank you for registering with %1.', $this->storeManager->getStore()->getFrontendName());
204  }
205  return $message;
206  }
207 
213  protected function getSuccessRedirect()
214  {
215  $backUrl = $this->getRequest()->getParam('back_url', false);
216  $redirectToDashboard = $this->scopeConfig->isSetFlag(
219  );
220  if (!$redirectToDashboard && $this->session->getBeforeAuthUrl()) {
221  $successUrl = $this->session->getBeforeAuthUrl(true);
222  } else {
223  $successUrl = $this->urlModel->getUrl('*/*/index', ['_secure' => true]);
224  }
225  return $this->_redirect->success($backUrl ? $backUrl : $successUrl);
226  }
227 }
_redirect($path, $arguments=[])
Definition: Action.php:167
$customer
Definition: customers.php:11
__()
Definition: __.php:13
$message
const XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD
Definition: Url.php:27
__construct(Context $context, Session $customerSession, ScopeConfigInterface $scopeConfig, StoreManagerInterface $storeManager, AccountManagementInterface $customerAccountManagement, CustomerRepositoryInterface $customerRepository, Address $addressHelper, UrlFactory $urlFactory)
Definition: Confirm.php:84