/
Product.php
239 lines (211 loc) · 7.33 KB
/
Product.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Review\Controller;
use Magento\Catalog\Model\Product as CatalogProduct;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Review\Model\Review;
/**
* Review controller
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
abstract class Product extends \Magento\Framework\App\Action\Action
{
/**
* Core registry
*
* @var \Magento\Framework\Registry
*/
protected $coreRegistry = null;
/**
* Customer session model
*
* @var \Magento\Customer\Model\Session
*/
protected $customerSession;
/**
* Generic session
*
* @var \Magento\Framework\Session\Generic
*/
protected $reviewSession;
/**
* Catalog catgory model
*
* @var \Magento\Catalog\Api\CategoryRepositoryInterface
*/
protected $categoryRepository;
/**
* Logger
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* Catalog product model
*
* @var \Magento\Catalog\Api\ProductRepositoryInterface
*/
protected $productRepository;
/**
* Review model
*
* @var \Magento\Review\Model\ReviewFactory
*/
protected $reviewFactory;
/**
* Rating model
*
* @var \Magento\Review\Model\RatingFactory
*/
protected $ratingFactory;
/**
* Catalog design model
*
* @var \Magento\Catalog\Model\Design
*/
protected $catalogDesign;
/**
* Core model store manager interface
*
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
* Core form key validator
*
* @var \Magento\Framework\Data\Form\FormKey\Validator
*/
protected $formKeyValidator;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\Registry $coreRegistry
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
* @param \Magento\Review\Model\ReviewFactory $reviewFactory
* @param \Magento\Review\Model\RatingFactory $ratingFactory
* @param \Magento\Catalog\Model\Design $catalogDesign
* @param \Magento\Framework\Session\Generic $reviewSession
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __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
) {
$this->storeManager = $storeManager;
$this->coreRegistry = $coreRegistry;
$this->customerSession = $customerSession;
$this->reviewSession = $reviewSession;
$this->categoryRepository = $categoryRepository;
$this->logger = $logger;
$this->productRepository = $productRepository;
$this->reviewFactory = $reviewFactory;
$this->ratingFactory = $ratingFactory;
$this->catalogDesign = $catalogDesign;
$this->formKeyValidator = $formKeyValidator;
parent::__construct($context);
}
/**
* Dispatch request
*
* @param RequestInterface $request
* @return \Magento\Framework\App\ResponseInterface
*/
public function dispatch(RequestInterface $request)
{
$allowGuest = $this->_objectManager->get(\Magento\Review\Helper\Data::class)->getIsGuestAllowToWrite();
if (!$request->isDispatched()) {
return parent::dispatch($request);
}
if (!$allowGuest && $request->getActionName() == 'post' && $request->isPost()) {
if (!$this->customerSession->isLoggedIn()) {
$this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
$this->customerSession->setBeforeAuthUrl($this->_url->getUrl('*/*/*', ['_current' => true]));
$this->reviewSession->setFormData(
$request->getPostValue()
)->setRedirectUrl(
$this->_redirect->getRefererUrl()
);
$this->getResponse()->setRedirect(
$this->_objectManager->get(\Magento\Customer\Model\Url::class)->getLoginUrl()
);
}
}
return parent::dispatch($request);
}
/**
* Initialize and check product
*
* @return \Magento\Catalog\Model\Product|bool
*/
protected function initProduct()
{
$this->_eventManager->dispatch('review_controller_product_init_before', ['controller_action' => $this]);
$categoryId = (int)$this->getRequest()->getParam('category', false);
$productId = (int)$this->getRequest()->getParam('id');
$product = $this->loadProduct($productId);
if (!$product) {
return false;
}
if ($categoryId) {
$category = $this->categoryRepository->get($categoryId);
$this->coreRegistry->register('current_category', $category);
}
try {
$this->_eventManager->dispatch('review_controller_product_init', ['product' => $product]);
$this->_eventManager->dispatch(
'review_controller_product_init_after',
['product' => $product, 'controller_action' => $this]
);
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->logger->critical($e);
return false;
}
return $product;
}
/**
* Load product model with data by passed id.
* Return false if product was not loaded or has incorrect status.
*
* @param int $productId
* @return bool|CatalogProduct
*/
protected function loadProduct($productId)
{
if (!$productId) {
return false;
}
try {
$product = $this->productRepository->getById($productId);
if (!in_array($this->storeManager->getStore()->getWebsiteId(), $product->getWebsiteIds())) {
throw new NoSuchEntityException();
}
if (!$product->isVisibleInCatalog() || !$product->isVisibleInSiteVisibility()) {
throw new NoSuchEntityException();
}
} catch (NoSuchEntityException $noEntityException) {
return false;
}
$this->coreRegistry->register('current_product', $product);
$this->coreRegistry->register('product', $product);
return $product;
}
}