src/Controller/Frontend/ReviewController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Controller\Controller;
  4. use App\Entity\Review;
  5. use App\Event\EntityEvent;
  6. use App\Form\Type\Frontend\CustomerReviewType;
  7. use App\Repository\ReviewRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  10. use Symfony\Component\Form\FormFactoryInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. class ReviewController extends Controller
  14. {
  15.     public function indexAction(Request $request,
  16.         ReviewRepository $reviewRepository,
  17.         FormFactoryInterface $formFactory,
  18.         EventDispatcherInterface $eventDispatcher,
  19.         TranslatorInterface $translator,
  20.         EntityManagerInterface $em)
  21.     {
  22.         $list $reviewRepository->findSomeByLatestPublished(30);
  23.         $average $reviewRepository->findAverageNote();
  24.         $total $reviewRepository->countAll();
  25.         if (null !== $user $this->getUser()) {
  26.             if (null === $review $reviewRepository->findOneByUser($user)) {
  27.                 $review = new Review();
  28.                 $review->setUser($user);
  29.             }
  30.             $form $formFactory->create(CustomerReviewType::class, $review);
  31.             $form->handleRequest($request);
  32.             if ($form->isSubmitted() && $form->isValid()) {
  33.                 $review->setPublished(false);
  34.                 $em->persist($review);
  35.                 $em->flush();
  36.                 $request->getSession()->getFlashBag()->add('success'$translator->trans('frontend.review.submit'));
  37.                 $eventDispatcher->dispatch(new EntityEvent($review), EntityEvent::NEW_REVIEW);
  38.                 return $this->redirect($this->generateUrl('frontend_client_opinion'));
  39.             }
  40.             return $this->render('/Frontend/review/index.html.twig', [
  41.                 'form' => $form->createView(),
  42.                 'list' => $list,
  43.                 'average' => $average,
  44.                 'total' => $total,
  45.             ]);
  46.         }
  47.         return $this->render('/Frontend/review/index.html.twig', [
  48.             'list' => $list,
  49.             'average' => $average,
  50.             'total' => $total,
  51.         ]);
  52.     }
  53. }