Skip to content

Commit 8e22676

Browse files
arnoudhgzlewisvoncken
authored andcommitted
Improve code quality for Subscriber/NewAction
+ Refactor `else` block for returning success message to private function + Use import statements for classes + Because imports are being used an `if` statement can be written on one line + Use `addExceptionMessage` and `addSuccessMessage` instead of their deprecated counterparts + Include with DI the `Magento\Framework\Validator\EmailAddress` class which can be used to validate the email address. Therefore the static function call to the `is` method on the `Zend_Validate` class is not needed anymore.
1 parent 11f66c4 commit 8e22676

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed

app/code/Magento/Newsletter/Controller/Subscriber/NewAction.php

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,32 @@
1010
use Magento\Customer\Model\Session;
1111
use Magento\Customer\Model\Url as CustomerUrl;
1212
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;
1321
use Magento\Store\Model\StoreManagerInterface;
1422
use Magento\Newsletter\Model\SubscriberFactory;
1523

1624
/**
1725
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1826
*/
19-
class NewAction extends \Magento\Newsletter\Controller\Subscriber
27+
class NewAction extends SubscriberController
2028
{
2129
/**
2230
* @var CustomerAccountManagement
2331
*/
2432
protected $customerAccountManagement;
2533

34+
/**
35+
* @var EmailValidator
36+
*/
37+
private $emailValidator;
38+
2639
/**
2740
* Initialize dependencies.
2841
*
@@ -32,16 +45,19 @@ class NewAction extends \Magento\Newsletter\Controller\Subscriber
3245
* @param StoreManagerInterface $storeManager
3346
* @param CustomerUrl $customerUrl
3447
* @param CustomerAccountManagement $customerAccountManagement
48+
* @param EmailValidator $emailValidator
3549
*/
3650
public function __construct(
3751
Context $context,
3852
SubscriberFactory $subscriberFactory,
3953
Session $customerSession,
4054
StoreManagerInterface $storeManager,
4155
CustomerUrl $customerUrl,
42-
CustomerAccountManagement $customerAccountManagement
56+
CustomerAccountManagement $customerAccountManagement,
57+
EmailValidator $emailValidator = null
4358
) {
4459
$this->customerAccountManagement = $customerAccountManagement;
60+
$this->emailValidator = $emailValidator ?: ObjectManager::getInstance()->get(EmailValidator::class);
4561
parent::__construct(
4662
$context,
4763
$subscriberFactory,
@@ -55,7 +71,7 @@ public function __construct(
5571
* Validates that the email address isn't being used by a different account.
5672
*
5773
* @param string $email
58-
* @throws \Magento\Framework\Exception\LocalizedException
74+
* @throws LocalizedException
5975
* @return void
6076
*/
6177
protected function validateEmailAvailable($email)
@@ -64,7 +80,7 @@ protected function validateEmailAvailable($email)
6480
if ($this->_customerSession->getCustomerDataObject()->getEmail() !== $email
6581
&& !$this->customerAccountManagement->isEmailAvailable($email, $websiteId)
6682
) {
67-
throw new \Magento\Framework\Exception\LocalizedException(
83+
throw new LocalizedException(
6884
__('This email address is already assigned to another user.')
6985
);
7086
}
@@ -73,19 +89,19 @@ protected function validateEmailAvailable($email)
7389
/**
7490
* Validates that if the current user is a guest, that they can subscribe to a newsletter.
7591
*
76-
* @throws \Magento\Framework\Exception\LocalizedException
92+
* @throws LocalizedException
7793
* @return void
7894
*/
7995
protected function validateGuestSubscription()
8096
{
81-
if ($this->_objectManager->get(\Magento\Framework\App\Config\ScopeConfigInterface::class)
97+
if ($this->_objectManager->get(ScopeConfigInterface::class)
8298
->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
85101
) != 1
86102
&& !$this->_customerSession->isLoggedIn()
87103
) {
88-
throw new \Magento\Framework\Exception\LocalizedException(
104+
throw new LocalizedException(
89105
__(
90106
'Sorry, but the administrator denied subscription for guests. Please <a href="%1">register</a>.',
91107
$this->_customerUrl->getRegisterUrl()
@@ -98,20 +114,19 @@ protected function validateGuestSubscription()
98114
* Validates the format of the email address
99115
*
100116
* @param string $email
101-
* @throws \Magento\Framework\Exception\LocalizedException
117+
* @throws LocalizedException
102118
* @return void
103119
*/
104120
protected function validateEmailFormat($email)
105121
{
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.'));
108124
}
109125
}
110126

111127
/**
112128
* New subscription action
113129
*
114-
* @throws \Magento\Framework\Exception\LocalizedException
115130
* @return void
116131
*/
117132
public function execute()
@@ -126,28 +141,37 @@ public function execute()
126141

127142
$subscriber = $this->_subscriberFactory->create()->loadByEmail($email);
128143
if ($subscriber->getId()
129-
&& $subscriber->getSubscriberStatus() == \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED
144+
&& (int) $subscriber->getSubscriberStatus() === Subscriber::STATUS_SUBSCRIBED
130145
) {
131-
throw new \Magento\Framework\Exception\LocalizedException(
146+
throw new LocalizedException(
132147
__('This email address is already subscribed.')
133148
);
134149
}
135150

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(
144155
$e,
145156
__('There was a problem with the subscription: %1', $e->getMessage())
146157
);
147158
} 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.'));
149160
}
150161
}
151162
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
152163
}
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+
}
153177
}

0 commit comments

Comments
 (0)