Post.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Contact\Controller\Index;
  8. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  9. use Magento\Contact\Model\ConfigInterface;
  10. use Magento\Contact\Model\MailInterface;
  11. use Magento\Framework\App\Action\Context;
  12. use Magento\Framework\App\Request\DataPersistorInterface;
  13. use Magento\Framework\Controller\Result\Redirect;
  14. use Magento\Framework\Exception\LocalizedException;
  15. use Psr\Log\LoggerInterface;
  16. use Magento\Framework\App\ObjectManager;
  17. use Magento\Framework\DataObject;
  18. class Post extends \Magento\Contact\Controller\Index implements HttpPostActionInterface
  19. {
  20. /**
  21. * @var DataPersistorInterface
  22. */
  23. private $dataPersistor;
  24. /**
  25. * @var Context
  26. */
  27. private $context;
  28. /**
  29. * @var MailInterface
  30. */
  31. private $mail;
  32. /**
  33. * @var LoggerInterface
  34. */
  35. private $logger;
  36. /**
  37. * @param Context $context
  38. * @param ConfigInterface $contactsConfig
  39. * @param MailInterface $mail
  40. * @param DataPersistorInterface $dataPersistor
  41. * @param LoggerInterface $logger
  42. */
  43. public function __construct(
  44. Context $context,
  45. ConfigInterface $contactsConfig,
  46. MailInterface $mail,
  47. DataPersistorInterface $dataPersistor,
  48. LoggerInterface $logger = null
  49. ) {
  50. parent::__construct($context, $contactsConfig);
  51. $this->context = $context;
  52. $this->mail = $mail;
  53. $this->dataPersistor = $dataPersistor;
  54. $this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
  55. }
  56. /**
  57. * Post user question
  58. *
  59. * @return Redirect
  60. */
  61. public function execute()
  62. {
  63. if (!$this->getRequest()->isPost()) {
  64. return $this->resultRedirectFactory->create()->setPath('*/*/');
  65. }
  66. try {
  67. $this->sendEmail($this->validatedParams());
  68. $this->messageManager->addSuccessMessage(
  69. __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.')
  70. );
  71. $this->dataPersistor->clear('contact_us');
  72. } catch (LocalizedException $e) {
  73. $this->messageManager->addErrorMessage($e->getMessage());
  74. $this->dataPersistor->set('contact_us', $this->getRequest()->getParams());
  75. } catch (\Exception $e) {
  76. $this->logger->critical($e);
  77. $this->messageManager->addErrorMessage(
  78. __('An error occurred while processing your form. Please try again later.')
  79. );
  80. $this->dataPersistor->set('contact_us', $this->getRequest()->getParams());
  81. }
  82. return $this->resultRedirectFactory->create()->setPath('contact/index');
  83. }
  84. /**
  85. * @param array $post Post data from contact form
  86. * @return void
  87. */
  88. private function sendEmail($post)
  89. {
  90. $this->mail->send(
  91. $post['email'],
  92. ['data' => new DataObject($post)]
  93. );
  94. }
  95. /**
  96. * @return array
  97. * @throws \Exception
  98. */
  99. private function validatedParams()
  100. {
  101. $request = $this->getRequest();
  102. if (trim($request->getParam('name')) === '') {
  103. throw new LocalizedException(__('Enter the Name and try again.'));
  104. }
  105. if (trim($request->getParam('comment')) === '') {
  106. throw new LocalizedException(__('Enter the comment and try again.'));
  107. }
  108. if (false === \strpos($request->getParam('email'), '@')) {
  109. throw new LocalizedException(__('The email address is invalid. Verify the email address and try again.'));
  110. }
  111. if (trim($request->getParam('hideit')) !== '') {
  112. throw new \Exception();
  113. }
  114. return $request->getParams();
  115. }
  116. }