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 ;
2426use Magento \Newsletter \Model \SubscriptionManagerInterface ;
2527use Magento \Store \Model \ScopeInterface ;
2628use Magento \Store \Model \StoreManagerInterface ;
29+ use Magento \Newsletter \Model \Config as NewsletterConfig ;
2730use Magento \Newsletter \Model \SubscriberFactory ;
2831
2932/**
@@ -43,6 +46,11 @@ class NewAction extends SubscriberController implements HttpPostActionInterface
4346 */
4447 private $ emailValidator ;
4548
49+ /**
50+ * @var NewsletterConfig
51+ */
52+ private $ newsletterConfig ;
53+
4654 /**
4755 * @var SubscriptionManagerInterface
4856 */
@@ -65,6 +73,8 @@ class NewAction extends SubscriberController implements HttpPostActionInterface
6573 * @param SubscriptionManagerInterface $subscriptionManager
6674 * @param EmailValidator|null $emailValidator
6775 * @param CustomerRepositoryInterface|null $customerRepository
76+ * @param NewsletterConfig|null $newsletterConfig
77+ * @SuppressWarnings(PHPMD.ExcessiveParameterList)
6878 */
6979 public function __construct (
7080 Context $ context ,
@@ -75,13 +85,16 @@ public function __construct(
7585 CustomerAccountManagement $ customerAccountManagement ,
7686 SubscriptionManagerInterface $ subscriptionManager ,
7787 EmailValidator $ emailValidator = null ,
78- CustomerRepositoryInterface $ customerRepository = null
88+ CustomerRepositoryInterface $ customerRepository = null ,
89+ NewsletterConfig $ newsletterConfig = null
7990 ) {
8091 $ this ->customerAccountManagement = $ customerAccountManagement ;
8192 $ this ->subscriptionManager = $ subscriptionManager ;
8293 $ this ->emailValidator = $ emailValidator ?: ObjectManager::getInstance ()->get (EmailValidator::class);
8394 $ this ->customerRepository = $ customerRepository ?: ObjectManager::getInstance ()
8495 ->get (CustomerRepositoryInterface::class);
96+ $ this ->newsletterConfig = $ newsletterConfig ?: ObjectManager::getInstance ()
97+ ->get (NewsletterConfig::class);
8598 parent ::__construct (
8699 $ context ,
87100 $ subscriberFactory ,
@@ -95,8 +108,9 @@ public function __construct(
95108 * Validates that the email address isn't being used by a different account.
96109 *
97110 * @param string $email
98- * @throws LocalizedException
111+ *
99112 * @return void
113+ * @throws LocalizedException
100114 */
101115 protected function validateEmailAvailable ($ email )
102116 {
@@ -139,8 +153,9 @@ protected function validateGuestSubscription()
139153 * Validates the format of the email address
140154 *
141155 * @param string $email
142- * @throws LocalizedException
156+ *
143157 * @return void
158+ * @throws LocalizedException
144159 */
145160 protected function validateEmailFormat ($ email )
146161 {
@@ -156,7 +171,10 @@ protected function validateEmailFormat($email)
156171 */
157172 public function execute ()
158173 {
159- if ($ this ->getRequest ()->isPost () && $ this ->getRequest ()->getPost ('email ' )) {
174+ if ($ this ->getRequest ()->isPost ()
175+ && $ this ->getRequest ()->getPost ('email ' )
176+ && $ this ->newsletterConfig ->isActive ()
177+ ) {
160178 $ email = (string )$ this ->getRequest ()->getPost ('email ' );
161179
162180 try {
@@ -195,6 +213,7 @@ public function execute()
195213 $ redirect = $ this ->resultFactory ->create (ResultFactory::TYPE_REDIRECT );
196214 // phpcs:ignore Magento2.Legacy.ObsoleteResponse
197215 $ redirectUrl = $ this ->_redirect ->getRedirectUrl ();
216+
198217 return $ redirect ->setUrl ($ redirectUrl );
199218 }
200219
@@ -203,6 +222,7 @@ public function execute()
203222 *
204223 * @param string $email
205224 * @param int $websiteId
225+ *
206226 * @return int|null
207227 */
208228 private function getCustomerId (string $ email , int $ websiteId ): ?int
@@ -219,6 +239,7 @@ private function getCustomerId(string $email, int $websiteId): ?int
219239 * Get success message
220240 *
221241 * @param int $status
242+ *
222243 * @return Phrase
223244 */
224245 private function getSuccessMessage (int $ status ): Phrase
0 commit comments