vendor/sylius/sylius/src/Sylius/Bundle/ShopBundle/Controller/ContactController.php line 44

  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ShopBundle\Controller;
  12. use Sylius\Bundle\CoreBundle\Form\Type\ContactType;
  13. use Sylius\Bundle\ShopBundle\EmailManager\ContactEmailManagerInterface;
  14. use Sylius\Component\Channel\Context\ChannelContextInterface;
  15. use Sylius\Component\Core\Model\ChannelInterface;
  16. use Sylius\Component\Customer\Context\CustomerContextInterface;
  17. use Sylius\Component\Locale\Context\LocaleContextInterface;
  18. use Symfony\Component\Form\FormFactoryInterface;
  19. use Symfony\Component\HttpFoundation\RedirectResponse;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  23. use Symfony\Component\Routing\RouterInterface;
  24. use Twig\Environment;
  25. use Webmozart\Assert\Assert;
  26. final class ContactController
  27. {
  28.     public function __construct(
  29.         private RouterInterface $router,
  30.         private FormFactoryInterface $formFactory,
  31.         private Environment $templatingEngine,
  32.         private ChannelContextInterface $channelContext,
  33.         private CustomerContextInterface $customerContext,
  34.         private LocaleContextInterface $localeContext,
  35.         private ContactEmailManagerInterface $contactEmailManager,
  36.     ) {
  37.     }
  38.     public function requestAction(Request $request): Response
  39.     {
  40.         $formType $this->getSyliusAttribute($request'form'ContactType::class);
  41.         $form $this->formFactory->create($formTypenull$this->getFormOptions());
  42.         if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
  43.             $data $form->getData();
  44.             $channel $this->channelContext->getChannel();
  45.             /** @var ChannelInterface $channel */
  46.             Assert::isInstanceOf($channelChannelInterface::class);
  47.             $contactEmail $channel->getContactEmail();
  48.             if (null === $contactEmail) {
  49.                 $errorMessage $this->getSyliusAttribute(
  50.                     $request,
  51.                     'error_flash',
  52.                     'sylius.contact.request_error',
  53.                 );
  54.                 /** @var FlashBagInterface $flashBag */
  55.                 $flashBag $request->getSession()->getBag('flashes');
  56.                 $flashBag->add('error'$errorMessage);
  57.                 return new RedirectResponse($request->headers->get('referer'));
  58.             }
  59.             $localeCode $this->localeContext->getLocaleCode();
  60.             $this->contactEmailManager->sendContactRequest($data, [$contactEmail], $channel$localeCode);
  61.             $successMessage $this->getSyliusAttribute(
  62.                 $request,
  63.                 'success_flash',
  64.                 'sylius.contact.request_success',
  65.             );
  66.             /** @var FlashBagInterface $flashBag */
  67.             $flashBag $request->getSession()->getBag('flashes');
  68.             $flashBag->add('success'$successMessage);
  69.             $redirectRoute $this->getSyliusAttribute($request'redirect''referer');
  70.             return new RedirectResponse($this->router->generate($redirectRoute));
  71.         }
  72.         $template $this->getSyliusAttribute($request'template''@SyliusShop/Contact/request.html.twig');
  73.         return new Response($this->templatingEngine->render($template, ['form' => $form->createView()]));
  74.     }
  75.     private function getSyliusAttribute(Request $requeststring $attributeName, ?string $default): ?string
  76.     {
  77.         $attributes $request->attributes->get('_sylius');
  78.         return $attributes[$attributeName] ?? $default;
  79.     }
  80.     private function getFormOptions(): array
  81.     {
  82.         $customer $this->customerContext->getCustomer();
  83.         if (null === $customer) {
  84.             return [];
  85.         }
  86.         return ['email' => $customer->getEmail()];
  87.     }
  88. }