-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Refactor contact module #8420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor contact module #8420
Changes from all commits
47a4681
e1d47cc
962c06d
fb902c6
352dcef
c8fbb70
36261ea
ec3fad1
0c78857
b4f68b0
5854390
854f16d
22004d7
cdf8725
28d939f
7a01613
0261088
a5c69d0
f5ac9f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,16 @@ | |
*/ | ||
namespace Magento\Contact\Controller\Index; | ||
|
||
use Magento\Framework\App\Request\DataPersistorInterface; | ||
use Magento\Contact\Model\ConfigInterface; | ||
use Magento\Contact\Model\MailInterface; | ||
use Magento\Framework\App\Action\Context; | ||
use Magento\Framework\App\ObjectManager; | ||
use Magento\Framework\App\Request\DataPersistorInterface; | ||
use Magento\Framework\Controller\Result\Redirect; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\HTTP\PhpEnvironment\Request; | ||
use Magento\Framework\Mail\Template\TransportBuilder; | ||
use Magento\Framework\Translate\Inline\StateInterface; | ||
|
||
class Post extends \Magento\Contact\Controller\Index | ||
{ | ||
|
@@ -16,75 +24,60 @@ class Post extends \Magento\Contact\Controller\Index | |
*/ | ||
private $dataPersistor; | ||
|
||
/** | ||
* @var Context | ||
*/ | ||
private $context; | ||
|
||
/** | ||
* @var MailInterface | ||
*/ | ||
private $mail; | ||
|
||
/** | ||
* @param Context $context | ||
* @param MailInterface $mail | ||
* @param ConfigInterface $contactsConfig | ||
* @param DataPersistorInterface $dataPersistor | ||
*/ | ||
public function __construct( | ||
Context $context, | ||
MailInterface $mail, | ||
ConfigInterface $contactsConfig, | ||
DataPersistorInterface $dataPersistor | ||
) { | ||
parent::__construct($context, $contactsConfig); | ||
$this->context = $context; | ||
$this->mail = $mail; | ||
$this->dataPersistor = $dataPersistor; | ||
} | ||
|
||
/** | ||
* Post user question | ||
* | ||
* @return void | ||
* @throws \Exception | ||
* @return Redirect | ||
*/ | ||
public function execute() | ||
{ | ||
$post = $this->getRequest()->getPostValue(); | ||
if (!$post) { | ||
$this->_redirect('*/*/'); | ||
return; | ||
if (! $this->isPostRequest()) { | ||
return $this->resultRedirectFactory->create()->setPath('*/*/'); | ||
} | ||
|
||
$this->inlineTranslation->suspend(); | ||
try { | ||
$postObject = new \Magento\Framework\DataObject(); | ||
$postObject->setData($post); | ||
|
||
$error = false; | ||
|
||
if (!\Zend_Validate::is(trim($post['name']), 'NotEmpty')) { | ||
$error = true; | ||
} | ||
if (!\Zend_Validate::is(trim($post['comment']), 'NotEmpty')) { | ||
$error = true; | ||
} | ||
if (!\Zend_Validate::is(trim($post['email']), 'EmailAddress')) { | ||
$error = true; | ||
} | ||
if (\Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) { | ||
$error = true; | ||
} | ||
if ($error) { | ||
throw new \Exception(); | ||
} | ||
|
||
$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE; | ||
$transport = $this->_transportBuilder | ||
->setTemplateIdentifier($this->scopeConfig->getValue(self::XML_PATH_EMAIL_TEMPLATE, $storeScope)) | ||
->setTemplateOptions( | ||
[ | ||
'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE, | ||
'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID, | ||
] | ||
) | ||
->setTemplateVars(['data' => $postObject]) | ||
->setFrom($this->scopeConfig->getValue(self::XML_PATH_EMAIL_SENDER, $storeScope)) | ||
->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope)) | ||
->setReplyTo($post['email']) | ||
->getTransport(); | ||
|
||
$transport->sendMessage(); | ||
$this->inlineTranslation->resume(); | ||
$this->sendEmail($this->validatedParams()); | ||
$this->messageManager->addSuccess( | ||
__('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.') | ||
); | ||
$this->getDataPersistor()->clear('contact_us'); | ||
$this->_redirect('contact/index'); | ||
return; | ||
} catch (LocalizedException $e) { | ||
$this->messageManager->addErrorMessage($e->getMessage()); | ||
$this->getDataPersistor()->set('contact_us', $this->getRequest()->getParams()); | ||
} catch (\Exception $e) { | ||
$this->inlineTranslation->resume(); | ||
$this->messageManager->addError( | ||
$this->messageManager->addErrorMessage( | ||
__('We can\'t process your request right now. Sorry, that\'s all we know.') | ||
); | ||
$this->getDataPersistor()->set('contact_us', $post); | ||
$this->_redirect('contact/index'); | ||
return; | ||
$this->getDataPersistor()->set('contact_us', $this->getRequest()->getParams()); | ||
} | ||
return $this->resultRedirectFactory->create()->setPath('contact/index'); | ||
} | ||
|
||
/** | ||
|
@@ -101,4 +94,46 @@ private function getDataPersistor() | |
|
||
return $this->dataPersistor; | ||
} | ||
|
||
/** | ||
* @param array $post Post data from contact form | ||
* @return void | ||
*/ | ||
private function sendEmail($post) | ||
{ | ||
$this->mail->send($post['email'], ['data' => new \Magento\Framework\DataObject($post)]); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
private function isPostRequest() | ||
{ | ||
/** @var Request $request */ | ||
$request = $this->getRequest(); | ||
return !empty($request->getPostValue()); | ||
} | ||
|
||
/** | ||
* @return array | ||
* @throws \Exception | ||
*/ | ||
private function validatedParams() | ||
{ | ||
$request = $this->getRequest(); | ||
if (trim($request->getParam('name')) === '') { | ||
throw new LocalizedException(__('Name is missing')); | ||
} | ||
if (trim($request->getParam('comment')) === '') { | ||
throw new LocalizedException(__('Comment is missing')); | ||
} | ||
if (false === \strpos($request->getParam('email'), '@')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are losing quite a bit of functionality here vs Zend_Validate_EmailAddress. Maybe the better approach would be to switch to the ZF2 component https://github.com/zendframework/zend-validator/blob/master/src/EmailAddress.php#L492 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did that on purpose because I don't believe in email address validation via regular expressions (relevant: https://hackernoon.com/the-100-correct-way-to-validate-email-addresses-7c4818f24643). If there's a decision to use the MX check, the ZF2 email validator would be useful again. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great article - you got me convinced. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we not using php's native
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @joe-vortex same reason that I dropped Zend_Validate_EmailAddress. The top comment on http://php.net/manual/en/function.filter-var.php already gives an example |
||
throw new LocalizedException(__('Invalid email address')); | ||
} | ||
if (trim($request->getParam('hideit')) !== '') { | ||
throw new \Exception(); | ||
} | ||
|
||
return $request->getParams(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
/** | ||
* Copyright © 2013-2017 Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Contact\Model; | ||
|
||
use Magento\Contact\Model\ConfigInterface; | ||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Store\Model\ScopeInterface; | ||
|
||
/** | ||
* Contact module configuration | ||
*/ | ||
class Config implements ConfigInterface | ||
{ | ||
/** | ||
* @var ScopeConfigInterface | ||
*/ | ||
private $scopeConfig; | ||
|
||
/** | ||
* @param ScopeConfigInterface $scopeConfig | ||
*/ | ||
public function __construct(ScopeConfigInterface $scopeConfig) | ||
{ | ||
$this->scopeConfig = $scopeConfig; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isEnabled() | ||
{ | ||
return $this->scopeConfig->isSetFlag( | ||
self::XML_PATH_ENABLED, | ||
ScopeInterface::SCOPE_STORE | ||
); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function emailTemplate() | ||
{ | ||
return $this->scopeConfig->getValue( | ||
ConfigInterface::XML_PATH_EMAIL_TEMPLATE, | ||
ScopeInterface::SCOPE_STORE | ||
); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function emailSender() | ||
{ | ||
return $this->scopeConfig->getValue( | ||
ConfigInterface::XML_PATH_EMAIL_SENDER, | ||
ScopeInterface::SCOPE_STORE | ||
); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function emailRecipient() | ||
{ | ||
return $this->scopeConfig->getValue( | ||
ConfigInterface::XML_PATH_EMAIL_RECIPIENT, | ||
ScopeInterface::SCOPE_STORE | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra space here after "!"