Skip to content

Commit 2b11ea6

Browse files
committed
Refactoring review module (magento/community-features#57)
1 parent 27207cf commit 2b11ea6

14 files changed

+269
-197
lines changed

app/code/Magento/Review/Block/Customer/View.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ public function getRating()
160160
/**
161161
* Get rating summary
162162
*
163+
* @deprecated
163164
* @return array
164165
*/
165166
public function getRatingSummary()
@@ -200,25 +201,6 @@ public function dateFormat($date)
200201
return $this->formatDate($date, \IntlDateFormatter::LONG);
201202
}
202203

203-
/**
204-
* Get product reviews summary
205-
*
206-
* @param \Magento\Catalog\Model\Product $product
207-
* @param bool $templateType
208-
* @param bool $displayIfNoReviews
209-
* @return string
210-
*/
211-
public function getReviewsSummaryHtml(
212-
\Magento\Catalog\Model\Product $product,
213-
$templateType = false,
214-
$displayIfNoReviews = false
215-
) {
216-
if (!$product->getRatingSummary()) {
217-
$this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
218-
}
219-
return parent::getReviewsSummaryHtml($product, $templateType, $displayIfNoReviews);
220-
}
221-
222204
/**
223205
* @return string
224206
*/

app/code/Magento/Review/Block/Product/Compare/ListCompare/Plugin/Review.php

Lines changed: 0 additions & 58 deletions
This file was deleted.

app/code/Magento/Review/Block/Product/ReviewRenderer.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
* Copyright © Magento, Inc. All rights reserved.
66
* See COPYING.txt for license details.
77
*/
8+
89
namespace Magento\Review\Block\Product;
910

1011
use Magento\Catalog\Block\Product\ReviewRendererInterface;
1112
use Magento\Catalog\Model\Product;
13+
use Magento\Framework\App\ObjectManager;
14+
use Magento\Review\Model\ReviewSummaryFactory;
1215
use Magento\Review\Observer\PredispatchReviewObserver;
1316

1417
/**
@@ -33,17 +36,25 @@ class ReviewRenderer extends \Magento\Framework\View\Element\Template implements
3336
*/
3437
protected $_reviewFactory;
3538

39+
/**
40+
* @var ReviewSummaryFactory
41+
*/
42+
private $reviewSummaryFactory;
43+
3644
/**
3745
* @param \Magento\Framework\View\Element\Template\Context $context
3846
* @param \Magento\Review\Model\ReviewFactory $reviewFactory
47+
* @param ReviewSummaryFactory $reviewSummaryFactory
3948
* @param array $data
4049
*/
4150
public function __construct(
4251
\Magento\Framework\View\Element\Template\Context $context,
4352
\Magento\Review\Model\ReviewFactory $reviewFactory,
44-
array $data = []
53+
array $data = [],
54+
ReviewSummaryFactory $reviewSummaryFactory = null
4555
) {
4656
$this->_reviewFactory = $reviewFactory;
57+
$this->reviewSummaryFactory = $reviewSummaryFactory ?? ObjectManager::getInstance()->get(ReviewSummaryFactory::class);
4758
parent::__construct($context, $data);
4859
}
4960

