From b30eda5e23739acac2f1a67aec718847b6743f19 Mon Sep 17 00:00:00 2001 From: Atish Goswami Date: Mon, 6 Nov 2017 19:45:52 +0530 Subject: [PATCH 1/3] Cleanup for object manager references and depricated method --- .../Magento/Contact/Controller/Index/Post.php | 36 +++++++------------ app/code/Magento/Contact/Model/Mail.php | 15 ++++---- 2 files changed, 19 insertions(+), 32 deletions(-) diff --git a/app/code/Magento/Contact/Controller/Index/Post.php b/app/code/Magento/Contact/Controller/Index/Post.php index 3374ff1fa5cf4..3d26555a3157f 100644 --- a/app/code/Magento/Contact/Controller/Index/Post.php +++ b/app/code/Magento/Contact/Controller/Index/Post.php @@ -9,12 +9,12 @@ 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\DataObject; class Post extends \Magento\Contact\Controller\Index { @@ -53,10 +53,10 @@ public function __construct( LoggerInterface $logger = null ) { parent::__construct($context, $contactsConfig); - $this->context = $context; - $this->mail = $mail; + $this->context = $context; + $this->mail = $mail; $this->dataPersistor = $dataPersistor; - $this->logger = $logger ?: \Magento\Framework\App\ObjectManager::getInstance()->get(LoggerInterface::class); + $this->logger = $logger; } /** @@ -71,45 +71,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)] + ); } /** diff --git a/app/code/Magento/Contact/Model/Mail.php b/app/code/Magento/Contact/Model/Mail.php index d63efbbca573b..6592969093ccd 100644 --- a/app/code/Magento/Contact/Model/Mail.php +++ b/app/code/Magento/Contact/Model/Mail.php @@ -6,9 +6,9 @@ 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\Area; class Mail implements MailInterface { @@ -44,20 +44,19 @@ public function __construct( ConfigInterface $contactsConfig, TransportBuilder $transportBuilder, StateInterface $inlineTranslation, - StoreManagerInterface $storeManager = null + StoreManagerInterface $storeManager ) { - $this->contactsConfig = $contactsConfig; - $this->transportBuilder = $transportBuilder; + $this->contactsConfig = $contactsConfig; + $this->transportBuilder = $transportBuilder; $this->inlineTranslation = $inlineTranslation; - $this->storeManager = $storeManager ?: - ObjectManager::getInstance()->get(StoreManagerInterface::class); + $this->storeManager = $storeManager; } /** * Send email from contact form * * @param string $replyTo - * @param array $variables + * @param array $variables * @return void */ public function send($replyTo, array $variables) @@ -71,7 +70,7 @@ public function send($replyTo, array $variables) ->setTemplateIdentifier($this->contactsConfig->emailTemplate()) ->setTemplateOptions( [ - 'area' => 'frontend', + 'area' => Area::AREA_FRONTEND, 'store' => $this->storeManager->getStore()->getId() ] ) From 9e63c27f8e211754be0d75f4e303029c9fc9d259 Mon Sep 17 00:00:00 2001 From: Atish Goswami Date: Mon, 6 Nov 2017 20:37:02 +0530 Subject: [PATCH 2/3] Reverted backward incompatible changes --- app/code/Magento/Contact/Controller/Index/Post.php | 4 +++- app/code/Magento/Contact/Model/Mail.php | 8 +++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Contact/Controller/Index/Post.php b/app/code/Magento/Contact/Controller/Index/Post.php index 3d26555a3157f..7b8d1d48eaecd 100644 --- a/app/code/Magento/Contact/Controller/Index/Post.php +++ b/app/code/Magento/Contact/Controller/Index/Post.php @@ -14,6 +14,7 @@ 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 @@ -56,7 +57,8 @@ public function __construct( $this->context = $context; $this->mail = $mail; $this->dataPersistor = $dataPersistor; - $this->logger = $logger; + $this->logger = $logger ?: + ObjectManager::getInstance()->get(LoggerInterface::class); } /** diff --git a/app/code/Magento/Contact/Model/Mail.php b/app/code/Magento/Contact/Model/Mail.php index 6592969093ccd..f648c426d9a4e 100644 --- a/app/code/Magento/Contact/Model/Mail.php +++ b/app/code/Magento/Contact/Model/Mail.php @@ -8,6 +8,7 @@ use Magento\Framework\Mail\Template\TransportBuilder; 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 @@ -44,12 +45,13 @@ public function __construct( ConfigInterface $contactsConfig, TransportBuilder $transportBuilder, StateInterface $inlineTranslation, - StoreManagerInterface $storeManager + StoreManagerInterface $storeManager = null ) { $this->contactsConfig = $contactsConfig; $this->transportBuilder = $transportBuilder; $this->inlineTranslation = $inlineTranslation; - $this->storeManager = $storeManager; + $this->storeManager = $storeManager ?: + ObjectManager::getInstance()->get(StoreManagerInterface::class); } /** @@ -70,7 +72,7 @@ public function send($replyTo, array $variables) ->setTemplateIdentifier($this->contactsConfig->emailTemplate()) ->setTemplateOptions( [ - 'area' => Area::AREA_FRONTEND, + 'area' => Area::AREA_FRONTEND, 'store' => $this->storeManager->getStore()->getId() ] ) From 75379cfc5b744fccd0698ccdc973b6490ecb915f Mon Sep 17 00:00:00 2001 From: Ievgen Shakhsuvarov Date: Thu, 9 Nov 2017 13:34:49 +0200 Subject: [PATCH 3/3] magento/magento2#12061: Cleanup for object manager references and deprecated method - Updated coding style to match the one used in core magento - Updated integration test to verify for the escaped string --- app/code/Magento/Contact/Controller/Index/Post.php | 7 +++---- app/code/Magento/Contact/Model/Mail.php | 11 +++++------ .../Magento/Contact/Controller/IndexTest.php | 2 +- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/app/code/Magento/Contact/Controller/Index/Post.php b/app/code/Magento/Contact/Controller/Index/Post.php index 7b8d1d48eaecd..ee2d23b74df24 100644 --- a/app/code/Magento/Contact/Controller/Index/Post.php +++ b/app/code/Magento/Contact/Controller/Index/Post.php @@ -54,11 +54,10 @@ public function __construct( LoggerInterface $logger = null ) { parent::__construct($context, $contactsConfig); - $this->context = $context; - $this->mail = $mail; + $this->context = $context; + $this->mail = $mail; $this->dataPersistor = $dataPersistor; - $this->logger = $logger ?: - ObjectManager::getInstance()->get(LoggerInterface::class); + $this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class); } /** diff --git a/app/code/Magento/Contact/Model/Mail.php b/app/code/Magento/Contact/Model/Mail.php index f648c426d9a4e..43c1974252b5a 100644 --- a/app/code/Magento/Contact/Model/Mail.php +++ b/app/code/Magento/Contact/Model/Mail.php @@ -47,18 +47,17 @@ public function __construct( StateInterface $inlineTranslation, StoreManagerInterface $storeManager = null ) { - $this->contactsConfig = $contactsConfig; - $this->transportBuilder = $transportBuilder; + $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); } /** * Send email from contact form * * @param string $replyTo - * @param array $variables + * @param array $variables * @return void */ public function send($replyTo, array $variables) @@ -72,7 +71,7 @@ public function send($replyTo, array $variables) ->setTemplateIdentifier($this->contactsConfig->emailTemplate()) ->setTemplateOptions( [ - 'area' => Area::AREA_FRONTEND, + 'area' => Area::AREA_FRONTEND, 'store' => $this->storeManager->getStore()->getId() ] ) diff --git a/dev/tests/integration/testsuite/Magento/Contact/Controller/IndexTest.php b/dev/tests/integration/testsuite/Magento/Contact/Controller/IndexTest.php index b8dbfec59845b..2d76632cae0b7 100644 --- a/dev/tests/integration/testsuite/Magento/Contact/Controller/IndexTest.php +++ b/dev/tests/integration/testsuite/Magento/Contact/Controller/IndexTest.php @@ -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." ), \Magento\Framework\Message\MessageInterface::TYPE_SUCCESS );