<?php
namespace App\Controller\Frontend;
use App\Controller\Controller;
use App\Entity\Review;
use App\Event\EntityEvent;
use App\Form\Type\Frontend\CustomerReviewType;
use App\Repository\ReviewRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\Translation\TranslatorInterface;
class ReviewController extends Controller
{
public function indexAction(Request $request,
ReviewRepository $reviewRepository,
FormFactoryInterface $formFactory,
EventDispatcherInterface $eventDispatcher,
TranslatorInterface $translator,
EntityManagerInterface $em)
{
$list = $reviewRepository->findSomeByLatestPublished(30);
$average = $reviewRepository->findAverageNote();
$total = $reviewRepository->countAll();
if (null !== $user = $this->getUser()) {
if (null === $review = $reviewRepository->findOneByUser($user)) {
$review = new Review();
$review->setUser($user);
}
$form = $formFactory->create(CustomerReviewType::class, $review);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$review->setPublished(false);
$em->persist($review);
$em->flush();
$request->getSession()->getFlashBag()->add('success', $translator->trans('frontend.review.submit'));
$eventDispatcher->dispatch(new EntityEvent($review), EntityEvent::NEW_REVIEW);
return $this->redirect($this->generateUrl('frontend_client_opinion'));
}
return $this->render('/Frontend/review/index.html.twig', [
'form' => $form->createView(),
'list' => $list,
'average' => $average,
'total' => $total,
]);
}
return $this->render('/Frontend/review/index.html.twig', [
'list' => $list,
'average' => $average,
'total' => $total,
]);
}
}