src/Controller/Mvc/ContactController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Mvc;
  3. use App\Entity\Account\Account;
  4. use App\Entity\Account\Owner;
  5. use App\Form\Account\OwnerRegistrationType;
  6. use App\Service\InquiryControllerService;
  7. use App\Utils\FormUtil;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. /**
  15.  * @Route("/contact")
  16.  */
  17. class ContactController extends AbstractController
  18. {
  19.     private const SESSION_NAME "contact_resume";
  20.     /**
  21.      * @Route("/", name="contact_index", methods={"GET"})
  22.      */
  23.     public function index(
  24.         Request $request,
  25.         InquiryControllerService $inquiryService
  26.     ): Response {
  27.         $form $inquiryService->prepareForm(
  28.             $request,
  29.             self::SESSION_NAME,
  30.             OwnerRegistrationType::class,
  31.             $this->createEmptyAccount()
  32.         );
  33.         return $this->render("pages/contact/index.html.twig", [
  34.             "form" => $form->createView(),
  35.             "retry" => false
  36.         ]);
  37.     }
  38.     /**
  39.      * @Route("/confirm", name="contact_confirm", methods={"POST"})
  40.      */
  41.     public function confirm(
  42.         Request $request,
  43.         InquiryControllerService $inquiryService
  44.     ): Response {
  45.         $Account $this->createEmptyAccount();
  46.         $form $inquiryService->getSubmittedForm(
  47.             $request,
  48.             OwnerRegistrationType::class,
  49.             $Account
  50.         );
  51.         if(false === $inquiryService->formValidation($request$form$Account)) {
  52.             return $this->render("pages/contact/index.html.twig", [
  53.                 "form" => $form->createView(),
  54.                 "retry" => true
  55.             ]);
  56.         }
  57.         return $this->render('pages/contact/confirm.html.twig', [
  58.             "inquiry" => $Account,
  59.             "form" => $form->createView(),
  60.             "confirmForm" => $inquiryService->saveSession($request$formself::SESSION_NAME)->createView()
  61.         ]);
  62.     }
  63.     /**
  64.      * @Route("/send", name="contact_send", methods={"POST"})
  65.      */
  66.     public function send(
  67.         Request $request,
  68.         InquiryControllerService $inquiryService,
  69.         UserPasswordHasherInterface $passwordHasher
  70.     ): Response {
  71.         // confirm csrf
  72.         if(!$inquiryService->handleConfirmForm($request)) {
  73.             return $this->redirectToRoute("contact_index");
  74.         }
  75.         // form data load from session
  76.         $Account $this->createEmptyAccount();
  77.         if(!$form $inquiryService->loadSessionAndGetForm(
  78.             $request,
  79.             $Account,
  80.             OwnerRegistrationType::class,
  81.             self::SESSION_NAME
  82.         )) {
  83.             return $this->redirectToRoute('contact_index');
  84.         }
  85.         // validate again
  86.         if(!$inquiryService->formValidation($request$form$Account)) {
  87.             return $this->render('pages/contact/index.html.twig', [
  88.                 "form" => $form->createView(),
  89.                 "retry" => true,
  90.             ]);
  91.         }
  92.         $Account
  93.             ->setPassword(
  94.                 $passwordHasher->hashPassword(
  95.                     $Account,
  96.                     $form->get('plain_password')->getData()
  97.                 )
  98.             )
  99.             ->setRoles(["ROLE_OWNER"])
  100.             ->getOwner()->setStatus(Owner::STATUS_DISABLE)
  101.         ;
  102.         // persist - sendmail
  103.         $inquiryService
  104.             ->persistAndSessionRemove($request$Accountself::SESSION_NAME)
  105.             ->sendmail($Account$form)
  106.         ;
  107.         // perdotのような送信がある場合、HTMLをここでレンダリング
  108. //        return $this->render('ownerregistration/send.html.twig', [
  109. //            "inquiry" => $Ownerregistration
  110. //        ]);
  111.         // goto complete
  112.         return $this->redirectToRoute('contact_complete');
  113.     }
  114.     /**
  115.      * @Route("/complete", name="contact_complete", methods={"GET"})
  116.      */
  117.     public function complete(): Response
  118.     {
  119.         return $this->render("pages/contact/complete.html.twig", []);
  120.     }
  121.     /**
  122.      * @Route("/pardot_mock", name="contact_pardot_mock", methods={"POST"})
  123.      */
  124.     public function pardotMock(Request $requestLoggerInterface $logger) {
  125.         $logger->info('Owner registration pardot mock. send: 'print_r($request->request->all(), true));
  126.         return $this->redirectToRoute('contact_complete');
  127.     }
  128.     /**
  129.      * @Route("/dev_mock", name="contact_dev_mock", methods={"GET"})
  130.      */
  131.     public function devMock(Request $request) {
  132.         $Account $this->createEmptyAccount();
  133.         $Account
  134.             ->setEmail('dev_mock@triple-e.jp')
  135.             ->getOwner()
  136.             ->setCompanyName('Triple-E JAPAN')
  137.             ->setCompanyKana('トリプルイージャパン')
  138.             ->setManagerName('運営者名')
  139.             ->setRepresentativeName('飯見裕一郎')
  140.             ->setRepresentativeKana('いいみゆういちろう')
  141.             ->setChargerName('土屋慎二')
  142.             ->setPhone('052-325-3648')
  143.             ->setAddress('愛知県名古屋市中村区大閤3丁目7-76')
  144.             ->setUrl('https://triple-e.jp')
  145.             ;
  146.         $form $this->createForm(OwnerRegistrationType::class, $Account);
  147.         $request->getSession()->set(self::SESSION_NAMEFormUtil::getViewData($form));
  148.         return $this->redirectToRoute('contact_index');
  149.     }
  150.     /**
  151.      * @return Account
  152.      */
  153.     private function createEmptyAccount(): Account
  154.     {
  155.         $Account = new Account();
  156.         $Owner = new Owner();
  157.         $Account->setOwner($Owner);
  158.         return $Account;
  159.     }
  160. }