src/Controller/ContactUs/ContactUsController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Controller\ContactUs;
  3. use App\DataMapper\ContactUs\ContactUsDataMapper;
  4. use App\Form\ContactUsFormType;
  5. use App\Repository\ContactUsRepository;
  6. use App\Services\RecaptchaService;
  7. use Exception;
  8. use Pimcore\Controller\FrontendController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. class ContactUsController extends FrontendController
  16. {
  17.     /**
  18.      * @Template
  19.      * @Route("{lang}/contact-us-form/", name="contact-us")
  20.      *
  21.      * @param Request $request
  22.      * @param ContactUsRepository $contactUsRepository
  23.      * @param RecaptchaService $recaptchaService
  24.      * @param TranslatorInterface $translator
  25.      * @param string|null $lang
  26.      * @return Response
  27.      * @throws Exception
  28.      */
  29.     public function postContactUsAction (
  30.         Request $request,
  31.         ContactUsRepository $contactUsRepository,
  32.         RecaptchaService $recaptchaService,
  33.         TranslatorInterface $translator,
  34.         string $lang null
  35.     ) : Response {
  36.         $lang $lang ?: $request->getLocale();
  37.         $form $this->createForm(ContactUsFormType::class, [], [
  38.             'action' => $this->generateUrl('contact-us', ['lang' => $lang])
  39.         ]);
  40.         $form->handleRequest($request);
  41.         if ($form->isSubmitted() && $form->isValid()) {
  42.             $data $form->getData();
  43.             $recaptcha $recaptchaService->checkRecaptcha(
  44.                 $request->get('contact-us-g-recaptcha-response'),
  45.                 $request->getClientIp()
  46.             );
  47.             if ($recaptcha === RecaptchaService::INCORRECT_CAPTCHA) {
  48.                 $error $translator->trans('meddox.recaptcha.error');
  49.                 return new JsonResponse(['error' => $error], 422);
  50.             }
  51.            $contactUsObject $contactUsRepository->createContactUsObject($data);
  52.            $contactUsMapped = (new ContactUsDataMapper($contactUsObject))->toArray($request);
  53.            return new JsonResponse(['contactUsObject' => $contactUsMapped], 200);
  54.         }
  55.         return $this->render("/segments/contact-us.html.twig", [
  56.             'form' => $form->createView()
  57.         ]);
  58.     }
  59. }