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

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