Created
October 11, 2017 13:30
-
-
Save chornobils/eb23501c9c41cef783461334bd09d5fc to your computer and use it in GitHub Desktop.
[Sylius] autoaccepting new reviews; disable accept state after creating a new review
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
<?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 <[email protected]> | |
* @author Grzegorz Sadowski <[email protected]> | |
*/ | |
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(); | |
} | |
} |
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
# 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 |
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
# 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