Skip to content

Cleanup ObjectManager usage - Magento_SendFriend #27385

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

Merged
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
87 changes: 49 additions & 38 deletions app/code/Magento/SendFriend/Controller/Product/Sendmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,93 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\SendFriend\Controller\Product;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Session;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Controller\Result\Forward;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Registry;
use Magento\SendFriend\Controller\Product;
use Magento\SendFriend\Model\CaptchaValidator;
use Magento\SendFriend\Model\SendFriend;

/**
* Class Sendmail. Represents request flow logic of 'sendmail' feature
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Sendmail extends \Magento\SendFriend\Controller\Product implements HttpPostActionInterface
class Sendmail extends Product implements HttpPostActionInterface
{
/**
* @var \Magento\Catalog\Api\CategoryRepositoryInterface
* @var CategoryRepositoryInterface
*/
protected $categoryRepository;
private $categoryRepository;

/**
* @var \Magento\Catalog\Model\Session
* @var Session
*/
protected $catalogSession;
private $catalogSession;

/**
* @var CaptchaValidator
*/
private $captchaValidator;

/**
* Sendmail class construct
*
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\Registry $coreRegistry
* @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
* @param \Magento\SendFriend\Model\SendFriend $sendFriend
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
* @param \Magento\Catalog\Model\Session $catalogSession
* @param Context $context
* @param Registry $coreRegistry
* @param Validator $formKeyValidator
* @param SendFriend $sendFriend
* @param ProductRepositoryInterface $productRepository
* @param CategoryRepositoryInterface $categoryRepository
* @param Session $catalogSession
* @param CaptchaValidator|null $captchaValidator
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Registry $coreRegistry,
\Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
\Magento\SendFriend\Model\SendFriend $sendFriend,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
\Magento\Catalog\Model\Session $catalogSession,
CaptchaValidator $captchaValidator = null
Context $context,
Registry $coreRegistry,
Validator $formKeyValidator,
SendFriend $sendFriend,
ProductRepositoryInterface $productRepository,
CategoryRepositoryInterface $categoryRepository,
Session $catalogSession,
CaptchaValidator $captchaValidator
) {
parent::__construct($context, $coreRegistry, $formKeyValidator, $sendFriend, $productRepository);
$this->categoryRepository = $categoryRepository;
$this->catalogSession = $catalogSession;
$this->captchaValidator = $captchaValidator ?: ObjectManager::getInstance()->create(CaptchaValidator::class);
$this->captchaValidator = $captchaValidator;
}

/**
* Send Email Post Action
*
* @return \Magento\Framework\Controller\ResultInterface
* @return ResultInterface
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
/** @var Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);

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

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

if ($validate === true) {
$this->sendFriend->send();
$this->messageManager->addSuccess(__('The link to a friend was sent.'));
$this->messageManager->addSuccessMessage(__('The link to a friend was sent.'));
$url = $product->getProductUrl();
$resultRedirect->setUrl($this->_redirect->success($url));
return $resultRedirect;
} else {
if (is_array($validate)) {
foreach ($validate as $errorMessage) {
$this->messageManager->addError($errorMessage);
}
} else {
$this->messageManager->addError(__('We found some problems with the data.'));
}

if (is_array($validate)) {
foreach ($validate as $errorMessage) {
$this->messageManager->addErrorMessage($errorMessage);
}
} else {
$this->messageManager->addErrorMessage(__('We found some problems with the data.'));
}
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addException($e, __('Some emails were not sent.'));
$this->messageManager->addExceptionMessage($e, __('Some emails were not sent.'));
}

// save form data
Expand Down