src/Controller/Shop/ContactController.php line 60

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Shop;
  4. use App\Events\PostContactEvent;
  5. use App\Files\Uploader\ContactUploader;
  6. use Sylius\Bundle\CoreBundle\Form\Type\ContactType;
  7. use Sylius\Bundle\ShopBundle\EmailManager\ContactEmailManagerInterface;
  8. use Sylius\Component\Channel\Context\ChannelContextInterface;
  9. use Sylius\Component\Core\Model\ChannelInterface;
  10. use Sylius\Component\Customer\Context\CustomerContextInterface;
  11. use Sylius\Component\Locale\Context\LocaleContextInterface;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\Form\FormFactoryInterface;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  18. use Symfony\Component\Routing\RouterInterface;
  19. use Twig\Environment;
  20. use Webmozart\Assert\Assert;
  21. final class ContactController
  22. {
  23.     private RouterInterface $router;
  24.     private FormFactoryInterface $formFactory;
  25.     private Environment $templatingEngine;
  26.     private ChannelContextInterface $channelContext;
  27.     private CustomerContextInterface $customerContext;
  28.     private LocaleContextInterface $localeContext;
  29.     private ContactEmailManagerInterface $contactEmailManager;
  30.     private ContactUploader $contactUploader;
  31.     private EventDispatcherInterface $eventDispatcher;
  32.     public function __construct(
  33.         RouterInterface $router,
  34.         FormFactoryInterface $formFactory,
  35.         Environment $templatingEngine,
  36.         ChannelContextInterface $channelContext,
  37.         CustomerContextInterface $customerContext,
  38.         LocaleContextInterface $localeContext,
  39.         ContactEmailManagerInterface $contactEmailManager,
  40.         ContactUploader $contactUploader,
  41.         EventDispatcherInterface $eventDispatcher
  42.     ) {
  43.         $this->router $router;
  44.         $this->formFactory $formFactory;
  45.         $this->templatingEngine $templatingEngine;
  46.         $this->channelContext $channelContext;
  47.         $this->customerContext $customerContext;
  48.         $this->localeContext $localeContext;
  49.         $this->contactEmailManager $contactEmailManager;
  50.         $this->contactUploader $contactUploader;
  51.         $this->eventDispatcher $eventDispatcher;
  52.     }
  53.     public function requestAction(Request $request): Response
  54.     {
  55.         $formType $this->getSyliusAttribute($request'form'ContactType::class);
  56.         $form $this->formFactory->create($formTypenull$this->getFormOptions());
  57.         if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
  58.             $data $form->getData();
  59.             $channel $this->channelContext->getChannel();
  60.             /** @var ChannelInterface $channel */
  61.             Assert::isInstanceOf($channelChannelInterface::class);
  62.             $contactEmail $channel->getContactEmail();
  63.             if (null === $contactEmail) {
  64.                 $errorMessage $this->getSyliusAttribute(
  65.                     $request,
  66.                     'error_flash',
  67.                     'sylius.contact.request_error'
  68.                 );
  69.                 /** @var FlashBagInterface $flashBag */
  70.                 $flashBag $request->getSession()->getBag('flashes');
  71.                 $flashBag->add('error'$errorMessage);
  72.                 return new RedirectResponse($request->headers->get('referer'));
  73.             }
  74.             $localeCode $this->localeContext->getLocaleCode();
  75.             $arrayAttachmentsPath = [];
  76.             foreach ($data['attachment'] as $attachment){
  77.                 if ($attachment !== null) {
  78.                     $attachmentPath $this->contactUploader->upload($attachment);
  79.                     $arrayAttachmentsPath[] = $attachmentPath;
  80.                 }
  81.             }
  82.             $data['attachmentPath'] = $arrayAttachmentsPath;
  83.             $this->contactEmailManager->sendContactRequest($data, [$contactEmail], $channel$localeCode);
  84.             $successMessage $this->getSyliusAttribute(
  85.                 $request,
  86.                 'success_flash',
  87.                 'sylius.contact.request_success'
  88.             );
  89.             /** @var FlashBagInterface $flashBag */
  90.             $flashBag $request->getSession()->getBag('flashes');
  91.             $flashBag->add('success'$successMessage);
  92.             $redirectRoute $this->getSyliusAttribute($request'redirect''referer');
  93.             //event datalayer
  94.             $event = new PostContactEvent($data['subject']);
  95.             $this->eventDispatcher->dispatch($eventPostContactEvent::NAME);
  96.             return new RedirectResponse($this->router->generate($redirectRoute));
  97.         }
  98.         $template $this->getSyliusAttribute($request'template''@SyliusShop/Contact/request.html.twig');
  99.         return new Response$this->templatingEngine->render($template, ['form' => $form->createView()]));
  100.     }
  101.     private function getSyliusAttribute(Request $requeststring $attributeName, ?string $default): ?string
  102.     {
  103.         $attributes $request->attributes->get('_sylius');
  104.         return $attributes[$attributeName] ?? $default;
  105.     }
  106.     private function getFormOptions(): array
  107.     {
  108.         $customer $this->customerContext->getCustomer();
  109.         if (null === $customer) {
  110.             return [];
  111.         }
  112.         return ['email' => $customer->getEmail()];
  113.     }
  114. }