vendor/shopware/storefront/Controller/FormController.php line 62

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Content\ContactForm\SalesChannel\AbstractContactFormRoute;
  4. use Shopware\Core\Content\Newsletter\SalesChannel\AbstractNewsletterSubscribeRoute;
  5. use Shopware\Core\Content\Newsletter\SalesChannel\AbstractNewsletterUnsubscribeRoute;
  6. use Shopware\Core\Framework\RateLimiter\Exception\RateLimitExceededException;
  7. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  8. use Shopware\Core\Framework\Routing\Annotation\Since;
  9. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  10. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  12. use Shopware\Storefront\Framework\Captcha\Annotation\Captcha;
  13. use Shopware\Storefront\Framework\Routing\RequestTransformer;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. /**
  18.  * @Route(defaults={"_routeScope"={"storefront"}})
  19.  *
  20.  * @deprecated tag:v6.5.0 - reason:becomes-internal - Will be internal
  21.  */
  22. class FormController extends StorefrontController
  23. {
  24.     public const SUBSCRIBE 'subscribe';
  25.     public const UNSUBSCRIBE 'unsubscribe';
  26.     /**
  27.      * @var AbstractContactFormRoute
  28.      */
  29.     private $contactFormRoute;
  30.     /**
  31.      * @var AbstractNewsletterSubscribeRoute
  32.      */
  33.     private $subscribeRoute;
  34.     /**
  35.      * @var AbstractNewsletterUnsubscribeRoute
  36.      */
  37.     private $unsubscribeRoute;
  38.     /**
  39.      * @internal
  40.      */
  41.     public function __construct(
  42.         AbstractContactFormRoute $contactFormRoute,
  43.         AbstractNewsletterSubscribeRoute $subscribeRoute,
  44.         AbstractNewsletterUnsubscribeRoute $unsubscribeRoute
  45.     ) {
  46.         $this->contactFormRoute $contactFormRoute;
  47.         $this->subscribeRoute $subscribeRoute;
  48.         $this->unsubscribeRoute $unsubscribeRoute;
  49.     }
  50.     /**
  51.      * @Since("6.1.0.0")
  52.      * @Route("/form/contact", name="frontend.form.contact.send", methods={"POST"}, defaults={"XmlHttpRequest"=true, "_captcha"=true})
  53.      */
  54.     public function sendContactForm(RequestDataBag $dataSalesChannelContext $context): JsonResponse
  55.     {
  56.         $response = [];
  57.         try {
  58.             $message $this->contactFormRoute
  59.                 ->load($data->toRequestDataBag(), $context)
  60.                 ->getResult()
  61.                 ->getIndividualSuccessMessage();
  62.             if (!$message) {
  63.                 $message $this->trans('contact.success');
  64.             }
  65.             $response[] = [
  66.                 'type' => 'success',
  67.                 'alert' => $message,
  68.             ];
  69.         } catch (ConstraintViolationException $formViolations) {
  70.             $violations = [];
  71.             foreach ($formViolations->getViolations() as $violation) {
  72.                 $violations[] = $violation->getMessage();
  73.             }
  74.             $response[] = [
  75.                 'type' => 'danger',
  76.                 'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
  77.                     'type' => 'danger',
  78.                     'list' => $violations,
  79.                 ]),
  80.             ];
  81.         } catch (RateLimitExceededException $exception) {
  82.             $response[] = [
  83.                 'type' => 'info',
  84.                 'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
  85.                     'type' => 'info',
  86.                     'content' => $this->trans('error.rateLimitExceeded', ['%seconds%' => $exception->getWaitTime()]),
  87.                 ]),
  88.             ];
  89.         }
  90.         return new JsonResponse($response);
  91.     }
  92.     /**
  93.      * @Since("6.1.0.0")
  94.      * @Route("/form/newsletter", name="frontend.form.newsletter.register.handle", methods={"POST"}, defaults={"XmlHttpRequest"=true, "_captcha"=true})
  95.      */
  96.     public function handleNewsletter(Request $requestRequestDataBag $dataSalesChannelContext $context): JsonResponse
  97.     {
  98.         $subscribe $data->get('option') === self::SUBSCRIBE;
  99.         if ($subscribe) {
  100.             $response $this->handleSubscribe($request$data$context);
  101.         } else {
  102.             $response $this->handleUnsubscribe($data$context);
  103.         }
  104.         return new JsonResponse($response);
  105.     }
  106.     private function handleSubscribe(Request $requestRequestDataBag $dataSalesChannelContext $context): array
  107.     {
  108.         try {
  109.             $data->set('storefrontUrl'$request->attributes->get(RequestTransformer::STOREFRONT_URL));
  110.             $this->subscribeRoute->subscribe($data$contextfalse);
  111.             $response[] = [
  112.                 'type' => 'success',
  113.                 'alert' => $this->trans('newsletter.subscriptionPersistedSuccess'),
  114.             ];
  115.             $response[] = [
  116.                 'type' => 'info',
  117.                 'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
  118.                     'type' => 'info',
  119.                     'list' => [$this->trans('newsletter.subscriptionPersistedInfo')],
  120.                 ]),
  121.             ];
  122.         } catch (ConstraintViolationException $exception) {
  123.             $errors = [];
  124.             foreach ($exception->getViolations() as $error) {
  125.                 $errors[] = $error->getMessage();
  126.             }
  127.             $response[] = [
  128.                 'type' => 'danger',
  129.                 'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
  130.                     'type' => 'danger',
  131.                     'list' => $errors,
  132.                 ]),
  133.             ];
  134.         } catch (\Exception $exception) {
  135.             $response[] = [
  136.                 'type' => 'danger',
  137.                 'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
  138.                     'type' => 'danger',
  139.                     'list' => [$this->trans('error.message-default')],
  140.                 ]),
  141.             ];
  142.         }
  143.         return $response;
  144.     }
  145.     private function handleUnsubscribe(RequestDataBag $dataSalesChannelContext $context): array
  146.     {
  147.         try {
  148.             $this->unsubscribeRoute->unsubscribe($data$context);
  149.             $response[] = [
  150.                 'type' => 'success',
  151.                 'alert' => $this->trans('newsletter.subscriptionRevokeSuccess'),
  152.             ];
  153.         } catch (ConstraintViolationException $exception) {
  154.             $errors = [];
  155.             foreach ($exception->getViolations() as $error) {
  156.                 $errors[] = $error->getMessage();
  157.             }
  158.             $response[] = [
  159.                 'type' => 'danger',
  160.                 'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
  161.                     'type' => 'danger',
  162.                     'list' => $errors,
  163.                 ]),
  164.             ];
  165.         } catch (\Exception $exception) {
  166.             $response = [];
  167.         }
  168.         return $response;
  169.     }
  170. }