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

Open in your IDE?
  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\Bundle\FrameworkBundle\Templating\EngineInterface;
  19. use Symfony\Component\Form\FormFactoryInterface;
  20. use Symfony\Component\HttpFoundation\RedirectResponse;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  24. use Symfony\Component\Routing\RouterInterface;
  25. use Twig\Environment;
  26. use Webmozart\Assert\Assert;
  27. final class ContactController
  28. {
  29.     /** @var RouterInterface */
  30.     private $router;
  31.     /** @var FormFactoryInterface */
  32.     private $formFactory;
  33.     /** @var EngineInterface|Environment */
  34.     private $templatingEngine;
  35.     /** @var ChannelContextInterface */
  36.     private $channelContext;
  37.     /** @var CustomerContextInterface */
  38.     private $customerContext;
  39.     /** @var LocaleContextInterface */
  40.     private $localeContext;
  41.     /** @var ContactEmailManagerInterface */
  42.     private $contactEmailManager;
  43.     /**
  44.      * @param EngineInterface|Environment $templatingEngine
  45.      */
  46.     public function __construct(
  47.         RouterInterface $router,
  48.         FormFactoryInterface $formFactory,
  49.         object $templatingEngine,
  50.         ChannelContextInterface $channelContext,
  51.         CustomerContextInterface $customerContext,
  52.         LocaleContextInterface $localeContext,
  53.         ContactEmailManagerInterface $contactEmailManager
  54.     ) {
  55.         $this->router $router;
  56.         $this->formFactory $formFactory;
  57.         $this->templatingEngine $templatingEngine;
  58.         $this->channelContext $channelContext;
  59.         $this->customerContext $customerContext;
  60.         $this->localeContext $localeContext;
  61.         $this->contactEmailManager $contactEmailManager;
  62.     }
  63.     public function requestAction(Request $request): Response
  64.     {
  65.         $formType $this->getSyliusAttribute($request'form'ContactType::class);
  66.         $form $this->formFactory->create($formTypenull$this->getFormOptions());
  67.         if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
  68.             $data $form->getData();
  69.             $channel $this->channelContext->getChannel();
  70.             /** @var ChannelInterface $channel */
  71.             Assert::isInstanceOf($channelChannelInterface::class);
  72.             $contactEmail $channel->getContactEmail();
  73.             if (null === $contactEmail) {
  74.                 $errorMessage $this->getSyliusAttribute(
  75.                     $request,
  76.                     'error_flash',
  77.                     'sylius.contact.request_error'
  78.                 );
  79.                 /** @var FlashBagInterface $flashBag */
  80.                 $flashBag $request->getSession()->getBag('flashes');
  81.                 $flashBag->add('error'$errorMessage);
  82.                 return new RedirectResponse($request->headers->get('referer'));
  83.             }
  84.             $localeCode $this->localeContext->getLocaleCode();
  85.             $this->contactEmailManager->sendContactRequest($data, [$contactEmail], $channel$localeCode);
  86.             $successMessage $this->getSyliusAttribute(
  87.                 $request,
  88.                 'success_flash',
  89.                 'sylius.contact.request_success'
  90.             );
  91.             /** @var FlashBagInterface $flashBag */
  92.             $flashBag $request->getSession()->getBag('flashes');
  93.             $flashBag->add('success'$successMessage);
  94.             $redirectRoute $this->getSyliusAttribute($request'redirect''referer');
  95.             return new RedirectResponse($this->router->generate($redirectRoute));
  96.         }
  97.         $template $this->getSyliusAttribute($request'template''@SyliusShop/Contact/request.html.twig');
  98.         return new Response($this->templatingEngine->render($template, ['form' => $form->createView()]));
  99.     }
  100.     private function getSyliusAttribute(Request $requeststring $attributeName, ?string $default): ?string
  101.     {
  102.         $attributes $request->attributes->get('_sylius');
  103.         return $attributes[$attributeName] ?? $default;
  104.     }
  105.     private function getFormOptions(): array
  106.     {
  107.         $customer $this->customerContext->getCustomer();
  108.         if (null === $customer) {
  109.             return [];
  110.         }
  111.         return ['email' => $customer->getEmail()];
  112.     }
  113. }