Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Product.php
Go to the documentation of this file.
1 <?php
7 
12 
19 {
25  protected $coreRegistry = null;
26 
32  protected $customerSession;
33 
39  protected $reviewSession;
40 
47 
53  protected $logger;
54 
60  protected $productRepository;
61 
67  protected $reviewFactory;
68 
74  protected $ratingFactory;
75 
81  protected $catalogDesign;
82 
88  protected $storeManager;
89 
95  protected $formKeyValidator;
96 
112  public function __construct(
113  \Magento\Framework\App\Action\Context $context,
114  \Magento\Framework\Registry $coreRegistry,
116  \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
117  \Psr\Log\LoggerInterface $logger,
118  \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
119  \Magento\Review\Model\ReviewFactory $reviewFactory,
120  \Magento\Review\Model\RatingFactory $ratingFactory,
121  \Magento\Catalog\Model\Design $catalogDesign,
122  \Magento\Framework\Session\Generic $reviewSession,
123  \Magento\Store\Model\StoreManagerInterface $storeManager,
124  \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
125  ) {
126  $this->storeManager = $storeManager;
127  $this->coreRegistry = $coreRegistry;
128  $this->customerSession = $customerSession;
129  $this->reviewSession = $reviewSession;
130  $this->categoryRepository = $categoryRepository;
131  $this->logger = $logger;
132  $this->productRepository = $productRepository;
133  $this->reviewFactory = $reviewFactory;
134  $this->ratingFactory = $ratingFactory;
135  $this->catalogDesign = $catalogDesign;
136  $this->formKeyValidator = $formKeyValidator;
137 
138  parent::__construct($context);
139  }
140 
148  {
149  $allowGuest = $this->_objectManager->get(\Magento\Review\Helper\Data::class)->getIsGuestAllowToWrite();
150  if (!$request->isDispatched()) {
151  return parent::dispatch($request);
152  }
153 
154  if (!$allowGuest && $request->getActionName() == 'post' && $request->isPost()) {
155  if (!$this->customerSession->isLoggedIn()) {
156  $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
157  $this->customerSession->setBeforeAuthUrl($this->_url->getUrl('*/*/*', ['_current' => true]));
158  $this->reviewSession->setFormData(
159  $request->getPostValue()
160  )->setRedirectUrl(
161  $this->_redirect->getRefererUrl()
162  );
163  $this->getResponse()->setRedirect(
164  $this->_objectManager->get(\Magento\Customer\Model\Url::class)->getLoginUrl()
165  );
166  }
167  }
168 
169  return parent::dispatch($request);
170  }
171 
177  protected function initProduct()
178  {
179  $this->_eventManager->dispatch('review_controller_product_init_before', ['controller_action' => $this]);
180  $categoryId = (int)$this->getRequest()->getParam('category', false);
181  $productId = (int)$this->getRequest()->getParam('id');
182 
183  $product = $this->loadProduct($productId);
184  if (!$product) {
185  return false;
186  }
187 
188  if ($categoryId) {
189  $category = $this->categoryRepository->get($categoryId);
190  $this->coreRegistry->register('current_category', $category);
191  }
192 
193  try {
194  $this->_eventManager->dispatch('review_controller_product_init', ['product' => $product]);
195  $this->_eventManager->dispatch(
196  'review_controller_product_init_after',
197  ['product' => $product, 'controller_action' => $this]
198  );
199  } catch (\Magento\Framework\Exception\LocalizedException $e) {
200  $this->logger->critical($e);
201  return false;
202  }
203 
204  return $product;
205  }
206 
214  protected function loadProduct($productId)
215  {
216  if (!$productId) {
217  return false;
218  }
219 
220  try {
221  $product = $this->productRepository->getById($productId);
222 
223  if (!in_array($this->storeManager->getStore()->getWebsiteId(), $product->getWebsiteIds())) {
224  throw new NoSuchEntityException();
225  }
226 
227  if (!$product->isVisibleInCatalog() || !$product->isVisibleInSiteVisibility()) {
228  throw new NoSuchEntityException();
229  }
230  } catch (NoSuchEntityException $noEntityException) {
231  return false;
232  }
233 
234  $this->coreRegistry->register('current_product', $product);
235  $this->coreRegistry->register('product', $product);
236 
237  return $product;
238  }
239 }
_redirect($path, $arguments=[])
Definition: Action.php:167
__construct(\Magento\Framework\App\Action\Context $context, \Magento\Framework\Registry $coreRegistry, \Magento\Customer\Model\Session $customerSession, \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository, \Psr\Log\LoggerInterface $logger, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Review\Model\ReviewFactory $reviewFactory, \Magento\Review\Model\RatingFactory $ratingFactory, \Magento\Catalog\Model\Design $catalogDesign, \Magento\Framework\Session\Generic $reviewSession, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator)
Definition: Product.php:112
dispatch(RequestInterface $request)
Definition: Product.php:147