@@ -52,7 +63,7 @@ public function __construct(
5263
*
5364
* @return string
5465
*/
55-
public function isReviewEnabled() : string
66+
public function isReviewEnabled(): string
5667
{
5768
return $this->_scopeConfig->getValue(
5869
PredispatchReviewObserver::XML_PATH_REVIEW_ACTIVE,
@@ -68,14 +79,19 @@ public function isReviewEnabled() : string
6879
* @param bool $displayIfNoReviews
6980
*
7081
* @return string
82+
* @throws \Magento\Framework\Exception\LocalizedException
83+
* @throws \Magento\Framework\Exception\NoSuchEntityException
7184
*/
7285
public function getReviewsSummaryHtml(
7386
\Magento\Catalog\Model\Product $product,
7487
$templateType = self::DEFAULT_VIEW,
7588
$displayIfNoReviews = false
7689
) {
77-
if (!$product->getRatingSummary()) {
78-
$this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
90+
if ($product->getRatingSummary() === null) {
91+
$this->reviewSummaryFactory->create()->appendSummaryDataToObject(
92+
$product,
93+
$this->_storeManager->getStore()->getId()
94+
);
7995
}
8096

8197
if (!$product->getRatingSummary() && !$displayIfNoReviews) {
@@ -101,7 +117,7 @@ public function getReviewsSummaryHtml(
101117
*/
102118
public function getRatingSummary()
103119
{
104-
return $this->getProduct()->getRatingSummary()->getRatingSummary();
120+
return $this->getProduct()->getRatingSummary();
105121
}
106122

107123
/**
@@ -111,7 +127,7 @@ public function getRatingSummary()
111127
*/
112128
public function getReviewsCount()
113129
{
114-
return $this->getProduct()->getRatingSummary()->getReviewsCount();
130+
return $this->getProduct()->getReviewsCount();
115131
}
116132

117133
/**

app/code/Magento/Review/Block/View.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public function getRating()
119119
/**
120120
* Retrieve rating summary for current product
121121
*
122+
* @deprecated
122123
* @return string
123124
*/
124125
public function getRatingSummary()
@@ -160,23 +161,4 @@ public function dateFormat($date)
160161
{
161162
return $this->formatDate($date, \IntlDateFormatter::LONG);
162163
}
163-
164-
/**
165-
* Get product reviews summary
166-
*
167-
* @param \Magento\Catalog\Model\Product $product
168-
* @param bool $templateType
169-
* @param bool $displayIfNoReviews
170-
* @return string
171-
*/
172-
public function getReviewsSummaryHtml(
173-
\Magento\Catalog\Model\Product $product,
174-
$templateType = false,
175-
$displayIfNoReviews = false
176-
) {
177-
if (!$product->getRatingSummary()) {
178-
$this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
179-
}
180-
return parent::getReviewsSummaryHtml($product, $templateType, $displayIfNoReviews);
181-
}
182164
}

app/code/Magento/Review/Model/ResourceModel/Review/Summary.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Review\Model\ResourceModel\Review;
78

89
use Magento\Framework\Model\AbstractModel;
@@ -73,4 +74,43 @@ public function reAggregate($summary)
7374
}
7475
return $this;
7576
}
77+
78+
/**
79+
* Append review summary fields to product collection
80+
*
81+
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection
82+
* @param $storeId
83+
* @param $entityCode
84+
* @return Summary
85+
* @throws \Magento\Framework\Exception\LocalizedException
86+
*/
87+
public function appendSummaryFieldsToCollection(
88+
\Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection,
89+
$storeId,
90+
$entityCode
91+
) {
92+
if (!$productCollection->isLoaded()) {
93+
$summaryEntitySubSelect = $this->getConnection()->select();
94+
$summaryEntitySubSelect
95+
->from(
96+
['review_entity' => $this->getTable('review_entity')],
97+
['entity_id']
98+
)->where(
99+
'entity_code = ?',
100+
$entityCode
101+
);
102+
$joinCond = new \Zend_Db_Expr("e.entity_id = review_summary.entity_pk_value AND review_summary.store_id = {$storeId} AND review_summary.entity_type = ({$summaryEntitySubSelect})");
103+
$productCollection->getSelect()
104+
->joinLeft(
105+
['review_summary' => $this->getMainTable()],
106+
$joinCond,
107+
[
108+
'reviews_count' => new \Zend_Db_Expr("IFNULL(review_summary.reviews_count, 0)"),
109+
'rating_summary' => new \Zend_Db_Expr("IFNULL(review_summary.rating_summary, 0)")
110+
]
111+
);
112+
}
113+
114+
return $this;
115+
}
76116
}

app/code/Magento/Review/Model/Review.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Review\Model;
78

89
use Magento\Framework\DataObject;
@@ -100,6 +101,7 @@ class Review extends \Magento\Framework\Model\AbstractModel implements IdentityI
100101
/**
101102
* Review model summary
102103
*
104+
* @deprecated Summary factory injected as separate property
103105
* @var \Magento\Review\Model\Review\Summary
104106
*/
105107
protected $_reviewSummary;
@@ -214,6 +216,7 @@ public function aggregate()
214216
/**
215217
* Get entity summary
216218
*
219+
* @deprecated
217220
* @param Product $product
218221
* @param int $storeId
219222
* @return void
@@ -301,10 +304,12 @@ public function afterDeleteCommit()
301304
}
302305

303306
/**
304-
* Append review summary to product collection
307+
* Append review summary data object to product collection
305308
*
309+
* @deprecated
306310
* @param ProductCollection $collection
307311
* @return $this
312+
* @throws \Magento\Framework\Exception\NoSuchEntityException
308313
*/
309314
public function appendSummary($collection)
310315
{
@@ -356,7 +361,7 @@ public function isAvailableOnStore($store = null)
356361
{
357362
$store = $this->_storeManager->getStore($store);
358363
if ($store) {
359-
return in_array($store->getId(), (array) $this->getStores());
364+
return in_array($store->getId(), (array)$this->getStores());
360365
}
361366
return false;
362367
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Review\Model;
9+
10+
use Magento\Framework\Model\AbstractModel;
11+
use Magento\Review\Model\ResourceModel\Review\Summary\CollectionFactory as SummaryCollectionFactory;
12+
13+
class ReviewSummary
14+
{
15+
/**
16+
* @var SummaryCollectionFactory
17+
*/
18+
private $summaryCollectionFactory;
19+
20+
public function __construct(
21+
SummaryCollectionFactory $sumColFactory
22+
) {
23+
$this->summaryCollectionFactory = $sumColFactory;
24+
}
25+
26+
/**
27+
* Append review summary data to product
28+
*
29+
* @param AbstractModel $object
30+
* @param $storeId
31+
* @param int $entityType
32+
*/
33+
public function appendSummaryDataToObject(AbstractModel $object, $storeId, $entityType = 1): void
34+
{
35+
$summary = $this->summaryCollectionFactory->create()
36+
->addEntityFilter($object->getId(), $entityType)
37+
->addStoreFilter($storeId)
38+
->getFirstItem();
39+
$object->addData([
40+
'reviews_count' => $summary->getData('reviews_count'),
41+
'rating_summary' => $summary->getData('rating_summary')
42+
]);
43+
}
44+
}

0 commit comments

Comments
 (0)