Skip to content

Commit

Permalink
Cleanup for object manager references and depricated method
Browse files Browse the repository at this point in the history
  • Loading branch information
atishgoswami committed Nov 6, 2017
1 parent 13a933a commit b30eda5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 32 deletions.
36 changes: 12 additions & 24 deletions app/code/Magento/Contact/Controller/Index/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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)]
);
}

/**
Expand Down
15 changes: 7 additions & 8 deletions app/code/Magento/Contact/Model/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
Expand All @@ -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()
]
)
Expand Down

0 comments on commit b30eda5

Please sign in to comment.