33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
7+
68namespace Magento \Customer \Controller \Account ;
79
810use Magento \Customer \Api \AccountManagementInterface ;
1315use Magento \Framework \App \Action \HttpPostActionInterface as HttpPostActionInterface ;
1416use Magento \Framework \App \Action \Context ;
1517use Magento \Framework \App \ObjectManager ;
18+ use Magento \Framework \Controller \Result \Redirect ;
19+ use Magento \Framework \Exception \LocalizedException ;
20+ use Magento \Framework \Exception \NoSuchEntityException ;
1621use Magento \Framework \Exception \State \InvalidTransitionException ;
22+ use Magento \Framework \View \Result \Page ;
1723use Magento \Framework \View \Result \PageFactory ;
1824use Magento \Store \Model \StoreManagerInterface ;
1925
2026/**
21- * Class Confirmation. Send confirmation link to specified email
27+ * Send confirmation link to specified email
2228 */
2329class Confirmation extends AbstractAccount implements HttpGetActionInterface, HttpPostActionInterface
2430{
2531 /**
26- * @var \Magento\Store\Model\ StoreManagerInterface
32+ * @var StoreManagerInterface
2733 */
2834 protected $ storeManager ;
2935
3036 /**
31- * @var \Magento\Customer\Api\ AccountManagementInterface
37+ * @var AccountManagementInterface
3238 */
3339 protected $ customerAccountManagement ;
3440
@@ -53,7 +59,7 @@ class Confirmation extends AbstractAccount implements HttpGetActionInterface, Ht
5359 * @param PageFactory $resultPageFactory
5460 * @param StoreManagerInterface $storeManager
5561 * @param AccountManagementInterface $customerAccountManagement
56- * @param Url $customerUrl
62+ * @param Url|null $customerUrl
5763 */
5864 public function __construct (
5965 Context $ context ,
@@ -74,48 +80,52 @@ public function __construct(
7480 /**
7581 * Send confirmation link to specified email
7682 *
77- * @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\View\Result\Page
83+ * @return Redirect|Page
84+ * @throws LocalizedException
7885 */
7986 public function execute ()
8087 {
8188 if ($ this ->session ->isLoggedIn ()) {
82- /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
83- $ resultRedirect = $ this ->resultRedirectFactory ->create ();
84- $ resultRedirect ->setPath ('*/*/ ' );
85- return $ resultRedirect ;
89+ return $ this ->getRedirect ('*/*/ ' );
8690 }
8791
88- // try to confirm by email
8992 $ email = $ this ->getRequest ()->getPost ('email ' );
90- if ($ email ) {
91- /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
92- $ resultRedirect = $ this ->resultRedirectFactory ->create ();
9393
94+ if ($ email ) {
9495 try {
9596 $ this ->customerAccountManagement ->resendConfirmation (
9697 $ email ,
9798 $ this ->storeManager ->getStore ()->getWebsiteId ()
9899 );
99100 $ this ->messageManager ->addSuccessMessage (__ ('Please check your email for confirmation key. ' ));
101+ return $ this ->getRedirect ('*/*/index ' , ['_secure ' => true ]);
100102 } catch (InvalidTransitionException $ e ) {
101103 $ this ->messageManager ->addSuccessMessage (__ ('This email does not require confirmation. ' ));
102- } catch (\Exception $ e ) {
103- $ this ->messageManager ->addExceptionMessage ($ e , __ ('Wrong email. ' ));
104- $ resultRedirect ->setPath ('*/*/* ' , ['email ' => $ email , '_secure ' => true ]);
105- return $ resultRedirect ;
104+ return $ this ->getRedirect ('*/*/index ' , ['_secure ' => true ]);
105+ } catch (NoSuchEntityException $ e ) {
106+ $ this ->messageManager ->addErrorMessage (__ ('Wrong email. ' ));
106107 }
107- $ this ->session ->setUsername ($ email );
108- $ resultRedirect ->setPath ('*/*/index ' , ['_secure ' => true ]);
109- return $ resultRedirect ;
110108 }
111109
112- /** @var \Magento\Framework\View\Result\Page $resultPage */
113110 $ resultPage = $ this ->resultPageFactory ->create ();
114- $ resultPage ->getLayout ()->getBlock ('accountConfirmation ' )->setEmail (
115- $ this ->getRequest ()->getParam ('email ' , $ email )
116- )->setLoginUrl (
117- $ this ->customerUrl ->getLoginUrl ()
118- );
111+ $ resultPage ->getLayout ()->getBlock ('accountConfirmation ' )
112+ ->setEmail ($ email )
113+ ->setLoginUrl ($ this ->customerUrl ->getLoginUrl ());
119114 return $ resultPage ;
120115 }
116+
117+ /**
118+ * Returns redirect object
119+ *
120+ * @param string $path
121+ * @param array $params
122+ * @return Redirect
123+ */
124+ private function getRedirect (string $ path , array $ params = []): Redirect
125+ {
126+ $ resultRedirect = $ this ->resultRedirectFactory ->create ();
127+ $ resultRedirect ->setPath ($ path , $ params );
128+
129+ return $ resultRedirect ;
130+ }
121131}
0 commit comments