-
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.
API: Product Reviews #55: product reviews web services
- Loading branch information
Showing
70 changed files
with
6,049 additions
and
313 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
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,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()); | ||
} | ||
} |
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,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); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...iew/Model/Api/SearchCriteria/CollectionProcessor/FilterProcessor/ReviewCustomerFilter.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,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; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...o/Review/Model/Api/SearchCriteria/CollectionProcessor/FilterProcessor/ReviewSkuFilter.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,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
150
app/code/Magento/Review/Model/Command/CreateReviews.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,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; | ||
} | ||
} |
Oops, something went wrong.