Product.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SendFriend\Controller;
  7. use Magento\Framework\Exception\NotFoundException;
  8. use Magento\Framework\App\RequestInterface;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. /**
  11. * Email to a Friend Product Controller
  12. *
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. abstract class Product extends \Magento\Framework\App\Action\Action
  16. {
  17. /**
  18. * Core registry
  19. *
  20. * @var \Magento\Framework\Registry
  21. */
  22. protected $_coreRegistry = null;
  23. /**
  24. * @var \Magento\Framework\Data\Form\FormKey\Validator
  25. */
  26. protected $_formKeyValidator;
  27. /**
  28. * @var \Magento\SendFriend\Model\SendFriend
  29. */
  30. protected $sendFriend;
  31. /**
  32. * @var \Magento\Catalog\Api\ProductRepositoryInterface
  33. */
  34. protected $productRepository;
  35. /**
  36. * @param \Magento\Framework\App\Action\Context $context
  37. * @param \Magento\Framework\Registry $coreRegistry
  38. * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
  39. * @param \Magento\SendFriend\Model\SendFriend $sendFriend
  40. * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  41. */
  42. public function __construct(
  43. \Magento\Framework\App\Action\Context $context,
  44. \Magento\Framework\Registry $coreRegistry,
  45. \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
  46. \Magento\SendFriend\Model\SendFriend $sendFriend,
  47. \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  48. ) {
  49. parent::__construct($context);
  50. $this->_coreRegistry = $coreRegistry;
  51. $this->_formKeyValidator = $formKeyValidator;
  52. $this->sendFriend = $sendFriend;
  53. $this->productRepository = $productRepository;
  54. }
  55. /**
  56. * Check if module is enabled
  57. * If allow only for customer - redirect to login page
  58. *
  59. * @param RequestInterface $request
  60. * @return \Magento\Framework\App\ResponseInterface
  61. * @throws \Magento\Framework\Exception\NotFoundException
  62. */
  63. public function dispatch(RequestInterface $request)
  64. {
  65. /* @var $helper \Magento\SendFriend\Helper\Data */
  66. $helper = $this->_objectManager->get(\Magento\SendFriend\Helper\Data::class);
  67. /* @var $session \Magento\Customer\Model\Session */
  68. $session = $this->_objectManager->get(\Magento\Customer\Model\Session::class);
  69. if (!$helper->isEnabled()) {
  70. throw new NotFoundException(__('Page not found.'));
  71. }
  72. if (!$helper->isAllowForGuest() && !$session->authenticate()) {
  73. $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
  74. if ($this->getRequest()->getActionName() == 'sendemail') {
  75. $session->setBeforeAuthUrl($this->_url->getUrl('sendfriend/product/send', ['_current' => true]));
  76. $this->_objectManager->get(\Magento\Catalog\Model\Session::class)
  77. ->setSendfriendFormData($request->getPostValue());
  78. }
  79. }
  80. return parent::dispatch($request);
  81. }
  82. /**
  83. * Initialize Product Instance
  84. *
  85. * @return \Magento\Catalog\Model\Product
  86. */
  87. protected function _initProduct()
  88. {
  89. $productId = (int)$this->getRequest()->getParam('id');
  90. if (!$productId) {
  91. return false;
  92. }
  93. try {
  94. $product = $this->productRepository->getById($productId);
  95. if (!$product->isVisibleInCatalog()) {
  96. return false;
  97. }
  98. } catch (NoSuchEntityException $noEntityException) {
  99. return false;
  100. }
  101. $this->_coreRegistry->register('product', $product);
  102. return $product;
  103. }
  104. }