Skip to content

Instantly share code, notes, and snippets.

@cornobils
Created October 11, 2017 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cornobils/eb23501c9c41cef783461334bd09d5fc to your computer and use it in GitHub Desktop.
Save cornobils/eb23501c9c41cef783461334bd09d5fc to your computer and use it in GitHub Desktop.
[Sylius] autoaccepting new reviews; disable accept state after creating a new review
<?php
namespace AppBundle\Resolver;
use Doctrine\Common\Persistence\ObjectManager;
use Sylius\Bundle\ReviewBundle\Updater\ReviewableRatingUpdaterInterface;
use Sylius\Component\Review\Calculator\ReviewableRatingCalculatorInterface;
use Sylius\Component\Review\Model\ReviewableInterface;
use Sylius\Component\Review\Model\ReviewInterface;
/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
* @author Grzegorz Sadowski <grzegorz.sadowski@lakion.com>
*/
final class CustomAverageRatingUpdater implements ReviewableRatingUpdaterInterface {
/**
* @var ReviewableRatingCalculatorInterface
*/
private $averageRatingCalculator;
/**
* @var ObjectManager
*/
private $reviewSubjectManager;
/**
* @param ReviewableRatingCalculatorInterface $averageRatingCalculator
* @param ObjectManager $reviewSubjectManager
*/
public function __construct(
ReviewableRatingCalculatorInterface $averageRatingCalculator,
ObjectManager $reviewSubjectManager
) {
$this->averageRatingCalculator = $averageRatingCalculator;
$this->reviewSubjectManager = $reviewSubjectManager;
}
public function update(ReviewableInterface $reviewSubject) {
$this->modifyReviewSubjectAverageRating($reviewSubject);
}
public function updateFromReview(ReviewInterface $review) {
$this->modifyReviewSubjectAverageRating($review->getReviewSubject());
}
/**
* @param ReviewableInterface $reviewSubject
*/
private function modifyReviewSubjectAverageRating(ReviewableInterface $reviewSubject)
{
$averageRating = $this->averageRatingCalculator->calculate($reviewSubject);
$reviewSubject->setAverageRating($averageRating);
$this->reviewSubjectManager->persist($reviewSubject);
$this->reviewSubjectManager->flush();
}
}
# app\Resources\SyliusShopBundle\config\routing\product_review.yml
sylius_shop_product_review_create:
path: /new
methods: [GET, POST]
defaults:
_controller: sylius.controller.product_review:createAction
_sylius:
template: "@SyliusShop/ProductReview/create.html.twig"
form:
options:
validation_groups: ['ignore-default', 'sylius_review']
factory:
method: createForSubjectWithReviewer
arguments:
- "expr:notFoundOnNull(service('sylius.repository.product').findOneByChannelAndSlug(service('sylius.context.channel').getChannel(), service('sylius.context.locale').getLocaleCode(), $slug))"
- "expr:service('sylius.context.customer').getCustomer()"
redirect:
route: sylius_shop_product_show
parameters:
slug: $slug
flash: sylius.review.wait_for_the_acceptation
state_machine:
graph: sylius_product_review
transition: accept
# app/config/services.yml
sylius.product_review.average_rating_updater:
class: AppBundle\Resolver\CustomAverageRatingUpdater
arguments:
- "@sylius.average_rating_calculator"
- "@sylius.manager.product_review"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment