10
10
use Magento \Customer \Model \Session ;
11
11
use Magento \Customer \Model \Url as CustomerUrl ;
12
12
use Magento \Framework \App \Action \Context ;
13
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
14
+ use Magento \Framework \App \ObjectManager ;
15
+ use Magento \Framework \Exception \LocalizedException ;
16
+ use Magento \Framework \Phrase ;
17
+ use Magento \Framework \Validator \EmailAddress as EmailValidator ;
18
+ use Magento \Newsletter \Controller \Subscriber as SubscriberController ;
19
+ use Magento \Newsletter \Model \Subscriber ;
20
+ use Magento \Store \Model \ScopeInterface ;
13
21
use Magento \Store \Model \StoreManagerInterface ;
14
22
use Magento \Newsletter \Model \SubscriberFactory ;
15
23
16
24
/**
17
25
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
18
26
*/
19
- class NewAction extends \ Magento \ Newsletter \ Controller \Subscriber
27
+ class NewAction extends SubscriberController
20
28
{
21
29
/**
22
30
* @var CustomerAccountManagement
23
31
*/
24
32
protected $ customerAccountManagement ;
25
33
34
+ /**
35
+ * @var EmailValidator
36
+ */
37
+ private $ emailValidator ;
38
+
26
39
/**
27
40
* Initialize dependencies.
28
41
*
@@ -32,16 +45,19 @@ class NewAction extends \Magento\Newsletter\Controller\Subscriber
32
45
* @param StoreManagerInterface $storeManager
33
46
* @param CustomerUrl $customerUrl
34
47
* @param CustomerAccountManagement $customerAccountManagement
48
+ * @param EmailValidator $emailValidator
35
49
*/
36
50
public function __construct (
37
51
Context $ context ,
38
52
SubscriberFactory $ subscriberFactory ,
39
53
Session $ customerSession ,
40
54
StoreManagerInterface $ storeManager ,
41
55
CustomerUrl $ customerUrl ,
42
- CustomerAccountManagement $ customerAccountManagement
56
+ CustomerAccountManagement $ customerAccountManagement ,
57
+ EmailValidator $ emailValidator = null
43
58
) {
44
59
$ this ->customerAccountManagement = $ customerAccountManagement ;
60
+ $ this ->emailValidator = $ emailValidator ?: ObjectManager::getInstance ()->get (EmailValidator::class);
45
61
parent ::__construct (
46
62
$ context ,
47
63
$ subscriberFactory ,
@@ -55,7 +71,7 @@ public function __construct(
55
71
* Validates that the email address isn't being used by a different account.
56
72
*
57
73
* @param string $email
58
- * @throws \Magento\Framework\Exception\ LocalizedException
74
+ * @throws LocalizedException
59
75
* @return void
60
76
*/
61
77
protected function validateEmailAvailable ($ email )
@@ -64,7 +80,7 @@ protected function validateEmailAvailable($email)
64
80
if ($ this ->_customerSession ->getCustomerDataObject ()->getEmail () !== $ email
65
81
&& !$ this ->customerAccountManagement ->isEmailAvailable ($ email , $ websiteId )
66
82
) {
67
- throw new \ Magento \ Framework \ Exception \ LocalizedException (
83
+ throw new LocalizedException (
68
84
__ ('This email address is already assigned to another user. ' )
69
85
);
70
86
}
@@ -73,19 +89,19 @@ protected function validateEmailAvailable($email)
73
89
/**
74
90
* Validates that if the current user is a guest, that they can subscribe to a newsletter.
75
91
*
76
- * @throws \Magento\Framework\Exception\ LocalizedException
92
+ * @throws LocalizedException
77
93
* @return void
78
94
*/
79
95
protected function validateGuestSubscription ()
80
96
{
81
- if ($ this ->_objectManager ->get (\ Magento \ Framework \ App \ Config \ ScopeConfigInterface::class)
97
+ if ($ this ->_objectManager ->get (ScopeConfigInterface::class)
82
98
->getValue (
83
- \ Magento \ Newsletter \ Model \ Subscriber::XML_PATH_ALLOW_GUEST_SUBSCRIBE_FLAG ,
84
- \ Magento \ Store \ Model \ ScopeInterface::SCOPE_STORE
99
+ Subscriber::XML_PATH_ALLOW_GUEST_SUBSCRIBE_FLAG ,
100
+ ScopeInterface::SCOPE_STORE
85
101
) != 1
86
102
&& !$ this ->_customerSession ->isLoggedIn ()
87
103
) {
88
- throw new \ Magento \ Framework \ Exception \ LocalizedException (
104
+ throw new LocalizedException (
89
105
__ (
90
106
'Sorry, but the administrator denied subscription for guests. Please <a href="%1">register</a>. ' ,
91
107
$ this ->_customerUrl ->getRegisterUrl ()
@@ -98,20 +114,19 @@ protected function validateGuestSubscription()
98
114
* Validates the format of the email address
99
115
*
100
116
* @param string $email
101
- * @throws \Magento\Framework\Exception\ LocalizedException
117
+ * @throws LocalizedException
102
118
* @return void
103
119
*/
104
120
protected function validateEmailFormat ($ email )
105
121
{
106
- if (!\Zend_Validate:: is ($ email, \ Magento \ Framework \ Validator \EmailAddress::class )) {
107
- throw new \ Magento \ Framework \ Exception \ LocalizedException (__ ('Please enter a valid email address. ' ));
122
+ if (!$ this -> emailValidator -> isValid ($ email )) {
123
+ throw new LocalizedException (__ ('Please enter a valid email address. ' ));
108
124
}
109
125
}
110
126
111
127
/**
112
128
* New subscription action
113
129
*
114
- * @throws \Magento\Framework\Exception\LocalizedException
115
130
* @return void
116
131
*/
117
132
public function execute ()
@@ -126,28 +141,37 @@ public function execute()
126
141
127
142
$ subscriber = $ this ->_subscriberFactory ->create ()->loadByEmail ($ email );
128
143
if ($ subscriber ->getId ()
129
- && $ subscriber ->getSubscriberStatus () == \ Magento \ Newsletter \ Model \ Subscriber::STATUS_SUBSCRIBED
144
+ && ( int ) $ subscriber ->getSubscriberStatus () === Subscriber::STATUS_SUBSCRIBED
130
145
) {
131
- throw new \ Magento \ Framework \ Exception \ LocalizedException (
146
+ throw new LocalizedException (
132
147
__ ('This email address is already subscribed. ' )
133
148
);
134
149
}
135
150
136
- $ status = $ this ->_subscriberFactory ->create ()->subscribe ($ email );
137
- if ($ status == \Magento \Newsletter \Model \Subscriber::STATUS_NOT_ACTIVE ) {
138
- $ this ->messageManager ->addSuccess (__ ('The confirmation request has been sent. ' ));
139
- } else {
140
- $ this ->messageManager ->addSuccess (__ ('Thank you for your subscription. ' ));
141
- }
142
- } catch (\Magento \Framework \Exception \LocalizedException $ e ) {
143
- $ this ->messageManager ->addException (
151
+ $ status = (int ) $ this ->_subscriberFactory ->create ()->subscribe ($ email );
152
+ $ this ->messageManager ->addSuccessMessage ($ this ->getSuccessMessage ($ status ));
153
+ } catch (LocalizedException $ e ) {
154
+ $ this ->messageManager ->addExceptionMessage (
144
155
$ e ,
145
156
__ ('There was a problem with the subscription: %1 ' , $ e ->getMessage ())
146
157
);
147
158
} catch (\Exception $ e ) {
148
- $ this ->messageManager ->addException ($ e , __ ('Something went wrong with the subscription. ' ));
159
+ $ this ->messageManager ->addExceptionMessage ($ e , __ ('Something went wrong with the subscription. ' ));
149
160
}
150
161
}
151
162
$ this ->getResponse ()->setRedirect ($ this ->_redirect ->getRedirectUrl ());
152
163
}
164
+
165
+ /**
166
+ * @param int $status
167
+ * @return Phrase
168
+ */
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
+ }
153
177
}
0 commit comments