4
4
* Copyright © Magento, Inc. All rights reserved.
5
5
* See COPYING.txt for license details.
6
6
*/
7
+ declare (strict_types=1 );
8
+
7
9
namespace Magento \Newsletter \Controller \Subscriber ;
8
10
9
11
use Magento \Customer \Api \AccountManagementInterface as CustomerAccountManagement ;
23
25
use Magento \Newsletter \Model \SubscriptionManagerInterface ;
24
26
use Magento \Store \Model \ScopeInterface ;
25
27
use Magento \Store \Model \StoreManagerInterface ;
28
+ use Magento \Newsletter \Model \Config as NewsletterConfig ;
26
29
use Magento \Newsletter \Model \SubscriberFactory ;
27
30
28
31
/**
@@ -42,6 +45,11 @@ class NewAction extends SubscriberController implements HttpPostActionInterface
42
45
*/
43
46
private $ emailValidator ;
44
47
48
+ /**
49
+ * @var NewsletterConfig
50
+ */
51
+ private $ newsletterConfig ;
52
+
45
53
/**
46
54
* @var SubscriptionManagerInterface
47
55
*/
@@ -57,7 +65,8 @@ class NewAction extends SubscriberController implements HttpPostActionInterface
57
65
* @param CustomerUrl $customerUrl
58
66
* @param CustomerAccountManagement $customerAccountManagement
59
67
* @param SubscriptionManagerInterface $subscriptionManager
60
- * @param EmailValidator $emailValidator
68
+ * @param EmailValidator|null $emailValidator
69
+ * @param NewsletterConfig|null $newsletterConfig
61
70
*/
62
71
public function __construct (
63
72
Context $ context ,
@@ -67,11 +76,13 @@ public function __construct(
67
76
CustomerUrl $ customerUrl ,
68
77
CustomerAccountManagement $ customerAccountManagement ,
69
78
SubscriptionManagerInterface $ subscriptionManager ,
70
- EmailValidator $ emailValidator = null
79
+ EmailValidator $ emailValidator = null ,
80
+ NewsletterConfig $ newsletterConfig = null
71
81
) {
72
82
$ this ->customerAccountManagement = $ customerAccountManagement ;
73
83
$ this ->subscriptionManager = $ subscriptionManager ;
74
84
$ this ->emailValidator = $ emailValidator ?: ObjectManager::getInstance ()->get (EmailValidator::class);
85
+ $ this ->newsletterConfig = $ newsletterConfig ?: ObjectManager::getInstance ()->get (NewsletterConfig::class);
75
86
parent ::__construct (
76
87
$ context ,
77
88
$ subscriberFactory ,
@@ -85,8 +96,9 @@ public function __construct(
85
96
* Validates that the email address isn't being used by a different account.
86
97
*
87
98
* @param string $email
88
- * @throws LocalizedException
99
+ *
89
100
* @return void
101
+ * @throws LocalizedException
90
102
*/
91
103
protected function validateEmailAvailable ($ email )
92
104
{
@@ -129,8 +141,9 @@ protected function validateGuestSubscription()
129
141
* Validates the format of the email address
130
142
*
131
143
* @param string $email
132
- * @throws LocalizedException
144
+ *
133
145
* @return void
146
+ * @throws LocalizedException
134
147
*/
135
148
protected function validateEmailFormat ($ email )
136
149
{
@@ -146,7 +159,10 @@ protected function validateEmailFormat($email)
146
159
*/
147
160
public function execute ()
148
161
{
149
- if ($ this ->getRequest ()->isPost () && $ this ->getRequest ()->getPost ('email ' )) {
162
+ if ($ this ->getRequest ()->isPost ()
163
+ && $ this ->getRequest ()->getPost ('email ' )
164
+ && $ this ->newsletterConfig ->isActive ()
165
+ ) {
150
166
$ email = (string )$ this ->getRequest ()->getPost ('email ' );
151
167
152
168
try {
@@ -183,13 +199,15 @@ public function execute()
183
199
/** @var Redirect $redirect */
184
200
$ redirect = $ this ->resultFactory ->create (ResultFactory::TYPE_REDIRECT );
185
201
$ redirectUrl = $ this ->_redirect ->getRedirectUrl ();
202
+
186
203
return $ redirect ->setUrl ($ redirectUrl );
187
204
}
188
205
189
206
/**
190
207
* Get customer id from session if he is owner of the email
191
208
*
192
209
* @param string $email
210
+ *
193
211
* @return int|null
194
212
*/
195
213
private function getSessionCustomerId (string $ email ): ?int
@@ -210,6 +228,7 @@ private function getSessionCustomerId(string $email): ?int
210
228
* Get success message
211
229
*
212
230
* @param int $status
231
+ *
213
232
* @return Phrase
214
233
*/
215
234
private function getSuccessMessage (int $ status ): Phrase
0 commit comments