Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Product Reviews GraphQl support #27882

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9365b57
Adding Product and Customer Reviews GraphQl support
eduard13 Apr 17, 2020
ddf7d53
Merge branch '2.4-develop' into feature/review-graphql-261
eduard13 May 6, 2020
51e2fbc
Small refactoring to re-implement the customer reviews
eduard13 May 9, 2020
0564f78
Merge branch '2.4-develop' into feature/review-graphql-261
eduard13 May 14, 2020
1d75c53
Small improvements
eduard13 May 14, 2020
e21d3a8
Merge branch '2.4-develop' into feature/review-graphql-261
eduard13 May 20, 2020
dd796f3
Merge branch '2.4-develop' into feature/review-graphql-261
eduard13 May 21, 2020
1b7bade
Merge branch '2.4-develop' into feature/review-graphql-261
eduard13 May 21, 2020
9b8912e
Merge branch '2.4-develop' into feature/review-graphql-261
eduard13 May 25, 2020
20dfcc6
Merge branch '2.4-develop' into feature/review-graphql-261
eduard13 May 28, 2020
5809bee
Merge branch '2.4-develop' into feature/review-graphql-261
eduard13 Jun 4, 2020
4fedd42
Merge branch '2.4-develop' into feature/review-graphql-261
eduard13 Jun 4, 2020
64f9f00
Merge branch '2.4-develop' into feature/review-graphql-261
eduard13 Jun 11, 2020
5a4b007
Merge branch '2.4-develop' into feature/review-graphql-261
eduard13 Jun 11, 2020
3592cf9
Merge branch '2.4-develop' into feature/review-graphql-261
eduard13 Jun 15, 2020
7089708
Merge branch '2.4-develop' into feature/review-graphql-261
avattam06 Jun 17, 2020
017da27
Updating schema description
eduard13 Jun 22, 2020
a020625
Adding reviews config checking
eduard13 Jun 24, 2020
0111b2d
Adding return types
eduard13 Jun 24, 2020
f168ca6
Small improvements
eduard13 Jun 25, 2020
f443741
Merge branch '2.4-develop' into feature/review-graphql-261
dthampy Jun 26, 2020
11c7fd4
Merge branch '2.4-develop' into feature/review-graphql-261
eduard13 Jul 1, 2020
c9184f6
Merge branch '2.4-develop' into feature/review-graphql-261
eduard13 Jul 6, 2020
b974b48
Adding rollback fixtures and providing small adjustments
eduard13 Jul 7, 2020
de8598c
Updating WebAPI tests
eduard13 Jul 7, 2020
9230db6
Merge branch '2.4-develop' into feature/review-graphql-261
prabhuram93 Jul 10, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions app/code/Magento/Review/Model/Review/Config.php
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 app/code/Magento/ReviewGraphQl/Mapper/ReviewDataMapper.php
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
];
}
}
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
]
];
}
}
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;
}
}
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;
}
}
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;
}
}
Loading