-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
28 changed files
with
1,981 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Review\Model\Review; | ||
|
||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Store\Model\ScopeInterface; | ||
|
||
/** | ||
* Provides reviews configuration | ||
*/ | ||
class Config | ||
{ | ||
const XML_PATH_REVIEW_ACTIVE = 'catalog/review/active'; | ||
|
||
/** | ||
* @var ScopeConfigInterface | ||
*/ | ||
private $scopeConfig; | ||
|
||
/** | ||
* @param ScopeConfigInterface $scopeConfig | ||
*/ | ||
public function __construct( | ||
ScopeConfigInterface $scopeConfig | ||
) { | ||
$this->scopeConfig = $scopeConfig; | ||
} | ||
|
||
/** | ||
* Check whether the reviews are enabled or not | ||
* | ||
* @return bool | ||
*/ | ||
public function isEnabled(): bool | ||
{ | ||
return $this->scopeConfig->isSetFlag( | ||
self::XML_PATH_REVIEW_ACTIVE, | ||
ScopeInterface::SCOPE_STORES | ||
); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
app/code/Magento/ReviewGraphQl/Mapper/ReviewDataMapper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\ReviewGraphQl\Mapper; | ||
|
||
use Magento\Catalog\Model\Product; | ||
use Magento\Review\Model\Review; | ||
|
||
/** | ||
* Converts the review data from review object to an associative array | ||
*/ | ||
class ReviewDataMapper | ||
{ | ||
/** | ||
* Mapping the review data | ||
* | ||
* @param Review $review | ||
* | ||
* @return array | ||
*/ | ||
public function map(Review $review): array | ||
{ | ||
return [ | ||
'summary' => $review->getData('title'), | ||
'text' => $review->getData('detail'), | ||
'nickname' => $review->getData('nickname'), | ||
'created_at' => $review->getData('created_at'), | ||
'sku' => $review->getSku(), | ||
'model' => $review | ||
]; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
app/code/Magento/ReviewGraphQl/Model/DataProvider/AggregatedReviewsDataProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\ReviewGraphQl\Model\DataProvider; | ||
|
||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Review\Model\ResourceModel\Review\Collection as ReviewCollection; | ||
use Magento\Review\Model\ResourceModel\Review\Product\Collection as ProductCollection; | ||
use Magento\ReviewGraphQl\Mapper\ReviewDataMapper; | ||
|
||
/** | ||
* Provides aggregated reviews result | ||
* | ||
* The following class prepares the GraphQl endpoints' result for Customer and Product reviews | ||
*/ | ||
class AggregatedReviewsDataProvider | ||
{ | ||
/** | ||
* @var ReviewDataMapper | ||
*/ | ||
private $reviewDataMapper; | ||
|
||
/** | ||
* @param ReviewDataMapper $reviewDataMapper | ||
*/ | ||
public function __construct(ReviewDataMapper $reviewDataMapper) | ||
{ | ||
$this->reviewDataMapper = $reviewDataMapper; | ||
} | ||
|
||
/** | ||
* Get reviews result | ||
* | ||
* @param ProductCollection|ReviewCollection $reviewsCollection | ||
* | ||
* @return array | ||
*/ | ||
public function getData($reviewsCollection): array | ||
{ | ||
if ($reviewsCollection->getPageSize()) { | ||
$maxPages = ceil($reviewsCollection->getSize() / $reviewsCollection->getPageSize()); | ||
} else { | ||
$maxPages = 0; | ||
} | ||
|
||
$currentPage = $reviewsCollection->getCurPage(); | ||
if ($reviewsCollection->getCurPage() > $maxPages && $reviewsCollection->getSize() > 0) { | ||
$currentPage = new GraphQlInputException( | ||
__( | ||
'currentPage value %1 specified is greater than the number of pages available.', | ||
[$maxPages] | ||
) | ||
); | ||
} | ||
|
||
$items = []; | ||
foreach ($reviewsCollection->getItems() as $item) { | ||
$items[] = $this->reviewDataMapper->map($item); | ||
} | ||
|
||
return [ | ||
'total_count' => $reviewsCollection->getSize(), | ||
'items' => $items, | ||
'page_info' => [ | ||
'page_size' => $reviewsCollection->getPageSize(), | ||
'current_page' => $currentPage, | ||
'total_pages' => $maxPages | ||
] | ||
]; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
app/code/Magento/ReviewGraphQl/Model/DataProvider/CustomerReviewsDataProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\ReviewGraphQl\Model\DataProvider; | ||
|
||
use Magento\Review\Model\ResourceModel\Review\Collection as ReviewsCollection; | ||
use Magento\Review\Model\ResourceModel\Review\CollectionFactory as ReviewsCollectionFactory; | ||
use Magento\Review\Model\Review; | ||
|
||
/** | ||
* Provides customer reviews | ||
*/ | ||
class CustomerReviewsDataProvider | ||
{ | ||
/** | ||
* @var ReviewsCollectionFactory | ||
*/ | ||
private $collectionFactory; | ||
|
||
/** | ||
* @param ReviewsCollectionFactory $collectionFactory | ||
*/ | ||
public function __construct( | ||
ReviewsCollectionFactory $collectionFactory | ||
) { | ||
$this->collectionFactory = $collectionFactory; | ||
} | ||
|
||
/** | ||
* Get customer reviews | ||
* | ||
* @param int $customerId | ||
* @param int $currentPage | ||
* @param int $pageSize | ||
* | ||
* @return ReviewsCollection | ||
*/ | ||
public function getData(int $customerId, int $currentPage, int $pageSize): ReviewsCollection | ||
{ | ||
/** @var ReviewsCollection $reviewsCollection */ | ||
$reviewsCollection = $this->collectionFactory->create(); | ||
$reviewsCollection | ||
->addCustomerFilter($customerId) | ||
->setPageSize($pageSize) | ||
->setCurPage($currentPage) | ||
->setDateOrder(); | ||
$reviewsCollection->getSelect()->join( | ||
['cpe' => $reviewsCollection->getTable('catalog_product_entity')], | ||
'cpe.entity_id = main_table.entity_pk_value', | ||
['sku'] | ||
); | ||
$reviewsCollection->addRateVotes(); | ||
|
||
return $reviewsCollection; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
app/code/Magento/ReviewGraphQl/Model/DataProvider/ProductReviewsDataProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\ReviewGraphQl\Model\DataProvider; | ||
|
||
use Magento\Review\Model\ResourceModel\Review\Collection; | ||
use Magento\Review\Model\ResourceModel\Review\CollectionFactory; | ||
use Magento\Review\Model\Review; | ||
|
||
/** | ||
* Provides product reviews | ||
*/ | ||
class ProductReviewsDataProvider | ||
{ | ||
/** | ||
* @var CollectionFactory | ||
*/ | ||
private $collectionFactory; | ||
|
||
/** | ||
* @param CollectionFactory $collectionFactory | ||
*/ | ||
public function __construct( | ||
CollectionFactory $collectionFactory | ||
) { | ||
$this->collectionFactory = $collectionFactory; | ||
} | ||
|
||
/** | ||
* Get product reviews | ||
* | ||
* @param int $productId | ||
* @param int $currentPage | ||
* @param int $pageSize | ||
* | ||
* @return Collection | ||
*/ | ||
public function getData(int $productId, int $currentPage, int $pageSize): Collection | ||
{ | ||
/** @var Collection $reviewsCollection */ | ||
$reviewsCollection = $this->collectionFactory->create() | ||
->addStatusFilter(Review::STATUS_APPROVED) | ||
->addEntityFilter(Review::ENTITY_PRODUCT_CODE, $productId) | ||
->setPageSize($pageSize) | ||
->setCurPage($currentPage) | ||
->setDateOrder(); | ||
$reviewsCollection->getSelect()->join( | ||
['cpe' => $reviewsCollection->getTable('catalog_product_entity')], | ||
'cpe.entity_id = main_table.entity_pk_value', | ||
['sku'] | ||
); | ||
$reviewsCollection->addRateVotes(); | ||
|
||
return $reviewsCollection; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
app/code/Magento/ReviewGraphQl/Model/DataProvider/ReviewRatingsDataProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\ReviewGraphQl\Model\DataProvider; | ||
|
||
use Magento\Review\Model\ResourceModel\Rating\Option\Vote\Collection as VoteCollection; | ||
use Magento\Review\Model\ResourceModel\Rating\Option\Vote\CollectionFactory as VoteCollectionFactory; | ||
|
||
/** | ||
* Provides rating votes | ||
*/ | ||
class ReviewRatingsDataProvider | ||
{ | ||
/** | ||
* @var VoteCollectionFactory | ||
*/ | ||
private $voteCollectionFactory; | ||
|
||
/** | ||
* @param VoteCollectionFactory $voteCollectionFactory | ||
*/ | ||
public function __construct(VoteCollectionFactory $voteCollectionFactory) | ||
{ | ||
$this->voteCollectionFactory = $voteCollectionFactory; | ||
} | ||
|
||
/** | ||
* Providing rating votes | ||
* | ||
* @param int $reviewId | ||
* | ||
* @return array | ||
*/ | ||
public function getData(int $reviewId): array | ||
{ | ||
/** @var VoteCollection $ratingVotes */ | ||
$ratingVotes = $this->voteCollectionFactory->create(); | ||
$ratingVotes->setReviewFilter($reviewId); | ||
$ratingVotes->addRatingInfo(); | ||
|
||
$data = []; | ||
|
||
foreach ($ratingVotes->getItems() as $ratingVote) { | ||
$data[] = [ | ||
'name' => $ratingVote->getData('rating_code'), | ||
'value' => $ratingVote->getData('value') | ||
]; | ||
} | ||
|
||
return $data; | ||
} | ||
} |
Oops, something went wrong.