Skip to content

Commit d238a65

Browse files
authored
ENGCOM-7174: Cleanup ObjectManager usage - Magento_SendFriend #27385
2 parents 7e682b5 + a089dd0 commit d238a65

File tree

1 file changed

+49
-38
lines changed

1 file changed

+49
-38
lines changed

app/code/Magento/SendFriend/Controller/Product/Sendmail.php

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,82 +3,93 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\SendFriend\Controller\Product;
89

10+
use Magento\Catalog\Api\CategoryRepositoryInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Catalog\Model\Session;
13+
use Magento\Framework\App\Action\Context;
914
use Magento\Framework\App\Action\HttpPostActionInterface;
10-
use Magento\Framework\App\ObjectManager;
15+
use Magento\Framework\Controller\Result\Forward;
16+
use Magento\Framework\Controller\Result\Redirect;
17+
use Magento\Framework\Controller\ResultInterface;
18+
use Magento\Framework\Data\Form\FormKey\Validator;
19+
use Magento\Framework\Exception\LocalizedException;
1120
use Magento\Framework\Exception\NoSuchEntityException;
1221
use Magento\Framework\Controller\ResultFactory;
22+
use Magento\Framework\Registry;
23+
use Magento\SendFriend\Controller\Product;
1324
use Magento\SendFriend\Model\CaptchaValidator;
25+
use Magento\SendFriend\Model\SendFriend;
1426

1527
/**
1628
* Class Sendmail. Represents request flow logic of 'sendmail' feature
1729
*
1830
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1931
*/
20-
class Sendmail extends \Magento\SendFriend\Controller\Product implements HttpPostActionInterface
32+
class Sendmail extends Product implements HttpPostActionInterface
2133
{
2234
/**
23-
* @var \Magento\Catalog\Api\CategoryRepositoryInterface
35+
* @var CategoryRepositoryInterface
2436
*/
25-
protected $categoryRepository;
37+
private $categoryRepository;
2638

2739
/**
28-
* @var \Magento\Catalog\Model\Session
40+
* @var Session
2941
*/
30-
protected $catalogSession;
42+
private $catalogSession;
3143

3244
/**
3345
* @var CaptchaValidator
3446
*/
3547
private $captchaValidator;
3648

3749
/**
38-
* Sendmail class construct
39-
*
40-
* @param \Magento\Framework\App\Action\Context $context
41-
* @param \Magento\Framework\Registry $coreRegistry
42-
* @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
43-
* @param \Magento\SendFriend\Model\SendFriend $sendFriend
44-
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
45-
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
46-
* @param \Magento\Catalog\Model\Session $catalogSession
50+
* @param Context $context
51+
* @param Registry $coreRegistry
52+
* @param Validator $formKeyValidator
53+
* @param SendFriend $sendFriend
54+
* @param ProductRepositoryInterface $productRepository
55+
* @param CategoryRepositoryInterface $categoryRepository
56+
* @param Session $catalogSession
4757
* @param CaptchaValidator|null $captchaValidator
4858
*/
4959
public function __construct(
50-
\Magento\Framework\App\Action\Context $context,
51-
\Magento\Framework\Registry $coreRegistry,
52-
\Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
53-
\Magento\SendFriend\Model\SendFriend $sendFriend,
54-
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
55-
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
56-
\Magento\Catalog\Model\Session $catalogSession,
57-
CaptchaValidator $captchaValidator = null
60+
Context $context,
61+
Registry $coreRegistry,
62+
Validator $formKeyValidator,
63+
SendFriend $sendFriend,
64+
ProductRepositoryInterface $productRepository,
65+
CategoryRepositoryInterface $categoryRepository,
66+
Session $catalogSession,
67+
CaptchaValidator $captchaValidator
5868
) {
5969
parent::__construct($context, $coreRegistry, $formKeyValidator, $sendFriend, $productRepository);
6070
$this->categoryRepository = $categoryRepository;
6171
$this->catalogSession = $catalogSession;
62-
$this->captchaValidator = $captchaValidator ?: ObjectManager::getInstance()->create(CaptchaValidator::class);
72+
$this->captchaValidator = $captchaValidator;
6373
}
6474

6575
/**
6676
* Send Email Post Action
6777
*
68-
* @return \Magento\Framework\Controller\ResultInterface
78+
* @return ResultInterface
79+
*
6980
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
7081
* @SuppressWarnings(PHPMD.NPathComplexity)
7182
*/
7283
public function execute()
7384
{
74-
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
85+
/** @var Redirect $resultRedirect */
7586
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
7687

7788
$product = $this->_initProduct();
7889
$data = $this->getRequest()->getPostValue();
7990

8091
if (!$product || !$data) {
81-
/** @var \Magento\Framework\Controller\Result\Forward $resultForward */
92+
/** @var Forward $resultForward */
8293
$resultForward = $this->resultFactory->create(ResultFactory::TYPE_FORWARD);
8394
$resultForward->forward('noroute');
8495
return $resultForward;
@@ -108,23 +119,23 @@ public function execute()
108119

109120
if ($validate === true) {
110121
$this->sendFriend->send();
111-
$this->messageManager->addSuccess(__('The link to a friend was sent.'));
122+
$this->messageManager->addSuccessMessage(__('The link to a friend was sent.'));
112123
$url = $product->getProductUrl();
113124
$resultRedirect->setUrl($this->_redirect->success($url));
114125
return $resultRedirect;
115-
} else {
116-
if (is_array($validate)) {
117-
foreach ($validate as $errorMessage) {
118-
$this->messageManager->addError($errorMessage);
119-
}
120-
} else {
121-
$this->messageManager->addError(__('We found some problems with the data.'));
126+
}
127+
128+
if (is_array($validate)) {
129+
foreach ($validate as $errorMessage) {
130+
$this->messageManager->addErrorMessage($errorMessage);
122131
}
132+
} else {
133+
$this->messageManager->addErrorMessage(__('We found some problems with the data.'));
123134
}
124-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
125-
$this->messageManager->addError($e->getMessage());
135+
} catch (LocalizedException $e) {
136+
$this->messageManager->addErrorMessage($e->getMessage());
126137
} catch (\Exception $e) {
127-
$this->messageManager->addException($e, __('Some emails were not sent.'));
138+
$this->messageManager->addExceptionMessage($e, __('Some emails were not sent.'));
128139
}
129140

130141
// save form data

0 commit comments

Comments
 (0)