Skip to content

Commit b30eda5

Browse files
committed
Cleanup for object manager references and depricated method
1 parent 13a933a commit b30eda5

File tree

2 files changed

+19
-32
lines changed

2 files changed

+19
-32
lines changed

app/code/Magento/Contact/Controller/Index/Post.php

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
use Magento\Contact\Model\ConfigInterface;
1010
use Magento\Contact\Model\MailInterface;
1111
use Magento\Framework\App\Action\Context;
12-
use Magento\Framework\App\ObjectManager;
1312
use Magento\Framework\App\Request\DataPersistorInterface;
1413
use Magento\Framework\Controller\Result\Redirect;
1514
use Magento\Framework\Exception\LocalizedException;
1615
use Magento\Framework\HTTP\PhpEnvironment\Request;
1716
use Psr\Log\LoggerInterface;
17+
use Magento\Framework\DataObject;
1818

1919
class Post extends \Magento\Contact\Controller\Index
2020
{
@@ -53,10 +53,10 @@ public function __construct(
5353
LoggerInterface $logger = null
5454
) {
5555
parent::__construct($context, $contactsConfig);
56-
$this->context = $context;
57-
$this->mail = $mail;
56+
$this->context = $context;
57+
$this->mail = $mail;
5858
$this->dataPersistor = $dataPersistor;
59-
$this->logger = $logger ?: \Magento\Framework\App\ObjectManager::getInstance()->get(LoggerInterface::class);
59+
$this->logger = $logger;
6060
}
6161

6262
/**
@@ -71,45 +71,33 @@ public function execute()
7171
}
7272
try {
7373
$this->sendEmail($this->validatedParams());
74-
$this->messageManager->addSuccess(
74+
$this->messageManager->addSuccessMessage(
7575
__('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.')
7676
);
77-
$this->getDataPersistor()->clear('contact_us');
77+
$this->dataPersistor->clear('contact_us');
7878
} catch (LocalizedException $e) {
7979
$this->messageManager->addErrorMessage($e->getMessage());
80-
$this->getDataPersistor()->set('contact_us', $this->getRequest()->getParams());
80+
$this->dataPersistor->set('contact_us', $this->getRequest()->getParams());
8181
} catch (\Exception $e) {
8282
$this->logger->critical($e);
8383
$this->messageManager->addErrorMessage(
8484
__('An error occurred while processing your form. Please try again later.')
8585
);
86-
$this->getDataPersistor()->set('contact_us', $this->getRequest()->getParams());
86+
$this->dataPersistor->set('contact_us', $this->getRequest()->getParams());
8787
}
8888
return $this->resultRedirectFactory->create()->setPath('contact/index');
8989
}
9090

91-
/**
92-
* Get Data Persistor
93-
*
94-
* @return DataPersistorInterface
95-
*/
96-
private function getDataPersistor()
97-
{
98-
if ($this->dataPersistor === null) {
99-
$this->dataPersistor = ObjectManager::getInstance()
100-
->get(DataPersistorInterface::class);
101-
}
102-
103-
return $this->dataPersistor;
104-
}
105-
10691
/**
10792
* @param array $post Post data from contact form
10893
* @return void
10994
*/
11095
private function sendEmail($post)
11196
{
112-
$this->mail->send($post['email'], ['data' => new \Magento\Framework\DataObject($post)]);
97+
$this->mail->send(
98+
$post['email'],
99+
['data' => new DataObject($post)]
100+
);
113101
}
114102

115103
/**

app/code/Magento/Contact/Model/Mail.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
namespace Magento\Contact\Model;
77

88
use Magento\Framework\Mail\Template\TransportBuilder;
9-
use Magento\Framework\App\ObjectManager;
109
use Magento\Framework\Translate\Inline\StateInterface;
1110
use Magento\Store\Model\StoreManagerInterface;
11+
use Magento\Framework\App\Area;
1212

1313
class Mail implements MailInterface
1414
{
@@ -44,20 +44,19 @@ public function __construct(
4444
ConfigInterface $contactsConfig,
4545
TransportBuilder $transportBuilder,
4646
StateInterface $inlineTranslation,
47-
StoreManagerInterface $storeManager = null
47+
StoreManagerInterface $storeManager
4848
) {
49-
$this->contactsConfig = $contactsConfig;
50-
$this->transportBuilder = $transportBuilder;
49+
$this->contactsConfig = $contactsConfig;
50+
$this->transportBuilder = $transportBuilder;
5151
$this->inlineTranslation = $inlineTranslation;
52-
$this->storeManager = $storeManager ?:
53-
ObjectManager::getInstance()->get(StoreManagerInterface::class);
52+
$this->storeManager = $storeManager;
5453
}
5554

5655
/**
5756
* Send email from contact form
5857
*
5958
* @param string $replyTo
60-
* @param array $variables
59+
* @param array $variables
6160
* @return void
6261
*/
6362
public function send($replyTo, array $variables)
@@ -71,7 +70,7 @@ public function send($replyTo, array $variables)
7170
->setTemplateIdentifier($this->contactsConfig->emailTemplate())
7271
->setTemplateOptions(
7372
[
74-
'area' => 'frontend',
73+
'area' => Area::AREA_FRONTEND,
7574
'store' => $this->storeManager->getStore()->getId()
7675
]
7776
)

0 commit comments

Comments
 (0)