Skip to content

Commit 4740e78

Browse files
ENGCOM-7508: Adding Product Reviews GraphQl support #27882
- Merge Pull Request #27882 from eduard13/magento2:feature/review-graphql-261 - Merged commits: 1. 9365b57 2. ddf7d53 3. 51e2fbc 4. 0564f78 5. 1d75c53 6. e21d3a8 7. dd796f3 8. 1b7bade 9. 9b8912e 10. 20dfcc6 11. 5809bee 12. 4fedd42 13. 64f9f00 14. 5a4b007 15. 3592cf9 16. 7089708 17. 017da27 18. a020625 19. 0111b2d 20. f168ca6 21. f443741 22. 11c7fd4 23. c9184f6 24. b974b48 25. de8598c 26. 9230db6
2 parents f8f1e5e + 9230db6 commit 4740e78

28 files changed

+1981
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\Review;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Store\Model\ScopeInterface;
12+
13+
/**
14+
* Provides reviews configuration
15+
*/
16+
class Config
17+
{
18+
const XML_PATH_REVIEW_ACTIVE = 'catalog/review/active';
19+
20+
/**
21+
* @var ScopeConfigInterface
22+
*/
23+
private $scopeConfig;
24+
25+
/**
26+
* @param ScopeConfigInterface $scopeConfig
27+
*/
28+
public function __construct(
29+
ScopeConfigInterface $scopeConfig
30+
) {
31+
$this->scopeConfig = $scopeConfig;
32+
}
33+
34+
/**
35+
* Check whether the reviews are enabled or not
36+
*
37+
* @return bool
38+
*/
39+
public function isEnabled(): bool
40+
{
41+
return $this->scopeConfig->isSetFlag(
42+
self::XML_PATH_REVIEW_ACTIVE,
43+
ScopeInterface::SCOPE_STORES
44+
);
45+
}
46+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\ReviewGraphQl\Mapper;
9+
10+
use Magento\Catalog\Model\Product;
11+
use Magento\Review\Model\Review;
12+
13+
/**
14+
* Converts the review data from review object to an associative array
15+
*/
16+
class ReviewDataMapper
17+
{
18+
/**
19+
* Mapping the review data
20+
*
21+
* @param Review $review
22+
*
23+
* @return array
24+
*/
25+
public function map(Review $review): array
26+
{
27+
return [
28+
'summary' => $review->getData('title'),
29+
'text' => $review->getData('detail'),
30+
'nickname' => $review->getData('nickname'),
31+
'created_at' => $review->getData('created_at'),
32+
'sku' => $review->getSku(),
33+
'model' => $review
34+
];
35+
}
36+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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\ReviewGraphQl\Model\DataProvider;
9+
10+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
11+
use Magento\Review\Model\ResourceModel\Review\Collection as ReviewCollection;
12+
use Magento\Review\Model\ResourceModel\Review\Product\Collection as ProductCollection;
13+
use Magento\ReviewGraphQl\Mapper\ReviewDataMapper;
14+
15+
/**
16+
* Provides aggregated reviews result
17+
*
18+
* The following class prepares the GraphQl endpoints' result for Customer and Product reviews
19+
*/
20+
class AggregatedReviewsDataProvider
21+
{
22+
/**
23+
* @var ReviewDataMapper
24+
*/
25+
private $reviewDataMapper;
26+
27+
/**
28+
* @param ReviewDataMapper $reviewDataMapper
29+
*/
30+
public function __construct(ReviewDataMapper $reviewDataMapper)
31+
{
32+
$this->reviewDataMapper = $reviewDataMapper;
33+
}
34+
35+
/**
36+
* Get reviews result
37+
*
38+
* @param ProductCollection|ReviewCollection $reviewsCollection
39+
*
40+
* @return array
41+
*/
42+
public function getData($reviewsCollection): array
43+
{
44+
if ($reviewsCollection->getPageSize()) {
45+
$maxPages = ceil($reviewsCollection->getSize() / $reviewsCollection->getPageSize());
46+
} else {
47+
$maxPages = 0;
48+
}
49+
50+
$currentPage = $reviewsCollection->getCurPage();
51+
if ($reviewsCollection->getCurPage() > $maxPages && $reviewsCollection->getSize() > 0) {
52+
$currentPage = new GraphQlInputException(
53+
__(
54+
'currentPage value %1 specified is greater than the number of pages available.',
55+
[$maxPages]
56+
)
57+
);
58+
}
59+
60+
$items = [];
61+
foreach ($reviewsCollection->getItems() as $item) {
62+
$items[] = $this->reviewDataMapper->map($item);
63+
}
64+
65+
return [
66+
'total_count' => $reviewsCollection->getSize(),
67+
'items' => $items,
68+
'page_info' => [
69+
'page_size' => $reviewsCollection->getPageSize(),
70+
'current_page' => $currentPage,
71+
'total_pages' => $maxPages
72+
]
73+
];
74+
}
75+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\ReviewGraphQl\Model\DataProvider;
9+
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;
13+
14+
/**
15+
* Provides customer reviews
16+
*/
17+
class CustomerReviewsDataProvider
18+
{
19+
/**
20+
* @var ReviewsCollectionFactory
21+
*/
22+
private $collectionFactory;
23+
24+
/**
25+
* @param ReviewsCollectionFactory $collectionFactory
26+
*/
27+
public function __construct(
28+
ReviewsCollectionFactory $collectionFactory
29+
) {
30+
$this->collectionFactory = $collectionFactory;
31+
}
32+
33+
/**
34+
* Get customer reviews
35+
*
36+
* @param int $customerId
37+
* @param int $currentPage
38+
* @param int $pageSize
39+
*
40+
* @return ReviewsCollection
41+
*/
42+
public function getData(int $customerId, int $currentPage, int $pageSize): ReviewsCollection
43+
{
44+
/** @var ReviewsCollection $reviewsCollection */
45+
$reviewsCollection = $this->collectionFactory->create();
46+
$reviewsCollection
47+
->addCustomerFilter($customerId)
48+
->setPageSize($pageSize)
49+
->setCurPage($currentPage)
50+
->setDateOrder();
51+
$reviewsCollection->getSelect()->join(
52+
['cpe' => $reviewsCollection->getTable('catalog_product_entity')],
53+
'cpe.entity_id = main_table.entity_pk_value',
54+
['sku']
55+
);
56+
$reviewsCollection->addRateVotes();
57+
58+
return $reviewsCollection;
59+
}
60+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\ReviewGraphQl\Model\DataProvider;
9+
10+
use Magento\Review\Model\ResourceModel\Review\Collection;
11+
use Magento\Review\Model\ResourceModel\Review\CollectionFactory;
12+
use Magento\Review\Model\Review;
13+
14+
/**
15+
* Provides product reviews
16+
*/
17+
class ProductReviewsDataProvider
18+
{
19+
/**
20+
* @var CollectionFactory
21+
*/
22+
private $collectionFactory;
23+
24+
/**
25+
* @param CollectionFactory $collectionFactory
26+
*/
27+
public function __construct(
28+
CollectionFactory $collectionFactory
29+
) {
30+
$this->collectionFactory = $collectionFactory;
31+
}
32+
33+
/**
34+
* Get product reviews
35+
*
36+
* @param int $productId
37+
* @param int $currentPage
38+
* @param int $pageSize
39+
*
40+
* @return Collection
41+
*/
42+
public function getData(int $productId, int $currentPage, int $pageSize): Collection
43+
{
44+
/** @var Collection $reviewsCollection */
45+
$reviewsCollection = $this->collectionFactory->create()
46+
->addStatusFilter(Review::STATUS_APPROVED)
47+
->addEntityFilter(Review::ENTITY_PRODUCT_CODE, $productId)
48+
->setPageSize($pageSize)
49+
->setCurPage($currentPage)
50+
->setDateOrder();
51+
$reviewsCollection->getSelect()->join(
52+
['cpe' => $reviewsCollection->getTable('catalog_product_entity')],
53+
'cpe.entity_id = main_table.entity_pk_value',
54+
['sku']
55+
);
56+
$reviewsCollection->addRateVotes();
57+
58+
return $reviewsCollection;
59+
}
60+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\ReviewGraphQl\Model\DataProvider;
9+
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+
13+
/**
14+
* Provides rating votes
15+
*/
16+
class ReviewRatingsDataProvider
17+
{
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+
31+
/**
32+
* Providing rating votes
33+
*
34+
* @param int $reviewId
35+
*
36+
* @return array
37+
*/
38+
public function getData(int $reviewId): array
39+
{
40+
/** @var VoteCollection $ratingVotes */
41+
$ratingVotes = $this->voteCollectionFactory->create();
42+
$ratingVotes->setReviewFilter($reviewId);
43+
$ratingVotes->addRatingInfo();
44+
45+
$data = [];
46+
47+
foreach ($ratingVotes->getItems() as $ratingVote) {
48+
$data[] = [
49+
'name' => $ratingVote->getData('rating_code'),
50+
'value' => $ratingVote->getData('value')
51+
];
52+
}
53+
54+
return $data;
55+
}
56+
}

0 commit comments

Comments
 (0)