Skip to content

Commit 51e2fbc

Browse files
committed
Small refactoring to re-implement the customer reviews
1 parent ddf7d53 commit 51e2fbc

File tree

7 files changed

+69
-131
lines changed

7 files changed

+69
-131
lines changed

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

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

app/code/Magento/Review/Service/GetReviewAverageRatingService.php

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

app/code/Magento/ReviewGraphQl/Mapper/ReviewDataMapper.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@ class ReviewDataMapper
1818
/**
1919
* Mapping the review data
2020
*
21-
* @param Review|Product $review
21+
* @param Review $review
2222
*
2323
* @return array
2424
*/
25-
public function map($review): array
25+
public function map(Review $review): array
2626
{
2727
return [
2828
'summary' => $review->getData('title'),
2929
'text' => $review->getData('detail'),
3030
'nickname' => $review->getData('nickname'),
3131
'created_at' => $review->getData('created_at'),
3232
'rating_votes' => $review->getData('rating_votes'),
33-
'sku' => $review->getSku()
33+
'sku' => $review->getSku(),
34+
'model' => $review
3435
];
3536
}
3637
}

app/code/Magento/ReviewGraphQl/Model/DataProvider/CustomerReviewsDataProvider.php

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,26 @@
77

88
namespace Magento\ReviewGraphQl\Model\DataProvider;
99

