3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Customer \Controller \Account ;
7
9
8
10
use Magento \Customer \Api \AccountManagementInterface ;
13
15
use Magento \Framework \App \Action \HttpPostActionInterface as HttpPostActionInterface ;
14
16
use Magento \Framework \App \Action \Context ;
15
17
use Magento \Framework \App \ObjectManager ;
18
+ use Magento \Framework \Controller \Result \Redirect ;
19
+ use Magento \Framework \Exception \LocalizedException ;
20
+ use Magento \Framework \Exception \NoSuchEntityException ;
16
21
use Magento \Framework \Exception \State \InvalidTransitionException ;
22
+ use Magento \Framework \View \Result \Page ;
17
23
use Magento \Framework \View \Result \PageFactory ;
18
24
use Magento \Store \Model \StoreManagerInterface ;
19
25
20
26
/**
21
- * Class Confirmation. Send confirmation link to specified email
27
+ * Send confirmation link to specified email
22
28
*/
23
29
class Confirmation extends AbstractAccount implements HttpGetActionInterface, HttpPostActionInterface
24
30
{
25
31
/**
26
- * @var \Magento\Store\Model\ StoreManagerInterface
32
+ * @var StoreManagerInterface
27
33
*/
28
34
protected $ storeManager ;
29
35
30
36
/**
31
- * @var \Magento\Customer\Api\ AccountManagementInterface
37
+ * @var AccountManagementInterface
32
38
*/
33
39
protected $ customerAccountManagement ;
34
40
@@ -53,7 +59,7 @@ class Confirmation extends AbstractAccount implements HttpGetActionInterface, Ht
53
59
* @param PageFactory $resultPageFactory
54
60
* @param StoreManagerInterface $storeManager
55
61
* @param AccountManagementInterface $customerAccountManagement
56
- * @param Url $customerUrl
62
+ * @param Url|null $customerUrl
57
63
*/
58
64
public function __construct (
59
65
Context $ context ,
@@ -74,48 +80,52 @@ public function __construct(
74
80
/**
75
81
* Send confirmation link to specified email
76
82
*
77
- * @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\View\Result\Page
83
+ * @return Redirect|Page
84
+ * @throws LocalizedException
78
85
*/
79
86
public function execute ()
80
87
{
81
88
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 ('*/*/ ' );
86
90
}
87
91
88
- // try to confirm by email
89
92
$ email = $ this ->getRequest ()->getPost ('email ' );
90
- if ($ email ) {
91
- /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
92
- $ resultRedirect = $ this ->resultRedirectFactory ->create ();
93
93
94
+ if ($ email ) {
94
95
try {
95
96
$ this ->customerAccountManagement ->resendConfirmation (
96
97
$ email ,
97
98
$ this ->storeManager ->getStore ()->getWebsiteId ()
98
99
);
99
100
$ this ->messageManager ->addSuccessMessage (__ ('Please check your email for confirmation key. ' ));
101
+ return $ this ->getRedirect ('*/*/index ' , ['_secure ' => true ]);
100
102
} catch (InvalidTransitionException $ e ) {
101
103
$ 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. ' ));
106
107
}
107
- $ this ->session ->setUsername ($ email );
108
- $ resultRedirect ->setPath ('*/*/index ' , ['_secure ' => true ]);
109
- return $ resultRedirect ;
110
108
}
111
109
112
- /** @var \Magento\Framework\View\Result\Page $resultPage */
113
110
$ 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 ());
119
114
return $ resultPage ;
120
115
}
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
+ }
121
131
}
0 commit comments