| Current Path : /var/www/www.indacotrentino.com/www/vendor/magento/module-review/Controller/Product/ |
| Current File : /var/www/www.indacotrentino.com/www/vendor/magento/module-review/Controller/Product/Post.php |
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Review\Controller\Product;
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Review\Controller\Product as ProductController;
use Magento\Framework\Controller\ResultFactory;
use Magento\Review\Model\Review;
/**
* Class Post
*/
class Post extends ProductController implements HttpPostActionInterface
{
/**
* Submit new review action
*
* @return \Magento\Framework\Controller\Result\Redirect
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
if (!$this->formKeyValidator->validate($this->getRequest())) {
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
return $resultRedirect;
}
$data = $this->reviewSession->getFormData(true);
if ($data) {
$rating = [];
if (isset($data['ratings']) && is_array($data['ratings'])) {
$rating = $data['ratings'];
}
} else {
$data = $this->getRequest()->getPostValue();
$rating = $this->getRequest()->getParam('ratings', []);
}
if (($product = $this->initProduct()) && !empty($data)) {
/** @var \Magento\Review\Model\Review $review */
$review = $this->reviewFactory->create()->setData($data);
$review->unsetData('review_id');
$validate = $review->validate();
if ($validate === true) {
try {
$review->setEntityId($review->getEntityIdByCode(Review::ENTITY_PRODUCT_CODE))
->setEntityPkValue($product->getId())
->setStatusId(Review::STATUS_PENDING)
->setCustomerId($this->customerSession->getCustomerId())
->setStoreId($this->storeManager->getStore()->getId())
->setStores([$this->storeManager->getStore()->getId()])
->save();
foreach ($rating as $ratingId => $optionId) {
$this->ratingFactory->create()
->setRatingId($ratingId)
->setReviewId($review->getId())
->setCustomerId($this->customerSession->getCustomerId())
->addOptionVote($optionId, $product->getId());
}
$review->aggregate();
$this->messageManager->addSuccessMessage(__('You submitted your review for moderation.'));
} catch (\Exception $e) {
$this->reviewSession->setFormData($data);
$this->messageManager->addErrorMessage(__('We can\'t post your review right now.'));
}
} else {
$this->reviewSession->setFormData($data);
if (is_array($validate)) {
foreach ($validate as $errorMessage) {
$this->messageManager->addErrorMessage($errorMessage);
}
} else {
$this->messageManager->addErrorMessage(__('We can\'t post your review right now.'));
}
}
}
$redirectUrl = $this->reviewSession->getRedirectUrl(true);
$resultRedirect->setUrl($redirectUrl ?: $this->_redirect->getRedirectUrl());
return $resultRedirect;
}
}