src/Controller/Mvc/ContactController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Mvc;
  3. use App\Entity\Contact\Data;
  4. use App\Event\Contact\Data\Send\PostMailSendEvent;
  5. use App\Event\Contact\Data\Send\PostPersistEvent;
  6. use App\Event\Contact\Data\Send\PrePersistEvent;
  7. use App\Form\Contact\DataType;
  8. use App\MailConfigure\Contact\DataConfigure;
  9. use App\Repository\Contact\DataRepository;
  10. use App\Service\InquiryControllerService;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. #[Route(path"/contact")]
  18. class ContactController extends AbstractController
  19. {
  20.     private const SESSION_NAME "contact";
  21.     private const INDEX_TEMPLATE "pages/contact/index.html.twig";
  22.     private const INDEX_ROUTE "contact_index";
  23.     
  24.     #[Route(
  25.         path"/",
  26.         name"contact_index",
  27.         methods: ["GET"]
  28.     )]
  29.     public function index(
  30.         Request $request,
  31.         InquiryControllerService $service
  32.     ): Response
  33.     {
  34.         $service->setContainer($this->container);
  35.         $form $service->prepareForm(
  36.             $request,
  37.             new Data(),
  38.             self::SESSION_NAME,
  39.             DataType::class
  40.         );
  41.         return $this->render(self::INDEX_TEMPLATE, [
  42.             "form" => $form->createView(),
  43.             "retry" => false
  44.         ]);
  45.     }
  46.     
  47.     #[Route(
  48.         path"/confirm",
  49.         name"contact_confirm",
  50.         methods: ["POST"]
  51.     )]
  52.     public function confirm(
  53.         Request $request,
  54.         InquiryControllerService $service,
  55.         DataRepository $repository
  56.     ): Response
  57.     {
  58.         $service->setContainer($this->container);
  59.         
  60.         $contact = new Data();
  61.         $form $service->getSubmittedForm(
  62.             $request,
  63.             $contact,
  64.             DataType::class
  65.         );
  66.         if(false === $service->formValidation($request$contact$form$repositoryfalse)) {
  67.             return $this->render(self::INDEX_TEMPLATE, [
  68.                 "form" => $form->createView(),
  69.                 "retry" => true
  70.             ]);
  71.         }
  72.         return $this->render("pages/contact/confirm.html.twig", [
  73.             "inquiry" => $contact,
  74.             "form" => $form->createView(),
  75.             "confirmForm" => $service
  76.                 ->saveAndGetConfirmForm($request$formself::SESSION_NAME)
  77.                 ->createView()
  78.         ]);
  79.     }
  80.     
  81.     #[Route(
  82.         path"/send",
  83.         name"contact_send",
  84.         methods: ["POST"]
  85.     )]
  86.     public function send(
  87.         Request $request,
  88.         InquiryControllerService $service,
  89.         DataRepository $repository,
  90.         DataConfigure $configure,
  91.         EventDispatcherInterface $eventDispatcher
  92.     ): Response
  93.     {
  94.         $service->setContainer($this->container);
  95.         if(!$service->handleConfirmForm($request)) {
  96.             return $this->redirectToRoute(self::INDEX_ROUTE);
  97.         }
  98.         $contact = new Data();
  99.         if(!$form $service->loadAndGetForm(
  100.             $request,
  101.             $contact,
  102.             DataType::class,
  103.             self::SESSION_NAME
  104.         )) {
  105.             return $this->redirectToRoute(self::INDEX_ROUTE);
  106.         }
  107.         
  108.         if(!$service->formValidation($request$contact$form$repository)) {
  109.             return $this->render(self::INDEX_TEMPLATE, [
  110.                 "form" => $service->createRetryForm(
  111.                     DataType::class,
  112.                     $contact,
  113.                     "送信時にエラーが発生しました"
  114.                 )->createView(),
  115.                 "retry" => true
  116.             ]);
  117.         }
  118.         
  119.         $preEvent = new PrePersistEvent($contact$form);
  120.         $eventDispatcher->dispatch($preEvent"contact_data_send.pre_persist");
  121.         
  122.         $repository->add($contact);
  123.         
  124.         $postEvent = new PostPersistEvent($contact$form);
  125.         $eventDispatcher->dispatch($postEvent"contact_data_send.post_persist");
  126.         
  127.         $sendSuccess true;
  128.         if(!$service->mailSend($configure"client"$contact$form)) {
  129.             $sendSuccess false;
  130.         }
  131.         if(!$service->mailSend($configure"reply"$contact$form)) {
  132.             $sendSuccess false;
  133.         }
  134.         if(false === $sendSuccess) {
  135.             return $this->render(self::INDEX_TEMPLATE, [
  136.                 "form" => $service->createRetryForm(
  137.                     DataType::class,
  138.                     $contact,
  139.                     "送信処理に失敗しました"
  140.                 )->createView(),
  141.                 "retry" => true
  142.             ]);
  143.         }
  144.         $postSendEvent = new PostMailSendEvent($contact$form);
  145.         $eventDispatcher->dispatch($postSendEvent"contact_data_send.post_mail_send");
  146.         
  147.         // pardot送信などの場合ここでHTMLレンダリング
  148.         // return $this->render("pages/contact/send.html.twig", [
  149.         //     "inquiry" => $contact
  150.         // ]);
  151.         
  152.         return $this->redirectToRoute("contact_complete");
  153.     }
  154.     
  155.     #[Route(
  156.         path"/complete",
  157.         name"contact_complete",
  158.         methods: ["GET"]
  159.     )]
  160.     public function complete(
  161.         Request $request
  162.     ): Response
  163.     {
  164.         $request->getSession()->remove(self::SESSION_NAME);
  165.         return $this->render("pages/contact/complete.html.twig", []);
  166.     }
  167.     
  168.     #[Route(
  169.         path"/failure",
  170.         name"contact_failure",
  171.         methods: ["GET"]
  172.     )]
  173.     public function failure(
  174.         Request $request,
  175.         InquiryControllerService $service,
  176.         DataConfigure $configure
  177.     ): Response
  178.     {
  179.         $service->setContainer($this->container);
  180.         $contact = new Data();
  181.         if(!$form $service->loadAndGetForm(
  182.             $request,
  183.             $contact,
  184.             DataType::class,
  185.             self::SESSION_NAME
  186.         )) {
  187.             return $this->redirectToRoute("contact_complete");
  188.         }
  189.         $service->mailSend($configure"pardotFailure"$contact$form);
  190.         
  191.         return $this->redirectToRoute("contact_complete");
  192.     }
  193.     
  194.     #[Route(
  195.         path"/pardot_mock",
  196.         name"contact_pardot_mock",
  197.         methods: ["POST"]
  198.     )]
  199.     public function pardotMock(
  200.         Request $request,
  201.         LoggerInterface $logger
  202.     ): Response
  203.     {
  204.         $logger->info("お問合わせ pardot send: "print_r($request->request->all(), true));
  205.         return $this->redirectToRoute("contact_failure");
  206.     }
  207.     
  208.     #[Route(
  209.         path"/mock",
  210.         name"contact_mock",
  211.         methods: ["GET"]
  212.     )]
  213.     public function mock(
  214.         Request $request,
  215.         InquiryControllerService $service
  216.     ): Response
  217.     {
  218.         $service->setContainer($this->container);
  219.         $contact $service->createMock(Data::class);
  220.         $service->saveMock($request$contactDataType::class, self::SESSION_NAME);
  221.         return $this->redirectToRoute(self::INDEX_ROUTE);
  222.     }
  223. }