10-
use Magento\Review\Model\ResourceModel\Review\Product\Collection as ProductReviewsCollection;
11-
use Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory;
12-
use Magento\Review\Model\Review\AddRatingVotesToCustomerReviews;
13-
10+
use Magento\Review\Model\ResourceModel\Review\Collection as ReviewsCollection;
11+
use Magento\Review\Model\ResourceModel\Review\CollectionFactory as ReviewsCollectionFactory;
12+
use Magento\Review\Model\Review;
1413
/**
1514
* Provides customer reviews
1615
*/
1716
class CustomerReviewsDataProvider
1817
{
1918
/**
20-
* @var CollectionFactory
19+
* @var ReviewsCollectionFactory
2120
*/
2221
private $collectionFactory;
2322

2423
/**
25-
* @var AddRatingVotesToCustomerReviews
26-
*/
27-
private $addRatingVotesToCustomerReviews;
28-
29-
/**
30-
* @param CollectionFactory $collectionFactory
31-
* @param AddRatingVotesToCustomerReviews $addRatingVotesToCustomerReviews
24+
* @param ReviewsCollectionFactory $collectionFactory
3225
*/
3326
public function __construct(
34-
CollectionFactory $collectionFactory,
35-
AddRatingVotesToCustomerReviews $addRatingVotesToCustomerReviews
27+
ReviewsCollectionFactory $collectionFactory
3628
) {
3729
$this->collectionFactory = $collectionFactory;
38-
$this->addRatingVotesToCustomerReviews = $addRatingVotesToCustomerReviews;
3930
}
4031

4132
/**
@@ -45,17 +36,23 @@ public function __construct(
4536
* @param int $currentPage
4637
* @param int $pageSize
4738
*
48-
* @return ProductReviewsCollection
39+
* @return ReviewsCollection
4940
*/
50-
public function getData(int $customerId, int $currentPage, int $pageSize): ProductReviewsCollection
41+
public function getData(int $customerId, int $currentPage, int $pageSize): ReviewsCollection
5142
{
52-
/** @var ProductReviewsCollection $reviewsCollection */
43+
/** @var ReviewsCollection $reviewsCollection */
5344
$reviewsCollection = $this->collectionFactory->create();
54-
$reviewsCollection->addCustomerFilter($customerId)
45+
$reviewsCollection->addStatusFilter(Review::STATUS_APPROVED)
46+
->addCustomerFilter($customerId)
5547
->setPageSize($pageSize)
5648
->setCurPage($currentPage)
5749
->setDateOrder();
58-
$this->addRatingVotesToCustomerReviews->execute($reviewsCollection);
50+
$reviewsCollection->getSelect()->join(
51+
['cpe' => $reviewsCollection->getTable('catalog_product_entity')],
52+
'cpe.entity_id = main_table.entity_pk_value',
53+
['sku']
54+
);
55+
$reviewsCollection->addRateVotes();
5956

6057
return $reviewsCollection;
6158
}

app/code/Magento/ReviewGraphQl/Model/DataProvider/ReviewRatingsDataProvider.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,44 @@
77

88
namespace Magento\ReviewGraphQl\Model\DataProvider;
99

10+
use Magento\Review\Model\ResourceModel\Rating\Option\Vote\Collection as VoteCollection;
11+
use Magento\Review\Model\ResourceModel\Rating\Option\Vote\CollectionFactory as VoteCollectionFactory;
12+
1013
/**
1114
* Provides rating votes
1215
*/
1316
class ReviewRatingsDataProvider
1417
{
18+
/**
19+
* @var VoteCollectionFactory
20+
*/
21+
private $voteCollectionFactory;
22+
23+
/**
24+
* @param VoteCollectionFactory $voteCollectionFactory
25+
*/
26+
public function __construct(VoteCollectionFactory $voteCollectionFactory)
27+
{
28+
$this->voteCollectionFactory = $voteCollectionFactory;
29+
}
30+
1531
/**
1632
* Providing rating votes
1733
*
18-
* @param array $ratingVotes
34+
* @param int $reviewId
1935
*
2036
* @return array
2137
*/
22-
public function getData(array $ratingVotes): array
38+
public function getData(int $reviewId): array
2339
{
40+
/** @var VoteCollection $ratingVotes */
41+
$ratingVotes = $this->voteCollectionFactory->create();
42+
$ratingVotes->setReviewFilter($reviewId);
43+
$ratingVotes->addRatingInfo();
44+
2445
$data = [];
2546

26-
foreach ($ratingVotes as $ratingVote) {
47+
foreach ($ratingVotes->getItems() as $ratingVote) {
2748
$data[] = [
2849
'name' => $ratingVote->getData('rating_code'),
2950
'value' => $ratingVote->getData('value')

app/code/Magento/ReviewGraphQl/Model/Resolver/Product/Review/AverageRating.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@
1313
use Magento\Framework\GraphQl\Query\Resolver\Value;
1414
use Magento\Framework\GraphQl\Query\ResolverInterface;
1515
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16-
use Magento\Review\Model\ResourceModel\Rating\Option\Vote\Collection as VoteCollection;
17-
use Magento\Review\Service\GetReviewAverageRatingService;
16+
use Magento\Review\Model\RatingFactory;
17+
use Magento\Review\Model\Review;
1818

1919
/**
2020
* Review average rating resolver
2121
*/
2222
class AverageRating implements ResolverInterface
2323
{
2424
/**
25-
* @var GetReviewAverageRatingService
25+
* @var RatingFactory
2626
*/
27-
private $getReviewAverageRatingService;
27+
private $ratingFactory;
2828

2929
/**
30-
* @param GetReviewAverageRatingService $getReviewAverageRatingService
30+
* @param RatingFactory $ratingFactory
3131
*/
3232
public function __construct(
33-
GetReviewAverageRatingService $getReviewAverageRatingService
33+
RatingFactory $ratingFactory
3434
) {
35-
$this->getReviewAverageRatingService = $getReviewAverageRatingService;
35+
$this->ratingFactory = $ratingFactory;
3636
}
3737

3838
/**
@@ -57,13 +57,19 @@ public function resolve(
5757
array $value = null,
5858
array $args = null
5959
) {
60-
if (!isset($value['rating_votes'])) {
61-
throw new GraphQlInputException(__('Value must contain "rating_votes" property.'));
60+
if (!isset($value['model'])) {
61+
throw new GraphQlInputException(__('Value must contain "model" property.'));
6262
}
6363

64-
/** @var VoteCollection $ratingVotes */
65-
$ratingVotes = $value['rating_votes'];
64+
/** @var Review $review */
65+
$review = $value['model'];
66+
$summary = $this->ratingFactory->create()->getReviewSummary($review->getId());
67+
$averageRating = $summary->getSum();
6668

67-
return $this->getReviewAverageRatingService->execute($ratingVotes->getItems());
69+
if ($summary->getSum() > 0) {
70+
$averageRating = (float) number_format($summary->getSum() / $summary->getCount() / 20, 2);
71+
}
72+
73+
return $averageRating;
6874
}
6975
}

app/code/Magento/ReviewGraphQl/Model/Resolver/Product/Review/RatingBreakdown.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Magento\Framework\GraphQl\Query\Resolver\Value;
1414
use Magento\Framework\GraphQl\Query\ResolverInterface;
1515
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16-
use Magento\Review\Model\ResourceModel\Rating\Option\Vote\Collection as VoteCollection;
16+
use Magento\Review\Model\Review;
1717
use Magento\ReviewGraphQl\Model\DataProvider\ReviewRatingsDataProvider;
1818

1919
/**
@@ -57,13 +57,13 @@ public function resolve(
5757
array $value = null,
5858
array $args = null
5959
) {
60-
if (!isset($value['rating_votes'])) {
61-
throw new GraphQlInputException(__('Value must contain "rating_votes" property.'));
60+
if (!isset($value['model'])) {
61+
throw new GraphQlInputException(__('Value must contain "model" property.'));
6262
}
6363

64-
/** @var VoteCollection $ratingVotes */
65-
$ratingVotes = $value['rating_votes'];
64+
/** @var Review $review */
65+
$review = $value['model'];
6666

67-
return $this->reviewRatingsDataProvider->getData($ratingVotes->getItems());
67+
return $this->reviewRatingsDataProvider->getData((int) $review->getId());
6868
}
6969
}

0 commit comments

Comments
 (0)