Skip to content

Commit

Permalink
API: Product Reviews #55: product reviews web services
Browse files Browse the repository at this point in the history
  • Loading branch information
sky-hub committed Apr 24, 2019
1 parent 45a6365 commit 0e0946d
Show file tree
Hide file tree
Showing 70 changed files with 6,049 additions and 313 deletions.
10 changes: 7 additions & 3 deletions app/code/Magento/Review/Controller/Adminhtml/Product/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Review\Controller\Adminhtml\Product;

use Magento\Review\Controller\Adminhtml\Product as ProductController;
use Magento\Framework\Controller\ResultFactory;
use Magento\Review\Controller\Adminhtml\Product as ProductController;

class Delete extends ProductController
{
Expand All @@ -17,9 +18,12 @@ public function execute()
{
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$reviewId = $this->getRequest()->getParam('id', false);
$reviewId = (int)$this->getRequest()->getParam('id', false);
try {
$this->reviewFactory->create()->setId($reviewId)->aggregate()->delete();
$review = $this->reviewFactory->create(
['data' => ['review_id' => $reviewId]]
);
$review->delete();

$this->messageManager->addSuccess(__('The review has been deleted.'));
if ($this->getRequest()->getParam('ret') == 'pending') {
Expand Down
42 changes: 42 additions & 0 deletions app/code/Magento/Review/Model/Aggregator/Ratings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Review\Model\Aggregator;

use Magento\Review\Model\ResourceModel\Review as ReviewResource;
use Magento\ReviewApi\Api\Data\ReviewInterface;
use Magento\ReviewApi\Model\AggregatorInterface;

/**
* Class Ratings
*/
class Ratings implements AggregatorInterface
{
/**
* @var ReviewResource
*/
private $reviewResource;

/**
* Aggregator constructor
*
* @param ReviewResource $reviewResource
*/
public function __construct(
ReviewResource $reviewResource
) {
$this->reviewResource = $reviewResource;
}

/**
* @inheritdoc
*/
public function aggregate(ReviewInterface $review): void
{
$this->reviewResource->reAggregateReview($review->getReviewId(), $review->getRelatedEntityId());
}
}
42 changes: 42 additions & 0 deletions app/code/Magento/Review/Model/Aggregator/Review.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Review\Model\Aggregator;

use Magento\Review\Model\ResourceModel\Review as ReviewResource;
use Magento\ReviewApi\Api\Data\ReviewInterface;
use Magento\ReviewApi\Model\AggregatorInterface;

/**
* Class Review
*/
class Review implements AggregatorInterface
{
/**
* @var ReviewResource
*/
private $reviewResource;

/**
* Aggregator constructor
*
* @param ReviewResource $reviewResource
*/
public function __construct(
ReviewResource $reviewResource
) {
$this->reviewResource = $reviewResource;
}

/**
* @inheritdoc
*/
public function aggregate(ReviewInterface $review): void
{
$this->reviewResource->aggregate($review);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Review\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor;

use Magento\Framework\Api\Filter;
use Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface;
use Magento\Framework\Data\Collection\AbstractDb;

/**
* Class ReviewCustomerFilter
*/
class ReviewCustomerFilter implements CustomFilterInterface
{
/**
* Apply customer_id Filter to Review Collection
*
* @param Filter $filter
* @param AbstractDb $collection
* @return bool Whether the filter is applied
*/
public function apply(Filter $filter, AbstractDb $collection)
{
/** @var \Magento\Review\Model\ResourceModel\Review\Collection $collection */
$collection->addCustomerFilter($filter->getValue());

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Review\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor;

use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
use Magento\Framework\Api\Filter;
use Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface;
use Magento\Framework\Data\Collection\AbstractDb;

/**
* Class ReviewSkuFilter
*/
class ReviewSkuFilter implements CustomFilterInterface
{
/**
* Product resource
*
* @var ProductResource
*/
private $productResource;

/**
* ReviewSkuFilter constructor
*
* @param ProductResource $productResource
*/
public function __construct(
ProductResource $productResource
) {
$this->productResource = $productResource;
}

/**
* Apply sku Filter to Review Collection
*
* @param Filter $filter
* @param AbstractDb $collection
* @return bool Whether the filter is applied
*/
public function apply(Filter $filter, AbstractDb $collection)
{
$productId = $this->productResource->getIdBySku($filter->getValue());

/** @var \Magento\Review\Model\ResourceModel\Review\Collection $collection */
$collection->addEntityFilter('product', $productId);

return true;
}
}
150 changes: 150 additions & 0 deletions app/code/Magento/Review/Model/Command/CreateReviews.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Review\Model\Command;

use Magento\Framework\Exception\InputException;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Review\Model\ResourceModel\Review\CreateMultiple;
use Magento\ReviewApi\Api\CreateReviewsInterface;
use Magento\ReviewApi\Api\Data\ReviewInterface;
use Magento\ReviewApi\Api\ReviewOperationResponseInterface;
use Magento\ReviewApi\Model\AggregatorInterface;
use Magento\ReviewApi\Model\ReviewValidatorChain;
use Psr\Log\LoggerInterface;

/**
* Class CreateReviews
*/
class CreateReviews implements CreateReviewsInterface
{
/**
* @var CreateMultiple
*/
private $createMultiple;

/**
* @var ReviewValidatorChain
*/
private $reviewValidatorChain;

/**
* @var AggregatorInterface
*/
private $reviewAggregator;

/**
* @var ReviewOperationResponseInterface
*/
private $reviewOperationResponse;

/**
* @var SerializerInterface
*/
private $jsonSerializer;

/**
* @var LoggerInterface
*/
private $logger;

/**
* CreateReviews constructor
*
* @param CreateMultiple $createMultiple
* @param ReviewValidatorChain $reviewValidatorChain
* @param AggregatorInterface $reviewAggregator
* @param ReviewOperationResponseInterface $reviewOperationResponse
* @param SerializerInterface $jsonSerializer
* @param LoggerInterface $logger
*/
public function __construct(
CreateMultiple $createMultiple,
ReviewValidatorChain $reviewValidatorChain,
AggregatorInterface $reviewAggregator,
ReviewOperationResponseInterface $reviewOperationResponse,
SerializerInterface $jsonSerializer,
LoggerInterface $logger
) {
$this->createMultiple = $createMultiple;
$this->reviewValidatorChain = $reviewValidatorChain;
$this->reviewAggregator = $reviewAggregator;
$this->reviewOperationResponse = $reviewOperationResponse;
$this->jsonSerializer = $jsonSerializer;
$this->logger = $logger;
}

/**
* @inheritdoc
*/
public function execute(array $reviews): ReviewOperationResponseInterface
{
if (empty($reviews)) {
throw new InputException(__('Input data is empty'));
}

$validReviews = [];
$invalidReviews = [];

foreach ($reviews as $key => $review) {
$validationResult = $this->reviewValidatorChain->validate($review);
if (!$validationResult->isValid()) {
$invalidReviews[] = $review;

$this->reviewOperationResponse->addError(
$this->getErrorMessage($validationResult->getErrors(), $review)
);
continue;
}
$validReviews[] = $review;
}

if (!empty($validReviews)) {
try {
$this->createMultiple->execute($validReviews);

foreach ($validReviews as $review) {
$this->reviewAggregator->aggregate($review);
}

$this->reviewOperationResponse->addSuccessfulReviews($validReviews);
} catch (\Exception $e) {
$this->reviewOperationResponse->addFailedReviews($validReviews);
$this->reviewOperationResponse->addError(
__('There was an error while saving the reviews.')
);
$this->logger->error($e->getMessage());
}
}
if (!empty($invalidReviews)) {
$this->reviewOperationResponse->addFailedReviews($invalidReviews);
}

unset($validReviews);
unset($invalidReviews);

return $this->reviewOperationResponse;
}

/**
* Get error message
*
* @param array $errors
* @param ReviewInterface $review
* @return string
*/
private function getErrorMessage(array $errors, ReviewInterface $review)
{
$errorMessage = sprintf(
'%s => Request Data: %s:',
implode(',', $errors),
$this->jsonSerializer->serialize($review->toArray())
);

return $errorMessage;
}
}
Loading

0 comments on commit 0e0946d

Please sign in to comment.