src/Aqarmap/Bundle/MainBundle/Controller/FeedbackController.php line 87

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\MainBundle\Controller;
  3. use Aqarmap\Bundle\MainBundle\Constant\FeedbackSource;
  4. use Aqarmap\Bundle\MainBundle\Entity\Feedback;
  5. use Aqarmap\Bundle\MainBundle\Event\FeedbackEvent;
  6. use Aqarmap\Bundle\MainBundle\Form\FeedbackType;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Egulias\EmailValidator\EmailValidator;
  9. use Egulias\EmailValidator\Validation\RFCValidation;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. class FeedbackController extends AbstractController
  18. {
  19.     private TranslatorInterface $translator;
  20.     private SessionInterface $session;
  21.     private EventDispatcherInterface $eventDispatcher;
  22.     public function __construct(TranslatorInterface $translatorSessionInterface $sessionEventDispatcherInterface $eventDispatcher)
  23.     {
  24.         $this->translator $translator;
  25.         $this->session $session;
  26.         $this->eventDispatcher $eventDispatcher;
  27.     }
  28.     /**
  29.      * @Route("/feedback", name="aqarmap_feedback_post", methods={"POST"})
  30.      */
  31.     public function post(Request $requestEntityManagerInterface $em)
  32.     {
  33.         $form $this->createForm(FeedbackType::class, $entity = new Feedback(), [
  34.             'action' => $this->generateUrl('aqarmap_feedback_post'),
  35.             'method' => 'POST',
  36.         ]);
  37.         $form->handleRequest($request);
  38.         if ($form->isSubmitted() && $form->isValid()) {
  39.             $validator = new EmailValidator();
  40.             $isValidEmail $validator->isValid($entity->getEmail(), new RFCValidation());
  41.             if (!$isValidEmail) {
  42.                 $this->addFlash(
  43.                     'danger',
  44.                     $this->translator->trans('static.invalid_email')
  45.                 );
  46.                 return $this->redirectToRoute('aqarmap_feedback_get');
  47.             }
  48.             // Set collected user data
  49.             $entity->setCollectedData(array_merge([
  50.                 'referer' => $form->get('referer')->getData() ?: $request->headers->get('referer'),
  51.             ], $request->attributes->get('_browser')));
  52.             $entity->setIp($request->getClientIp());
  53.             $entity->setSource(FeedbackSource::WEB);
  54.             $em->persist($entity);
  55.             $this->eventDispatcher->dispatch(new FeedbackEvent($entity), 'aqarmap.feedback.send_message');
  56.             $userMail $form->get('email')->getData();
  57.         } else {
  58.             // Set error flash message
  59.             $this->session->getFlashBag()->add(
  60.                 'danger',
  61.                 $this->translator->trans('static.problem_error_message')
  62.             );
  63.             return $this->redirect($request->headers->get('referer') ?: $this->generateUrl('homepage'));
  64.         }
  65.         return $this->render('@AqarmapMain/Feedback/post.html.twig', [
  66.             'userMail' => $userMail,
  67.         ]);
  68.     }
  69.     /**
  70.      * @Route("/feedback", name="aqarmap_feedback_get", methods={"GET"})
  71.      */
  72.     public function feedback(Request $request): Response
  73.     {
  74.         $form $this->createForm(FeedbackType::class, new Feedback(), [
  75.             'action' => $this->generateUrl('aqarmap_feedback_post'),
  76.             'method' => 'POST',
  77.         ]);
  78.         // Set the referrer URL
  79.         if (!$form->get('referer')->getData()) {
  80.             $form->get('referer')->setData($request->headers->get('referer'));
  81.         }
  82.         return $this->render('@AqarmapMain/Feedback/feedbackForm.html.twig', [
  83.             'form' => $form->createView(),
  84.         ]);
  85.     }
  86. }