Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
126 changes: 126 additions & 0 deletions app/code/Magento/Review/Api/Data/ReviewInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
namespace Magento\Review\Api\Data;

interface ReviewInterface
{
/**
* Get Review Id
*
* @return integer
*/
public function getReviewId();

/**
* Set Review Id
*
* @param integer $reviewId
* @return \Magento\Review\Model\Review
*/
public function setReviewId($reviewId);

/**
* Get entity id
*
* @return integer
*/
public function getEntityId();

/**
* Set entity id
*
* @param integer $entityId
* @return \Magento\Review\Model\Review
*/
public function setEntityId($entityId);

/**
* Get Detail ID
*
* @return integer
*/
public function getDetailId();

/**
* Set Detail ID
*
* @param integer $detailId
* @return \Magento\Review\Model\Review
*/
public function setDetailId($detailId);

/**
* Get Title
*
* @return string|null
*/
public function getTitle();

/**
* Set Title
*
* @param string $title
* @return \Magento\Review\Model\Review
*/
public function setTitle($title);

/**
* Get Detail
*
* @return string
*/
public function getDetail();

/**
* Set Detail
*
* @param string $detail
* @return \Magento\Review\Model\Review
*/
public function setDetail($detail);

/**
* Get Nickname
*
* @return string
*/
public function getNickname();

/**
* Set Nickname
*
* @param string $nickname
* @return \Magento\Review\Model\Review
*/
public function setNickname($nickname);

/**
* Get Customer ID
*
* @return integer
*/
public function getCustomerId();

/**
* Set Customer ID
*
* @param integer $customerId
* @return \Magento\Review\Model\Review
*/
public function setCustomerId($customerId);

/**
* Gets the status code of the review.
*
* @return string
*/
public function getStatus();

/**
* Sets the status ID based on the status code.
*
* @param string $statusCode
* @return $this
* @throws NoSuchEntityException on invalid status set
*/
public function setStatus($statusCode);
}
23 changes: 23 additions & 0 deletions app/code/Magento/Review/Api/Data/ReviewSearchResultsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace Magento\Review\Api\Data;

use Magento\Framework\Api\SearchResultsInterface;

/**
* Interface ReviewSearchResultsInterface
*
* @api
*/
interface ReviewSearchResultsInterface extends SearchResultsInterface
{
/**
* @return \Magento\Review\Api\Data\ReviewInterface[]
*/
public function getItems();

/**
* @param \Magento\Review\Model\Review[] $items
* @return $this
*/
public function setItems(array $items);
}
43 changes: 43 additions & 0 deletions app/code/Magento/Review/Api/ReviewRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
namespace Magento\Review\Api;

use Magento\Review\Model\ResourceModel\Review\Collection;

interface ReviewRepositoryInterface
{

/**
* Gets a review by it's id.
*
* @param int $reviewId
* @return \Magento\Review\Api\Data\ReviewInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function get($reviewId);

/**
* Returns list of reviews based on the given searchCriteria.
*
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
* @return \Magento\Review\Api\Data\ReviewSearchResultsInterface
*/
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria);

/**
* Saves a review.
*
* @param \Magento\Review\Api\Data\ReviewInterface $review
* @return \Magento\Review\Api\Data\ReviewInterface
* @throws \Magento\Framework\Exception\CouldNotSaveException
*/
public function save(\Magento\Review\Api\Data\ReviewInterface $review);

/**
* Deletes a review.
*
* @param int $reviewId
* @return \Magento\Review\Api\Data\ReviewInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function delete($reviewId);
}
163 changes: 163 additions & 0 deletions app/code/Magento/Review/Model/ResourceModel/ReviewRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php
namespace Magento\Review\Model\ResourceModel;

use Magento\Framework\Api\SearchCriteria;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Review\Api\Data\ReviewSearchResultsInterface;
use Magento\Review\Api\ReviewRepositoryInterface;

class ReviewRepository implements ReviewRepositoryInterface
{

/**
* @var \Magento\Review\Model\ReviewFactory
*/
protected $reviewFactory;

/**
* @var Review\CollectionFactory
*/
protected $collectionFactory;

