Skip to content

Commit 3459ebf

Browse files
committed
AC-13672: Product review modification
1 parent 45cc4db commit 3459ebf

File tree

2 files changed

+78
-46
lines changed

2 files changed

+78
-46
lines changed

app/code/Magento/Review/Controller/Product.php

Lines changed: 73 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,157 @@
11
<?php
2+
3+
declare(strict_types=1);
4+
25
/**
36
* Copyright © Magento, Inc. All rights reserved.
47
* See COPYING.txt for license details.
58
*/
69
namespace Magento\Review\Controller;
710

11+
use Magento\Catalog\Api\CategoryRepositoryInterface;
12+
use Magento\Catalog\Api\ProductRepositoryInterface;
13+
use Magento\Catalog\Model\Design;
814
use Magento\Catalog\Model\Product as CatalogProduct;
15+
use Magento\Customer\Model\Session;
16+
use Magento\Customer\Model\Url;
17+
use Magento\Framework\App\Action\Action;
18+
use Magento\Framework\App\Action\Context;
19+
use Magento\Framework\App\ObjectManager;
20+
use Magento\Framework\Registry;
921
use Magento\Framework\App\RequestInterface;
22+
use Magento\Framework\Data\Form\FormKey\Validator;
23+
use Magento\Framework\Exception\LocalizedException;
1024
use Magento\Framework\Exception\NoSuchEntityException;
25+
use Magento\Framework\Session\Generic;
26+
use Magento\Review\Helper\Data;
27+
use Magento\Review\Model\RatingFactory;
1128
use Magento\Review\Model\Review;
29+
use Magento\Review\Model\Review\Config as ReviewsConfig;
30+
use Magento\Review\Model\ReviewFactory;
31+
use Magento\Store\Model\StoreManagerInterface;
32+
use Psr\Log\LoggerInterface;
1233

