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 ;
24
26
use Magento \Newsletter \Model \SubscriptionManagerInterface ;
25
27
use Magento \Store \Model \ScopeInterface ;
26
28
use Magento \Store \Model \StoreManagerInterface ;
29
+ use Magento \Newsletter \Model \Config as NewsletterConfig ;
27
30
use Magento \Newsletter \Model \SubscriberFactory ;
28
31
29
32
/**
@@ -43,6 +46,11 @@ class NewAction extends SubscriberController implements HttpPostActionInterface
43
46
*/
44
47
private $ emailValidator ;
45
48
49
+ /**
50
+ * @var NewsletterConfig
51
+ */
52
+ private $ newsletterConfig ;
53
+
46
54
/**
47
55
* @var SubscriptionManagerInterface
48
56
*/
@@ -65,6 +73,8 @@ class NewAction extends SubscriberController implements HttpPostActionInterface
65
73
* @param SubscriptionManagerInterface $subscriptionManager
66
74
* @param EmailValidator|null $emailValidator
67
75
* @param CustomerRepositoryInterface|null $customerRepository
76
+ * @param NewsletterConfig|null $newsletterConfig
77
+ * @SuppressWarnings(PHPMD.ExcessiveParameterList)
68
78
*/
69
79
public function __construct (
70
80
Context $ context ,
@@ -75,13 +85,16 @@ public function __construct(
75
85
CustomerAccountManagement $ customerAccountManagement ,
76
86
SubscriptionManagerInterface $ subscriptionManager ,
77
87
EmailValidator $ emailValidator = null ,
78
- CustomerRepositoryInterface $ customerRepository = null
88
+ CustomerRepositoryInterface $ customerRepository = null ,
89
+ NewsletterConfig $ newsletterConfig = null
79
90
) {
80
91
$ this ->customerAccountManagement = $ customerAccountManagement ;
81
92
$ this ->subscriptionManager = $ subscriptionManager ;
82
93
$ this ->emailValidator = $ emailValidator ?: ObjectManager::getInstance ()->get (EmailValidator::class);
83
94
$ this ->customerRepository = $ customerRepository ?: ObjectManager::getInstance ()
84
95
->get (CustomerRepositoryInterface::class);
96
+ $ this ->newsletterConfig = $ newsletterConfig ?: ObjectManager::getInstance ()
97
+ ->get (NewsletterConfig::class);
85
98
parent ::__construct (
86
99
$ context ,
87
100
$ subscriberFactory ,
@@ -95,8 +108,9 @@ public function __construct(
95
108
* Validates that the email address isn't being used by a different account.
96
109
*
97
110
* @param string $email
98
- * @throws LocalizedException
111
+ *
99
112
* @return void
113
+ * @throws LocalizedException
100
114
*/
101
115
protected function validateEmailAvailable ($ email )
102
116
{
@@ -139,8 +153,9 @@ protected function validateGuestSubscription()
139
153
* Validates the format of the email address
140
154
*
141
155
* @param string $email
142
- * @throws LocalizedException
156
+ *
143
157
* @return void
158
+ * @throws LocalizedException
144
159
*/
145
160
protected function validateEmailFormat ($ email )
146
161
{
@@ -156,7 +171,10 @@ protected function validateEmailFormat($email)
156
171
*/
157
172
public function execute ()
158
173
{
159
- if ($ this ->getRequest ()->isPost () && $ this ->getRequest ()->getPost ('email ' )) {
174
+ if ($ this ->getRequest ()->isPost ()
175
+ && $ this ->getRequest ()->getPost ('email ' )
176
+ && $ this ->newsletterConfig ->isActive ()
177
+ ) {
160
178
$ email = (string )$ this ->getRequest ()->getPost ('email ' );
161
179
162
180
try {
@@ -195,6 +213,7 @@ public function execute()
195
213
$ redirect = $ this ->resultFactory ->create (ResultFactory::TYPE_REDIRECT );
196
214
// phpcs:ignore Magento2.Legacy.ObsoleteResponse
197
215
$ redirectUrl = $ this ->_redirect ->getRedirectUrl ();
216
+
198
217
return $ redirect ->setUrl ($ redirectUrl );
199
218
}
200
219
@@ -203,6 +222,7 @@ public function execute()
203
222
*
204
223
* @param string $email
205
224
* @param int $websiteId
225
+ *
206
226
* @return int|null
207
227
*/
208
228
private function getCustomerId (string $ email , int $ websiteId ): ?int
@@ -219,6 +239,7 @@ private function getCustomerId(string $email, int $websiteId): ?int
219
239
* Get success message
220
240
*
221
241
* @param int $status
242
+ *
222
243
* @return Phrase
223
244
*/
224
245
private function getSuccessMessage (int $ status ): Phrase
0 commit comments