Skip to content
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

Cleanup for object manager references and depricated method #12061

Merged
merged 4 commits into from
Nov 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 11 additions & 22 deletions app/code/Magento/Contact/Controller/Index/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
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 Psr\Log\LoggerInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObject;

class Post extends \Magento\Contact\Controller\Index
{
Expand Down Expand Up @@ -56,7 +57,7 @@ public function __construct(
$this->context = $context;
$this->mail = $mail;
$this->dataPersistor = $dataPersistor;
$this->logger = $logger ?: \Magento\Framework\App\ObjectManager::getInstance()->get(LoggerInterface::class);
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
}

/**
Expand All @@ -71,45 +72,33 @@ public function execute()
}
try {
$this->sendEmail($this->validatedParams());
$this->messageManager->addSuccess(
$this->messageManager->addSuccessMessage(
__('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.')
);
$this->getDataPersistor()->clear('contact_us');
$this->dataPersistor->clear('contact_us');
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$this->getDataPersistor()->set('contact_us', $this->getRequest()->getParams());
$this->dataPersistor->set('contact_us', $this->getRequest()->getParams());
} catch (\Exception $e) {
$this->logger->critical($e);
$this->messageManager->addErrorMessage(
__('An error occurred while processing your form. Please try again later.')
);
$this->getDataPersistor()->set('contact_us', $this->getRequest()->getParams());
$this->dataPersistor->set('contact_us', $this->getRequest()->getParams());
}
return $this->resultRedirectFactory->create()->setPath('contact/index');
}

/**
* Get Data Persistor
*
* @return DataPersistorInterface
*/
private function getDataPersistor()
{
if ($this->dataPersistor === null) {
$this->dataPersistor = ObjectManager::getInstance()
->get(DataPersistorInterface::class);
}

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)]);
$this->mail->send(
$post['email'],
['data' => new DataObject($post)]
);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions app/code/Magento/Contact/Model/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
namespace Magento\Contact\Model;

use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Area;

class Mail implements MailInterface
{
Expand Down Expand Up @@ -49,8 +50,7 @@ public function __construct(
$this->contactsConfig = $contactsConfig;
$this->transportBuilder = $transportBuilder;
$this->inlineTranslation = $inlineTranslation;
$this->storeManager = $storeManager ?:
ObjectManager::getInstance()->get(StoreManagerInterface::class);
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
}

/**
Expand All @@ -71,7 +71,7 @@ public function send($replyTo, array $variables)
->setTemplateIdentifier($this->contactsConfig->emailTemplate())
->setTemplateOptions(
[
'area' => 'frontend',
'area' => Area::AREA_FRONTEND,
'store' => $this->storeManager->getStore()->getId()
]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testPostAction()
$this->assertRedirect($this->stringContains('contact/index'));
$this->assertSessionMessages(
$this->contains(
"Thanks for contacting us with your comments and questions. We'll respond to you very soon."
"Thanks for contacting us with your comments and questions. We'll respond to you very soon."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure that it will be good idea to make it escaped there. Have you checked this change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I have checked this manually. On Contact Form It works as expected:
contact_form_html_entities

Since this test is testing the controller I assume that it is valid to check for the exact html we expect.

),
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
);
Expand Down