1334
/**
1435
* Review controller
1536
*
1637
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1738
*/
18-
abstract class Product extends \Magento\Framework\App\Action\Action
39+
abstract class Product extends Action
1940
{
2041
/**
21-
* Core registry
42+
* Core Registry class
2243
*
23-
* @var \Magento\Framework\Registry
44+
* @var Registry
2445
*/
2546
protected $coreRegistry = null;
2647

2748
/**
2849
* Customer session model
2950
*
30-
* @var \Magento\Customer\Model\Session
51+
* @var Session
3152
*/
3253
protected $customerSession;
3354

3455
/**
3556
* Generic session
3657
*
37-
* @var \Magento\Framework\Session\Generic
58+
* @var Generic
3859
*/
3960
protected $reviewSession;
4061

4162
/**
4263
* Catalog category model
4364
*
44-
* @var \Magento\Catalog\Api\CategoryRepositoryInterface
65+
* @var CategoryRepositoryInterface
4566
*/
4667
protected $categoryRepository;
4768

4869
/**
49-
* Logger
70+
* Logger for adding logs
5071
*
51-
* @var \Psr\Log\LoggerInterface
72+
* @var LoggerInterface
5273
*/
5374
protected $logger;
5475

5576
/**
5677
* Catalog product model
5778
*
58-
* @var \Magento\Catalog\Api\ProductRepositoryInterface
79+
* @var ProductRepositoryInterface
5980
*/
6081
protected $productRepository;
6182

6283
/**
6384
* Review model
6485
*
65-
* @var \Magento\Review\Model\ReviewFactory
86+
* @var ReviewFactory
6687
*/
6788
protected $reviewFactory;
6889

6990
/**
7091
* Rating model
7192
*
72-
* @var \Magento\Review\Model\RatingFactory
93+
* @var RatingFactory
7394
*/
7495
protected $ratingFactory;
7596

7697
/**
7798
* Catalog design model
7899
*
79-
* @var \Magento\Catalog\Model\Design
100+
* @var Design
80101
*/
81102
protected $catalogDesign;
82103

83104
/**
84105
* Core model store manager interface
85106
*
86-
* @var \Magento\Store\Model\StoreManagerInterface
107+
* @var StoreManagerInterface
87108
*/
88109
protected $storeManager;
89110

90111
/**
91112
* Core form key validator
92113
*
93-
* @var \Magento\Framework\Data\Form\FormKey\Validator
114+
* @var Validator
94115
*/
95116
protected $formKeyValidator;
96117

97118
/**
98-
* @param \Magento\Framework\App\Action\Context $context
99-
* @param \Magento\Framework\Registry $coreRegistry
100-
* @param \Magento\Customer\Model\Session $customerSession
101-
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
102-
* @param \Psr\Log\LoggerInterface $logger
103-
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
104-
* @param \Magento\Review\Model\ReviewFactory $reviewFactory
105-
* @param \Magento\Review\Model\RatingFactory $ratingFactory
106-
* @param \Magento\Catalog\Model\Design $catalogDesign
107-
* @param \Magento\Framework\Session\Generic $reviewSession
108-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
109-
* @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
119+
* Review config
120+
*
121+
* @var ReviewsConfig
122+
*/
123+
protected $reviewsConfig;
124+
125+
/**
126+
* @param Context $context
127+
* @param Registry $coreRegistry
128+
* @param Session $customerSession
129+
* @param CategoryRepositoryInterface $categoryRepository
130+
* @param LoggerInterface $logger
131+
* @param ProductRepositoryInterface $productRepository
132+
* @param ReviewFactory $reviewFactory
133+
* @param RatingFactory $ratingFactory
134+
* @param Design $catalogDesign
135+
* @param Generic $reviewSession
136+
* @param StoreManagerInterface $storeManager
137+
* @param Validator $formKeyValidator
138+
* @param ReviewsConfig $reviewsConfig
110139
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
111140
*/
112141
public function __construct(
113-
\Magento\Framework\App\Action\Context $context,
114-
\Magento\Framework\Registry $coreRegistry,
115-
\Magento\Customer\Model\Session $customerSession,
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
142+
Context $context,
143+
Registry $coreRegistry,
144+
Session $customerSession,
145+
CategoryRepositoryInterface $categoryRepository,
146+
LoggerInterface $logger,
147+
ProductRepositoryInterface $productRepository,
148+
ReviewFactory $reviewFactory,
149+
RatingFactory $ratingFactory,
150+
Design $catalogDesign,
151+
Generic $reviewSession,
152+
StoreManagerInterface $storeManager,
153+
Validator $formKeyValidator,
154+
ReviewsConfig $reviewsConfig = null
125155
) {
126156
$this->storeManager = $storeManager;
127157
$this->coreRegistry = $coreRegistry;
@@ -134,7 +164,7 @@ public function __construct(
134164
$this->ratingFactory = $ratingFactory;
135165
$this->catalogDesign = $catalogDesign;
136166
$this->formKeyValidator = $formKeyValidator;
137-
167+
$this->reviewsConfig = $reviewsConfig ?: ObjectManager::getInstance()->get(ReviewsConfig::class);
138168
parent::__construct($context);
139169
}
140170

@@ -146,7 +176,7 @@ public function __construct(
146176
*/
147177
public function dispatch(RequestInterface $request)
148178
{
149-
$allowGuest = $this->_objectManager->get(\Magento\Review\Helper\Data::class)->getIsGuestAllowToWrite();
179+
$allowGuest = $this->_objectManager->get(Data::class)->getIsGuestAllowToWrite();
150180
if (!$request->isDispatched()) {
151181
return parent::dispatch($request);
152182
}
@@ -161,7 +191,7 @@ public function dispatch(RequestInterface $request)
161191
$this->_redirect->getRefererUrl()
162192
);
163193
$this->getResponse()->setRedirect(
164-
$this->_objectManager->get(\Magento\Customer\Model\Url::class)->getLoginUrl()
194+
$this->_objectManager->get(Url::class)->getLoginUrl()
165195
);
166196
}
167197
}
@@ -196,7 +226,7 @@ protected function initProduct()
196226
'review_controller_product_init_after',
197227
['product' => $product, 'controller_action' => $this]
198228
);
199-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
229+
} catch (LocalizedException $e) {
200230
$this->logger->critical($e);
201231
return false;
202232
}
@@ -205,8 +235,7 @@ protected function initProduct()
205235
}
206236

207237
/**
208-
* Load product model with data by passed id.
209-
* Return false if product was not loaded or has incorrect status.
238+
* Load product model with data by passed id. Return false if product was not loaded or has incorrect status.
210239
*
211240
* @param int $productId
212241
* @return bool|CatalogProduct

app/code/Magento/Review/Controller/Product/Post.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
declare(strict_types=1);
4+
25
/**
36
* Copyright © Magento, Inc. All rights reserved.
47
* See COPYING.txt for license details.
@@ -11,7 +14,7 @@
1114
use Magento\Review\Model\Review;
1215

1316
/**
14-
* Class Post
17+
* Class Post for posting the review
1518
*/
1619
class Post extends ProductController implements HttpPostActionInterface
1720
{
@@ -26,7 +29,7 @@ public function execute()
2629
{
2730
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
2831
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
29-
if (!$this->formKeyValidator->validate($this->getRequest())) {
32+
if (false === $this->reviewsConfig->isEnabled() || !$this->formKeyValidator->validate($this->getRequest())) {
3033
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
3134
return $resultRedirect;
3235
}

0 commit comments

Comments
 (0)