src/Controller/SuluPageController.php line 121

Open in your IDE?
  1. <?php
  2. /*
  3.  * 2001-2019 Reflet Communication
  4.  *
  5.  * NOTICE OF LICENSE
  6.  *
  7.  * This source file is subject to the Open Software License (OSL 3.0) that is available
  8.  * through the world-wide-web at this URL: http://www.opensource.org/licenses/OSL-3.0
  9.  * If you are unable to obtain it through the world-wide-web, please send an email
  10.  * to agence@reflet-digital.com so we can send you a copy immediately.
  11.  *
  12.  * @author Reflet Communication
  13.  * @copyright 2001-2019 Reflet Communication
  14.  * @license http://www.opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
  15.  */
  16. namespace App\Controller;
  17. use App\Entity\Contact;
  18. use App\Event\ContactEvent;
  19. use App\Event\ContactFormEvent;
  20. use App\Exception\BadResponseException;
  21. use App\Filter\ProxyFilter;
  22. use App\Form\Type\BasicContactType;
  23. use App\Manager\Proxy\ProxyEntityManagerInterface;
  24. use App\Manager\Proxy\SuluManager;
  25. use App\Routing\CustomPageLoader;
  26. use App\Service\MailChimpService;
  27. use App\Service\ProxyService;
  28. use Aws\Exception\AwsException;
  29. use Psr\Log\LoggerInterface;
  30. use Rc\MailChimp\Exception\MailChimpException;
  31. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  32. use Symfony\Component\HttpFoundation\Request;
  33. use Symfony\Component\HttpFoundation\RequestStack;
  34. use Symfony\Component\HttpFoundation\Response;
  35. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  36. use Symfony\Component\Routing\Annotation\Route;
  37. use Symfony\Contracts\Cache\TagAwareCacheInterface;
  38. /**
  39.  * Class SuluPageController
  40.  *
  41.  * @package App\Controller
  42.  *
  43.  * @Route(
  44.  *     name = "sulu_page_"
  45.  * )
  46.  */
  47. class SuluPageController extends BaseController
  48. {
  49.     const CUSTOM_PAGE_FORM_NAME 'Opération spéciale';
  50.     /**
  51.      * @var ProxyEntityManagerInterface
  52.      */
  53.     protected $proxyEntityManager;
  54.     /**
  55.      * @var ProxyFilter
  56.      */
  57.     protected $proxyFilter;
  58.     /**
  59.      * SuluPageController constructor.
  60.      *
  61.      * @param RequestStack $stack
  62.      * @param ProxyService $proxyService
  63.      * @param ProxyFilter $proxyFilter
  64.      * @param TagAwareCacheInterface $cache
  65.      */
  66.     public function __construct(
  67.         RequestStack $stack,
  68.         ProxyService $proxyService,
  69.         ProxyFilter $proxyFilter,
  70.         TagAwareCacheInterface $cache
  71.     ) {
  72.         $request $stack->getCurrentRequest();
  73.         $this->proxyEntityManager = new SuluManager($proxyService$request->server$cache);
  74.         $this->proxyFilter $proxyFilter;
  75.     }
  76.     /**
  77.      * @Route(
  78.      *     "/{path}",
  79.      *     requirements = {
  80.      *         "path" = "^(([a-z0-9]+(-[a-z0-9]+)*)(/([a-z0-9]+(-[a-z0-9]+)*))*)$"
  81.      *     },
  82.      *     name = "index"
  83.      * )
  84.      *
  85.      * @param Request $request
  86.      * @param LoggerInterface $logger
  87.      * @param string $path
  88.      *
  89.      * @return Response
  90.      */
  91.     public function index(Request $requestLoggerInterface $loggerstring $path): Response
  92.     {
  93.         $query $request
  94.             ->query
  95.             ->all()
  96.         ;
  97.         if (!empty($query)) {
  98.             $path .= '?'.http_build_query($query);
  99.         }
  100.         try {
  101.             $content $this
  102.                 ->proxyEntityManager
  103.                 ->getContent('/' $path)
  104.             ;
  105.         } catch (BadResponseException $exception) {
  106.             $response $exception->getResponse();
  107.             if (isset($response)) {
  108.                 $body strval($response->getBody());
  109.                 $logger->error('Sulu call error: '.$path, [
  110.                     $body,
  111.                 ]);
  112.             }
  113.             throw new NotFoundHttpException('Page not found');
  114.         }
  115.         $proxyFilter $this->proxyFilter;
  116.         $content $proxyFilter($request$content);
  117.         $firstImage $proxyFilter->getImage($content);
  118.         return $this->render('controller/sulu-page/index.html.twig', [
  119.             'proxyContent' => $content,
  120.             'firstImage' => $firstImage,
  121.         ]);
  122.     }
  123.     /**
  124.      * @param Request $request
  125.      * @param LoggerInterface $logger
  126.      * @param MailChimpService $mailChimp
  127.      * @param EventDispatcherInterface $eventDispatcher
  128.      *
  129.      * @return Response
  130.      */
  131.     public function custom(
  132.         Request $request,
  133.         LoggerInterface $logger,
  134.         MailChimpService $mailChimp,
  135.         EventDispatcherInterface $eventDispatcher
  136.     ): Response
  137.     {
  138.         $path $request->getRequestUri();
  139.         $path CustomPageLoader::PATH_PREFIX.$path;
  140.         try {
  141.             $content $this
  142.                 ->proxyEntityManager
  143.                 ->getContent($path)
  144.             ;
  145.         } catch (BadResponseException $exception) {
  146.             $response $exception->getResponse();
  147.             if (isset($response)) {
  148.                 $body strval($response->getBody());
  149.                 $logger->error('Sulu call error: '.$path, [
  150.                     $body,
  151.                 ]);
  152.             }
  153.             throw new NotFoundHttpException('Page not found');
  154.         }
  155.         $proxyFilter $this->proxyFilter;
  156.         $content $proxyFilter($request$content);
  157.         $firstImage $proxyFilter->getImage($content);
  158.         $contact = new Contact();
  159.         $form $this->createForm(BasicContactType::class, $contact);
  160.         $route $request
  161.             ->attributes
  162.             ->get('_route')
  163.         ;
  164.         if ($proxyFilter->hasContactForm($content)) {
  165.             $form->handleRequest($request);
  166.             if (
  167.                 $form->isSubmitted()
  168.                 && $form->isValid()
  169.             ) {
  170.                 $contact $form->getData();
  171.                 $customerEvent = new ContactFormEvent(
  172.                     $contact,
  173.                     'user_acknowledgement',
  174.                     $proxyFilter->getMailMeta($content'user/subject'),
  175.                     $proxyFilter->getMailMeta($content'user/content')
  176.                 );
  177.                 $salesEvent = new ContactFormEvent(
  178.                     $contact,
  179.                     'sales_notification',
  180.                     $proxyFilter->getMailMeta($content'sales/subject'),
  181.                     $proxyFilter->getMailMeta($content'sales/content')
  182.                 );
  183.                 try {
  184.                     $eventDispatcher->dispatch($customerEventContactFormEvent::NAME);
  185.                 } catch (AwsException $e) {
  186.                     // Ignore
  187.                 }
  188.                 try {
  189.                     $eventDispatcher->dispatch($salesEventContactFormEvent::NAME);
  190.                     $this->addFlash(
  191.                         'success',
  192.                         'Votre demande a bien été envoyée.'
  193.                     );
  194.                 } catch (AwsException $e) {
  195.                     $this->addFlash(
  196.                         'error',
  197.                         'Une erreur s\'est produite lors de l\'envoi de votre demande. Veuillez réessayer ultérieurement.'
  198.                     );
  199.                 }
  200.                 if (!empty($contact->getNewsletterSubscribe())) {
  201.                     $email $contact->getEmail();
  202.                     try {
  203.                         try {
  204.                             $result $mailChimp->get($email);
  205.                             if (
  206.                                 $result->status === MailChimpService::STATUS_ARCHIVED
  207.                                 || $result->status === MailChimpService::STATUS_UNSUBSCRIBED
  208.                             ) {
  209.                                 $result $mailChimp->put($email);
  210.                             }
  211.                         } catch (MailChimpException $e) {
  212.                             $result $mailChimp->put($email);
  213.                         }
  214.                         $this->addFlash(
  215.                             'success',
  216.                             $result->status === MailChimpService::STATUS_PENDING
  217.                                 ContactController::NEWSLETTER_SUCCESS_NEW
  218.                                 ContactController::NEWSLETTER_SUCCESS_OLD
  219.                         );
  220.                     } catch (MailChimpException $e) {
  221.                         $this->addFlash(
  222.                             'error',
  223.                             'Une erreur s\'est produite lors de votre inscription à la newsletter. Veuillez réessayer ultérieurement.'
  224.                         );
  225.                     }
  226.                 }
  227.                 return $this->redirectToRoute($route);
  228.             }
  229.             $proxyFilter->setContactForm($content$formself::CUSTOM_PAGE_FORM_NAME$route);
  230.         }
  231.         return $this->render('controller/sulu-page/index.html.twig', [
  232.             'proxyContent' => $content,
  233.             'firstImage' => $firstImage,
  234.         ]);
  235.     }
  236. }