src/Controller/Mvc/RecruitController.php line 52

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Mvc;
  3. use App\Entity\Recruit\Entry;
  4. use App\Entity\Recruit\Inquiry;
  5. use App\Event\Recruit\Inquiry\PostMailSendEvent;
  6. use App\Event\Recruit\Inquiry\PostPersistEvent;
  7. use App\Event\Recruit\Inquiry\PrePersistEvent;
  8. use App\Form\Recruit\InquiryType;
  9. use App\MailConfigure\Recruit\InquiryConfigure;
  10. use App\Repository\Recruit\CategoryRepository;
  11. use App\Repository\Recruit\InquiryRepository;
  12. use App\Service\Entity\Recruit\EntryService;
  13. use App\Service\InquiryControllerService;
  14. use Psr\Log\LoggerInterface;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. #[Route("/entry")]
  21. class RecruitController extends AbstractController
  22. {
  23.     private const SESSION_NAME "recruit";
  24.     private const SESSION_DETAIL_ID "recruit_detail_id";
  25.     
  26.     public function __construct(
  27.         private readonly EntryService $entryService
  28.     ) {}
  29.     
  30.     private function getEntry(int $id): Entry
  31.     {
  32.         if(!$entry $this->entryService->getEntryById($id)) {
  33.             throw $this->createNotFoundException("entry not found");
  34.         }
  35.         return $entry;
  36.     }
  37.     
  38.     private function getEntryFromSession(Request $request): Entry
  39.     {
  40.         if(
  41.             !$request->getSession()->has(self::SESSION_DETAIL_ID) ||
  42.             !is_numeric($request->getSession()->get(self::SESSION_DETAIL_ID))
  43.         ) {
  44.             throw $this->createNotFoundException("Session invalid");
  45.         }
  46.         return $this->getEntry($request->getSession()->get(self::SESSION_DETAIL_ID));
  47.     }
  48.     
  49.     #[Route(path"/"name"recruit_index"methods: ["GET"])]
  50.     public function index(
  51.         Request $request,
  52.         CategoryRepository $categoryRepository,
  53.         EntryService $service
  54.     ): Response
  55.     {
  56.         $service->setContainer($this->container);
  57.         $paginate $service->getPagination($request);
  58.         return $this->render("pages/recruit/index.html.twig", [
  59.             "paginate" => $paginate,
  60.             "categories" => $categoryRepository->getCategoriesHaveEntry()
  61.         ]);
  62.     }
  63.     
  64.     #[Route(path"/headline"name"recruit_headline"methods: ["GET"])]
  65.     public function headline(
  66.         Request $request,
  67.         EntryService $service
  68.     ): Response
  69.     {
  70.         $service->setContainer($this->container);
  71.         $paginate $service->getPagination($request4);
  72.         return $this->render("pages/recruit/headline.html.twig", [
  73.             "paginate" => $paginate
  74.         ]);
  75.     }
  76.     
  77.     #[Route(path"/{id}"name"recruit_detail"requirements: ["id" => "\d+"], methods: ["GET"])]
  78.     public function detail(
  79.         Request $request,
  80.         InquiryControllerService $inquiryService,
  81.         int $id
  82.     ): Response
  83.     {
  84.         $entry $this->getEntry($id);
  85.         if(!$this->entryService->isEnableInquiry($entry)) {
  86.             return $this->render("pages/recruit/detail.html.twig", [
  87.                 "entry" => $entry,
  88.                 "form" => null,
  89.                 "retry" => false
  90.             ]);
  91.         }
  92.         $inquiryService->setContainer($this->container);
  93.         $inquiry = (new Inquiry())
  94.             ->setEntry($entry)
  95.         ;
  96.         $form $inquiryService->prepareForm(
  97.             $request,
  98.             $inquiry,
  99.             self::SESSION_NAME,
  100.             InquiryType::class, [
  101.                 "entry" => $entry
  102.             ]
  103.         );
  104.         $request->getSession()->set(self::SESSION_DETAIL_ID$entry->getId());
  105.         return $this->render("pages/recruit/detail.html.twig", [
  106.             "entry" => $entry,
  107.             "form" => $form->createView(),
  108.             "retry" => false
  109.         ]);
  110.     }
  111.     
  112.     #[Route(path"/confirm"name"recruit_confirm"methods: ["POST"])]
  113.     public function confirm(
  114.         Request $request,
  115.         InquiryControllerService $inquiryService,
  116.         InquiryRepository $inquiryRepository
  117.     ): Response
  118.     {
  119.         $inquiryService->setContainer($this->container);
  120.         $entry $this->getEntryFromSession($request);
  121.         $inquiry = (new Inquiry())
  122.         ->setEntry($entry)
  123.         ;
  124.         if(!$this->entryService->isEnableInquiry($entry)) {
  125.             return $this->redirectToRoute("recruit_detail", [
  126.                 "id" => $entry->getId()
  127.             ]);
  128.         }
  129.         $form $inquiryService->getSubmittedForm(
  130.             $request,
  131.             $inquiry,
  132.             InquiryType::class,
  133.             [
  134.                 "entry" => $entry
  135.             ]
  136.         );
  137.         
  138.         if(false === $inquiryService->formValidation($request$inquiry$form$inquiryRepository)) {
  139.             return $this->render("pages/recruit/detail.html.twig", [
  140.                 "entry" => $entry,
  141.                 "form" => $form->createView(),
  142.                 "retry" => true
  143.             ]);
  144.         }
  145.         return $this->render("pages/recruit/confirm.html.twig", [
  146.             "entry" => $entry,
  147.             "inquiry" => $inquiry,
  148.             "form" => $form->createView(),
  149.             "confirmForm" => $inquiryService
  150.                 ->saveAndGetConfirmForm($request$formself::SESSION_NAME)
  151.                 ->createView()
  152.         ]);
  153.     }
  154.     #[Route(path"/send"name"recruit_send"methods: ["POST"])]
  155.     public function send(
  156.         Request $request,
  157.         InquiryControllerService $inquiryService,
  158.         InquiryRepository $inquiryRepository,
  159.         InquiryConfigure $configure,
  160.         EventDispatcherInterface $eventDispatcher
  161.     ): Response
  162.     {
  163.         $inquiryService->setContainer($this->container);
  164.         $entry $this->getEntryFromSession($request);
  165.         if(!$inquiryService->handleConfirmForm($request)) {
  166.             return $this->redirectToRoute("recruit_detail", [
  167.                 "id" => $entry->getId()
  168.             ]);
  169.         }
  170.         if(!$this->entryService->isEnableInquiry($entry)) {
  171.             return $this->redirectToRoute("recruit_detail", [
  172.                 "id" => $entry->getId()
  173.             ]);
  174.         }
  175.         
  176.         $inquiry = (new Inquiry())
  177.         ->setEntry($entry)
  178.         ;
  179.         if(!$form $inquiryService->loadAndGetForm(
  180.             $request,
  181.             $inquiry,
  182.             InquiryType::class,
  183.             self::SESSION_NAME,
  184.             [
  185.                 "entry" => $entry
  186.             ]
  187.         )) {
  188.             return $this->redirectToRoute("recruit_detail", [
  189.                 "id" => $entry->getId()
  190.             ]);
  191.         }
  192.         
  193.         if(!$inquiryService->formValidation($request$inquiry$form$inquiryRepository)) {
  194.             return $this->render("pages/recruit/detail.html.twig", [
  195.                 "form" => $inquiryService->createRetryForm(
  196.                     InquiryType::class,
  197.                     $inquiry,
  198.                     "送信時にエラーが発生しました",
  199.                     [
  200.                         "entry" => $entry
  201.                     ]
  202.                 )->createView(),
  203.                 "retry" => true,
  204.                 "entry" => $entry
  205.             ]);
  206.         }
  207.         $preEvent = new PrePersistEvent($inquiry$form);
  208.         $eventDispatcher->dispatch($preEvent"recruit_inquiry.prePersist");
  209.         
  210.         $inquiryRepository->add($inquiry);
  211.         
  212.         $postEvent = new PostPersistEvent($inquiry$form);
  213.         $eventDispatcher->dispatch($postEvent"recruit_inquiry.postPersist");
  214.         
  215.         $sendSuccess true;
  216.         if(!$inquiryService->mailSend($configure"client"$inquiry$form, [
  217.             "entry" => $entry
  218.         ])) {
  219.             $sendSuccess false;
  220.         }
  221.         if(!$inquiryService->mailSend($configure"reply"$inquiry$form, [
  222.             "entry" => $entry
  223.         ])) {
  224.             $sendSuccess false;
  225.         }
  226.         if(false === $sendSuccess) {
  227.             return $this->render("pages/recruit/detail.html.twig", [
  228.                 "form" => $inquiryService->createRetryForm(
  229.                     InquiryType::class,
  230.                     $inquiry,
  231.                     "送信処理に失敗しました",
  232.                     [
  233.                         "entry" => $entry
  234.                     ]
  235.                 )->createView(),
  236.                 "retry" => true,
  237.                 "entry" => $entry
  238.             ]);
  239.         }
  240.         $postSendEvent = new PostMailSendEvent($inquiry$form);
  241.         $eventDispatcher->dispatch($postSendEvent"recruit_inquiry.postMailSend");
  242.         
  243.         
  244.         // pardot送信などの場合ここでHTMLレンダリング
  245.         // return $this->render("pages/recruit/send.html.twig", [
  246.         //     "inquiry" => $inquiry,
  247.         //     "entry" => $entry
  248.         // ]);
  249.         
  250.         return $this->redirectToRoute("recruit_complete");
  251.     }
  252.     
  253.     #[Route(path"/complete"name"recruit_complete"methods: ["GET"])]
  254.     public function complete(
  255.         Request $request,
  256.         InquiryControllerService $inquiryService
  257.     ): Response
  258.     {
  259.         $inquiryService->setContainer($this->container);
  260.         $entry $this->getEntryFromSession($request);
  261.         $inquiry = (new Inquiry())
  262.         ->setEntry($entry)
  263.         ;
  264.         if(!$form $inquiryService->loadAndGetForm(
  265.             $request,
  266.             $inquiry,
  267.             InquiryType::class,
  268.             self::SESSION_NAME,
  269.             [
  270.                 "entry" => $entry
  271.             ]
  272.         )) {
  273.             return $this->redirectToRoute("recruit_detail", [
  274.                 "id" => $entry->getId()
  275.             ]);
  276.         }
  277.         
  278.         $request->getSession()->remove(self::SESSION_NAME);
  279.         $request->getSession()->remove(self::SESSION_DETAIL_ID);
  280.         
  281.         return $this->render("pages/recruit/complete.html.twig", [
  282.             "entry" => $entry,
  283.             "form" => $form->createView()
  284.         ]);
  285.     }
  286.     
  287.     #[Route(path"/failure"name"recruit_failure"methods: ["GET"])]
  288.     public function failure(
  289.         Request $request,
  290.         InquiryControllerService $inquiryService,
  291.         InquiryConfigure $configure
  292.     ): Response
  293.     {
  294.         $inquiryService->setContainer($this->container);
  295.         $entry $this->getEntryFromSession($request);
  296.         $inquiry = (new Inquiry())
  297.         ->setEntry($entry)
  298.         ;
  299.         if(!$form $inquiryService->loadAndGetForm(
  300.             $request,
  301.             $inquiry,
  302.             InquiryType::class,
  303.             self::SESSION_NAME,
  304.             [
  305.                 "entry" => $entry
  306.             ]
  307.         )) {
  308.             return $this->redirectToRoute("recruit_complete", [
  309.                 "id" => $entry->getId()
  310.             ]);
  311.         }
  312.         
  313.         $inquiryService->mailSend($configure"pardotFailure"$inquiry$form);
  314.         
  315.         return $this->redirectToRoute("recruit_complete");
  316.     }
  317.     
  318.     #[Route(path"/pardot_mock"name"recruit_pardot_mock"methods: ["POST"])]
  319.     public function pardotMock(
  320.         Request $request,
  321.         LoggerInterface $logger
  322.     ): Response
  323.     {
  324.         $logger->info("採用エントリー pardot send:" .print_r($request->request->all(), true));
  325.         return $this->redirectToRoute("recruit_failure");
  326.     }
  327.     
  328.     #[Route(
  329.         path"/mock/{id}",
  330.         name"recruit_mock",
  331.         requirements: ["id" => "\d+"],
  332.         methods: ["GET"]
  333.     )]
  334.     public function mock(
  335.         Request $request,
  336.         InquiryControllerService $inquiryService,
  337.         int $id
  338.     ): Response
  339.     {
  340.         $inquiryService->setContainer($this->container);
  341.         $entry $this->getEntry($id);
  342.         
  343.         $inquiry $inquiryService->createMock(Inquiry::class)
  344.             ->setName('田中太郎')
  345.             ->setKana('たなかたろう')
  346.             ->setPhone('052-325-3648')
  347.             ->setEmail("info@triple-e.jp")
  348.             ->setMessage('採用エントリーのローカルの動作テストです')
  349.         ;
  350.         
  351.         $inquiryService->saveMock($request$inquiryInquiryType::class, self::SESSION_NAME, [
  352.             "entry" => $entry
  353.         ]);
  354.         return $this->redirectToRoute("recruit_detail", [
  355.             "id" => $entry->getId()
  356.         ]);
  357.     }
  358. }