|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
| 6 | +declare(strict_types=1); |
6 | 7 |
|
7 | 8 | namespace Magento\SendFriend\Controller\Product;
|
8 | 9 |
|
| 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; |
9 | 14 | 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; |
11 | 20 | use Magento\Framework\Exception\NoSuchEntityException;
|
12 | 21 | use Magento\Framework\Controller\ResultFactory;
|
| 22 | +use Magento\Framework\Registry; |
| 23 | +use Magento\SendFriend\Controller\Product; |
13 | 24 | use Magento\SendFriend\Model\CaptchaValidator;
|
| 25 | +use Magento\SendFriend\Model\SendFriend; |
14 | 26 |
|
15 | 27 | /**
|
16 | 28 | * Class Sendmail. Represents request flow logic of 'sendmail' feature
|
17 | 29 | *
|
18 | 30 | * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
19 | 31 | */
|
20 |
| -class Sendmail extends \Magento\SendFriend\Controller\Product implements HttpPostActionInterface |
| 32 | +class Sendmail extends Product implements HttpPostActionInterface |
21 | 33 | {
|
22 | 34 | /**
|
23 |
| - * @var \Magento\Catalog\Api\CategoryRepositoryInterface |
| 35 | + * @var CategoryRepositoryInterface |
24 | 36 | */
|
25 |
| - protected $categoryRepository; |
| 37 | + private $categoryRepository; |
26 | 38 |
|
27 | 39 | /**
|
28 |
| - * @var \Magento\Catalog\Model\Session |
| 40 | + * @var Session |
29 | 41 | */
|
30 |
| - protected $catalogSession; |
| 42 | + private $catalogSession; |
31 | 43 |
|
32 | 44 | /**
|
33 | 45 | * @var CaptchaValidator
|
34 | 46 | */
|
35 | 47 | private $captchaValidator;
|
36 | 48 |
|
37 | 49 | /**
|
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 |
47 | 57 | * @param CaptchaValidator|null $captchaValidator
|
48 | 58 | */
|
49 | 59 | 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 |
58 | 68 | ) {
|
59 | 69 | parent::__construct($context, $coreRegistry, $formKeyValidator, $sendFriend, $productRepository);
|
60 | 70 | $this->categoryRepository = $categoryRepository;
|
61 | 71 | $this->catalogSession = $catalogSession;
|
62 |
| - $this->captchaValidator = $captchaValidator ?: ObjectManager::getInstance()->create(CaptchaValidator::class); |
| 72 | + $this->captchaValidator = $captchaValidator; |
63 | 73 | }
|
64 | 74 |
|
65 | 75 | /**
|
66 | 76 | * Send Email Post Action
|
67 | 77 | *
|
68 |
| - * @return \Magento\Framework\Controller\ResultInterface |
| 78 | + * @return ResultInterface |
| 79 | + * |
69 | 80 | * @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
70 | 81 | * @SuppressWarnings(PHPMD.NPathComplexity)
|
71 | 82 | */
|
72 | 83 | public function execute()
|
73 | 84 | {
|
74 |
| - /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ |
| 85 | + /** @var Redirect $resultRedirect */ |
75 | 86 | $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
|
76 | 87 |
|
77 | 88 | $product = $this->_initProduct();
|
78 | 89 | $data = $this->getRequest()->getPostValue();
|
79 | 90 |
|
80 | 91 | if (!$product || !$data) {
|
81 |
| - /** @var \Magento\Framework\Controller\Result\Forward $resultForward */ |
| 92 | + /** @var Forward $resultForward */ |
82 | 93 | $resultForward = $this->resultFactory->create(ResultFactory::TYPE_FORWARD);
|
83 | 94 | $resultForward->forward('noroute');
|
84 | 95 | return $resultForward;
|
@@ -108,23 +119,23 @@ public function execute()
|
108 | 119 |
|
109 | 120 | if ($validate === true) {
|
110 | 121 | $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.')); |
112 | 123 | $url = $product->getProductUrl();
|
113 | 124 | $resultRedirect->setUrl($this->_redirect->success($url));
|
114 | 125 | 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); |
122 | 131 | }
|
| 132 | + } else { |
| 133 | + $this->messageManager->addErrorMessage(__('We found some problems with the data.')); |
123 | 134 | }
|
124 |
| - } catch (\Magento\Framework\Exception\LocalizedException $e) { |
125 |
| - $this->messageManager->addError($e->getMessage()); |
| 135 | + } catch (LocalizedException $e) { |
| 136 | + $this->messageManager->addErrorMessage($e->getMessage()); |
126 | 137 | } catch (\Exception $e) {
|
127 |
| - $this->messageManager->addException($e, __('Some emails were not sent.')); |
| 138 | + $this->messageManager->addExceptionMessage($e, __('Some emails were not sent.')); |
128 | 139 | }
|
129 | 140 |
|
130 | 141 | // save form data
|
|
0 commit comments