/**
* @var \Magento\Review\Api\Data\ReviewSearchResultsInterfaceFactory
*/
protected $searchResultsFactory;

/**
* @var \Magento\Framework\Api\SearchCriteriaInterfaceFactory
*/
protected $searchCriteriaFactory;

/**
* ReviewRepository constructor.
*
* @param \Magento\Review\Model\ResourceModel\Review\CollectionFactory $collectionFactory
* @param \Magento\Review\Api\Data\ReviewSearchResultsInterfaceFactory $searchResultsInterface
* @param \Magento\Framework\Api\SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
*/
public function __construct(
\Magento\Review\Model\ReviewFactory $reviewFactory,
\Magento\Review\Model\ResourceModel\Review\CollectionFactory $collectionFactory,
\Magento\Review\Api\Data\ReviewSearchResultsInterfaceFactory $searchResultsInterface,
\Magento\Framework\Api\SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
) {
$this->reviewFactory = $reviewFactory;
$this->collectionFactory = $collectionFactory;
$this->searchResultsFactory = $searchResultsInterface;
$this->searchCriteriaFactory = $searchCriteriaBuilderFactory;
}

/**
* {@inheritdoc}
*/
public function get($reviewId)
{
/** @var \Magento\Review\Model\Review $review */
$review = $this->reviewFactory->create()
->load($reviewId);

if (!$review->getId()) {
throw new NoSuchEntityException(__("Requested Review doesn't exist"));
}
return $review;
}

/**
* {@inheritdoc}
*/
public function save(\Magento\Review\Api\Data\ReviewInterface $review)
{
/** @var \Magento\Review\Model\Review $review */
$validationResult = $review->validate();
if (true !== $validationResult) {
throw new \Magento\Framework\Exception\CouldNotSaveException(
__('Invalid review data: %1', implode(',', $validationResult))
);
}
$review->setEntityId(1); // product

$review->save();
$review->aggregate();
return $review;
}

/**
* {@inheritdoc}
*/
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
{
$collection = $this->collectionFactory->create();
foreach ($searchCriteria->getFilterGroups() as $filterGroup) {
$this->addFilterGroupToCollection($filterGroup, $collection);
}

/** @var \Magento\Framework\Api\SortOrder $sortOrder */
foreach ((array)$searchCriteria->getSortOrders() as $sortOrder) {
$field = $sortOrder->getField();
$collection->addOrder(
$field,
($sortOrder->getDirection() == \Magento\Framework\Api\SortOrder::SORT_ASC) ? 'ASC' : 'DESC'
);
}
$collection->setCurPage($searchCriteria->getCurrentPage());
$collection->setPageSize($searchCriteria->getPageSize());
$collection->load();

$searchResult = $this->searchResultsFactory->create();
$searchResult->setSearchCriteria($searchCriteria);
$searchResult->setItems($collection->getItems());
$searchResult->setTotalCount($collection->getSize());

return $searchResult;
}

/**
* {@inheritdoc}
*/
public function delete($reviewId)
{
$review = $this->reviewFactory->create()
->load($reviewId);

if (!$review->getId()) {
throw new NoSuchEntityException(__("Requested Review doesn't exist"));
}

$review->delete();

return $review;
}

/**
* Helper function that adds a FilterGroup to the collection.
*
* @param \Magento\Framework\Api\Search\FilterGroup $filterGroup
* @param \Magento\Review\Model\ResourceModel\Review\Collection $collection
* @return void
*/
protected function addFilterGroupToCollection(
\Magento\Framework\Api\Search\FilterGroup $filterGroup,
\Magento\Review\Model\ResourceModel\Review\Collection $collection
) {
$fields = [];
$productFilter = [];
foreach ($filterGroup->getFilters() as $filter) {
$conditionType = $filter->getConditionType() ? $filter->getConditionType() : 'eq';

if ($filter->getField() == 'product_id') {
$productFilter[$conditionType][] = $filter->getValue();
continue;
}
$fields[] = ['attribute' => $filter->getField(), $conditionType => $filter->getValue()];
}

if ($productFilter) {
$collection->addEntityFilter('product', $productFilter);
}

if ($fields) {
$collection->addFieldToFilter($fields);
}
}
}
Loading