-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Refactor contact module #8420
Changes from 6 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 |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
|
||
use Magento\Framework\App\Request\DataPersistorInterface; | ||
use Magento\Framework\App\ObjectManager; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\HTTP\PhpEnvironment\Request; | ||
|
||
class Post extends \Magento\Contact\Controller\Index | ||
{ | ||
|
@@ -19,72 +21,32 @@ class Post extends \Magento\Contact\Controller\Index | |
/** | ||
* Post user question | ||
* | ||
* @return void | ||
* @throws \Exception | ||
*/ | ||
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()); | ||
} finally { | ||
$this->inlineTranslation->resume(); | ||
} | ||
return $this->resultRedirectFactory->create()->setPath('contact/index'); | ||
} | ||
|
||
/** | ||
|
@@ -101,4 +63,57 @@ private function getDataPersistor() | |
|
||
return $this->dataPersistor; | ||
} | ||
|
||
/** | ||
* @param array $post Post data from contact form | ||
*/ | ||
protected function sendEmail($post) | ||
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. Magento discourages the introduction of any new "protected" properties or methods. The reason is that we are trying to get rid of inheritance-based APIS and promote composition ver inheritance. Only public or private modifiers should be used. Please change this to "private" 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. That's good to hear. Would you mind if I removed the abstract base controller then and moved the email sending out of the controller? I hesitated because it looked like inheritance was intended here and extensions might already use it (although that's improbable) 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. Abstract base controllers (and similar framework pieces such as Blocks, Models, etc) still remain places where legacy inheritance-based APIs haven't been refactored yet. I would suggest keeping abstract base controller for consistency with other controllers (and you are right, for backward compatibility) However the policy for the new methods/properties to be public or private. 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. Just to be clear, I was referring to this one, not the framework base controller: https://github.com/magento/magento2/blob/develop/app/code/Magento/Contact/Controller/Index.php |
||
{ | ||
$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, | ||
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. our static test failing with this message:
do we really need the dependency on Backend here? If yes, please add the dependency to the composer.json otherwise. 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. This is the same code as before, just moved to a new method, but good that the static tests catched it :) I'll check if we can get rid of the dependency! 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. yeah, noticed that, though the fact you touched that classed triggered static test failure :) I'd appreciate if you ensure that the dependency is needed and add it to the composer. |
||
] | ||
) | ||
->setTemplateVars(['data' => new \Magento\Framework\DataObject($post)]) | ||
->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(); | ||
} | ||
|
||
private function isPostRequest() | ||
{ | ||
/** @var Request $request */ | ||
$request = $this->getRequest(); | ||
return !empty($request->getPostValue()); | ||
} | ||
|
||
/** | ||
* @return array | ||
* @throws \Exception | ||
*/ | ||
protected 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(); | ||
} | ||
} |
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 "!"