44 * Copyright © Magento, Inc. All rights reserved.
55 * See COPYING.txt for license details.
66 */
7+ declare (strict_types=1 );
8+
79namespace Magento \Newsletter \Controller \Subscriber ;
810
911use Magento \Customer \Api \AccountManagementInterface as CustomerAccountManagement ;
2325use Magento \Newsletter \Model \SubscriptionManagerInterface ;
2426use Magento \Store \Model \ScopeInterface ;
2527use Magento \Store \Model \StoreManagerInterface ;
28+ use Magento \Newsletter \Model \Config as NewsletterConfig ;
2629use Magento \Newsletter \Model \SubscriberFactory ;
2730
2831/**
@@ -42,6 +45,11 @@ class NewAction extends SubscriberController implements HttpPostActionInterface
4245 */
4346 private $ emailValidator ;
4447
48+ /**
49+ * @var NewsletterConfig
50+ */
51+ private $ newsletterConfig ;
52+
4553 /**
4654 * @var SubscriptionManagerInterface
4755 */
@@ -57,7 +65,8 @@ class NewAction extends SubscriberController implements HttpPostActionInterface
5765 * @param CustomerUrl $customerUrl
5866 * @param CustomerAccountManagement $customerAccountManagement
5967 * @param SubscriptionManagerInterface $subscriptionManager
60- * @param EmailValidator $emailValidator
68+ * @param EmailValidator|null $emailValidator
69+ * @param NewsletterConfig|null $newsletterConfig
6170 */
6271 public function __construct (
6372 Context $ context ,
@@ -67,11 +76,13 @@ public function __construct(
6776 CustomerUrl $ customerUrl ,
6877 CustomerAccountManagement $ customerAccountManagement ,
6978 SubscriptionManagerInterface $ subscriptionManager ,
70- EmailValidator $ emailValidator = null
79+ EmailValidator $ emailValidator = null ,
80+ NewsletterConfig $ newsletterConfig = null
7181 ) {
7282 $ this ->customerAccountManagement = $ customerAccountManagement ;
7383 $ this ->subscriptionManager = $ subscriptionManager ;
7484 $ this ->emailValidator = $ emailValidator ?: ObjectManager::getInstance ()->get (EmailValidator::class);
85+ $ this ->newsletterConfig = $ newsletterConfig ?: ObjectManager::getInstance ()->get (NewsletterConfig::class);
7586 parent ::__construct (
7687 $ context ,
7788 $ subscriberFactory ,
@@ -85,8 +96,9 @@ public function __construct(
8596 * Validates that the email address isn't being used by a different account.
8697 *
8798 * @param string $email
88- * @throws LocalizedException
99+ *
89100 * @return void
101+ * @throws LocalizedException
90102 */
91103 protected function validateEmailAvailable ($ email )
92104 {
@@ -129,8 +141,9 @@ protected function validateGuestSubscription()
129141 * Validates the format of the email address
130142 *
131143 * @param string $email
132- * @throws LocalizedException
144+ *
133145 * @return void
146+ * @throws LocalizedException
134147 */
135148 protected function validateEmailFormat ($ email )
136149 {
@@ -146,7 +159,10 @@ protected function validateEmailFormat($email)
146159 */
147160 public function execute ()
148161 {
149- if ($ this ->getRequest ()->isPost () && $ this ->getRequest ()->getPost ('email ' )) {
162+ if ($ this ->getRequest ()->isPost ()
163+ && $ this ->getRequest ()->getPost ('email ' )
164+ && $ this ->newsletterConfig ->isActive ()
165+ ) {
150166 $ email = (string )$ this ->getRequest ()->getPost ('email ' );
151167
152168 try {
@@ -183,13 +199,15 @@ public function execute()
183199 /** @var Redirect $redirect */
184200 $ redirect = $ this ->resultFactory ->create (ResultFactory::TYPE_REDIRECT );
185201 $ redirectUrl = $ this ->_redirect ->getRedirectUrl ();
202+
186203 return $ redirect ->setUrl ($ redirectUrl );
187204 }
188205
189206 /**
190207 * Get customer id from session if he is owner of the email
191208 *
192209 * @param string $email
210+ *
193211 * @return int|null
194212 */
195213 private function getSessionCustomerId (string $ email ): ?int
@@ -210,6 +228,7 @@ private function getSessionCustomerId(string $email): ?int
210228 * Get success message
211229 *
212230 * @param int $status
231+ *
213232 * @return Phrase
214233 */
215234 private function getSuccessMessage (int $ status ): Phrase
0 commit comments