src/Controller/Newsletter/NewsletterController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Newsletter;
  3. use App\Services\NewsletterApiService;
  4. use Exception;
  5. use App\Form\NewsletterFormType;
  6. use App\Services\RecaptchaService;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Pimcore\Controller\FrontendController;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. class NewsletterController extends FrontendController
  15. {
  16.     /**
  17.      * @Template
  18.      * @Route("{lang}/newsletter-form/", name="newsletter-form")
  19.      * @param Request $request
  20.      * @param RecaptchaService $recaptchaService
  21.      * @param TranslatorInterface $translator
  22.      * @param NewsletterApiService $newsletterApi
  23.      * @param string|null $lang
  24.      * @return Response
  25.      */
  26.     public function subscribeAction(
  27.         Request              $request,
  28.         RecaptchaService     $recaptchaService,
  29.         TranslatorInterface  $translator,
  30.         NewsletterApiService $newsletterApi,
  31.         string               $lang null
  32.     ): Response
  33.     {
  34.         $lang $lang ?: $request->getLocale();
  35.         $form $this->createForm(NewsletterFormType::class, [], [
  36.             'action' => $this->generateUrl('newsletter-form', ['lang' => $lang])
  37.         ]);
  38.         $form->handleRequest($request);
  39.         if ($form->isSubmitted() && $form->isValid()) {
  40.             $data $form->getData();
  41.             $email $data['email'];
  42.             $recaptcha $recaptchaService->checkRecaptcha(
  43.                 $request->get('newsletter-g-recaptcha-response'),
  44.                 $request->getClientIp()
  45.             );
  46.             if ($recaptcha === RecaptchaService::INCORRECT_CAPTCHA) {
  47.                 $error $translator->trans('meddox.recaptcha.error');
  48.                 return new JsonResponse(['error' => $error], 422);
  49.             }
  50.             return $newsletterApi->relayRequestToApi($emailactionNewsletterApiService::SUBSCRIBE_ACTION);
  51.         }
  52.         return $this->render('/segments/newsletter.html.twig', [
  53.             'form' => $form->createView(),
  54.             'fixedVersion' => $request->get('fixedVersion')
  55.         ]);
  56.     }
  57.     /**
  58.      * @Template()
  59.      * @param Request $request
  60.      * @param NewsletterApiService $newsletterApi
  61.      * @return Response
  62.      * @throws Exception
  63.      */
  64.     public function unsubscribeAction(
  65.         Request              $request,
  66.         NewsletterApiService $newsletterApi,
  67.     ): Response
  68.     {
  69.         $token $request->get('token');
  70.         if ($token) {
  71.             $newsletterApi->relayRequestToApi($tokenactionNewsletterApiService::UNSUBSCRIBE_ACTION);
  72.         }
  73.         return $this->render($this->document->getTemplate());
  74.     }
  75. }