src/Service/InquiryControllerService.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Interfaces\InquiryInterface;
  4. use App\MailConfigure\MailConfigureInterface;
  5. use App\Repository\Interfaces\InquiryRepositoryInterface;
  6. use Psr\Container\ContainerInterface;
  7. use Psr\Log\LoggerInterface;
  8. use ReCaptcha\ReCaptcha;
  9. use Symfony\Component\Form\FormError;
  10. use Symfony\Component\Form\FormInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Mime\Email;
  13. use TripleE\Utilities\FormUtil;
  14. use TripleE\Utilities\ParameterBagUtil;
  15. class InquiryControllerService
  16. {
  17.     protected ContainerInterface $container;
  18.     public function __construct(
  19.         private ReCaptcha $reCaptcha,
  20.         private LoggerInterface $logger
  21.     ) {}
  22.     public function setContainer(ContainerInterface $container): void
  23.     {
  24.         $this->container $container;
  25.     }
  26.     /**
  27.      * フォーム入力(indexページ)で使用するフォームを準備する。セッションにデータがあれば割り当てる
  28.      * @param Request $request
  29.      * @param InquiryInterface $inquiry
  30.      * @param string $sessionName
  31.      * @param string $formType
  32.      * @param array $formOptions
  33.      * @return FormInterface
  34.      * @throws \Psr\Container\ContainerExceptionInterface
  35.      * @throws \Psr\Container\NotFoundExceptionInterface
  36.      */
  37.     public function prepareForm(
  38.         Request $request,
  39.         InquiryInterface $inquiry,
  40.         string $sessionName,
  41.         string $formType,
  42.         array $formOptions = []
  43.     ): FormInterface {
  44.         $form $this->getForm($formType$inquiry$formOptions);
  45.         if($request->getSession()->has($sessionName)) {
  46.             $form->submit($request->getSession()->get($sessionName));
  47.         }
  48.         return $form;
  49.     }
  50.     /**
  51.      * フォームを作成
  52.      * @param string $formType
  53.      * @param InquiryInterface $inquiry
  54.      * @param array $formOptions
  55.      * @return FormInterface
  56.      * @throws \Psr\Container\ContainerExceptionInterface
  57.      * @throws \Psr\Container\NotFoundExceptionInterface
  58.      */
  59.     public function getForm(string $formTypeInquiryInterface $inquiry, array $formOptions): FormInterface
  60.     {
  61.         return $this->container->get('form.factory')->create($formType$inquiry$formOptions);
  62.     }
  63.     /**
  64.      * 確認ページでFormにRequestをアサインして送信済みFormを返す
  65.      * @param Request $request
  66.      * @param InquiryInterface $inquiry
  67.      * @param string $formType
  68.      * @param array $formOptions
  69.      * @return FormInterface
  70.      * @throws \Psr\Container\ContainerExceptionInterface
  71.      * @throws \Psr\Container\NotFoundExceptionInterface
  72.      */
  73.     public function getSubmittedForm(
  74.         Request $request,
  75.         InquiryInterface $inquiry,
  76.         string $formType,
  77.         array $formOptions = []
  78.     ): FormInterface
  79.     {
  80.         $form $this->getForm($formType$inquiry$formOptions);
  81.         $form->handleRequest($request);
  82.         return $form;
  83.     }
  84.     /**
  85.      * フォームのバリデーションを行う
  86.      * @param Request $request
  87.      * @param InquiryInterface $inquiry
  88.      * @param FormInterface $form
  89.      * @param InquiryRepositoryInterface $repository
  90.      * @param string $thresholdTime
  91.      * @return bool
  92.      */
  93.     public function formValidation(
  94.         Request $request,
  95.         InquiryInterface $inquiry,
  96.         FormInterface $form,
  97.         InquiryRepositoryInterface $repository,
  98.         string $thresholdTime "-20 second"
  99.     ): bool {
  100.         $isValid true;
  101.         if($form->isSubmitted() && $form->isValid()) {
  102.             $inquiry->setIp($request->getClientIp());
  103.             if (
  104.                 ParameterBagUtil::$bag->get('kernel.environment') === "dev" &&
  105.                 ParameterBagUtil::$bag->get('inquiry.form_validation') === "false"
  106.             ) {
  107.                 return true;
  108.             }
  109.             if ($repository->isContinuePost($inquiry$thresholdTime)) {
  110.                 $form->addError(new FormError("連続送信は時間をおいてください"));
  111.                 $isValid false;
  112.             }
  113.             $reCaptchaResponse $this->reCaptcha->verify(
  114.                 $request->request->get('g-recaptcha-response'),
  115.                 $request->getClientIp()
  116.             );
  117.             if (!$reCaptchaResponse->isSuccess()) {
  118.                 $form->addError(new FormError("不正な処理を感知しました"));
  119.                 $this->logger->notice(
  120.                     "reCaptcha invalid. "print_r($reCaptchaResponse->getErrorCodes(), true)
  121.                 );
  122.                 $isValid false;
  123.             }
  124.         } else {
  125.             $form->addError(new FormError("入力内容に不備がありました"));
  126.             $isValid false;
  127.         }
  128.         return $isValid;
  129.     }
  130.     /**
  131.      * 送信前のCSRFトークンチェック
  132.      * @param Request $request
  133.      * @return bool
  134.      */
  135.     public function handleConfirmForm(Request $request): bool
  136.     {
  137.         $form $this->getConfirmForm();
  138.         $form->handleRequest($request);
  139.         return ($form->isSubmitted() && $form->isValid());
  140.     }
  141.     public function getConfirmForm(): FormInterface
  142.     {
  143.         return $this->container->get('form.factory')->create();
  144.     }
  145.     /**
  146.      * セッションに保存されているデータを持たせたフォームを返す
  147.      * @param Request $request
  148.      * @param InquiryInterface $inquiry
  149.      * @param string $formType
  150.      * @param string $sessionName
  151.      * @param array $formOptions
  152.      * @return FormInterface|null
  153.      * @throws \Psr\Container\ContainerExceptionInterface
  154.      * @throws \Psr\Container\NotFoundExceptionInterface
  155.      */
  156.     public function loadAndGetForm(
  157.         Request $request,
  158.         InquiryInterface $inquiry,
  159.         string $formType,
  160.         string $sessionName,
  161.         array $formOptions = []
  162.     ): ?FormInterface
  163.     {
  164.         if(!$request->getSession()->has($sessionName)) {
  165.             return null;
  166.         }
  167.         $form $this->container->get('form.factory')->create($formType$inquiryarray_merge(
  168.             $formOptions,
  169.             [
  170.                 "csrf_protection" => false
  171.             ]
  172.         ));
  173.         $form->submit($request->getSession()->get($sessionName));
  174.         return $form;
  175.     }
  176.     /**
  177.      * メールを送信する
  178.      * @param MailConfigureInterface $configure
  179.      * @param string $type
  180.      * @param InquiryInterface $inquiry
  181.      * @param FormInterface $form
  182.      * @param array $twigAssign
  183.      * @return Email|null
  184.      */
  185.     public function mailSend(
  186.         MailConfigureInterface $configure,
  187.         string $type,
  188.         InquiryInterface $inquiry,
  189.         FormInterface $form,
  190.         array $twigAssign = []
  191.     ): ?Email {
  192.         $twigAssign["form"] = $form->createView();
  193.         $config $configure->getOption($type$inquiry$twigAssign);
  194.         try {
  195.             return $configure->getMailService()->send($config);
  196.         } catch (\Throwable $e) {
  197.             $this->logger->error($e->getMessage());
  198.             return null;
  199.         }
  200.     }
  201.     /**
  202.      * モックデータの作成
  203.      * @param string $className
  204.      * @return InquiryInterface
  205.      */
  206.     public function createMock(string $className): InquiryInterface
  207.     {
  208.         return (new $className)
  209.             ->setEmail('info@triple-e.inc')
  210.             ;
  211.     }
  212.     /**
  213.      * @param Request $request
  214.      * @param InquiryInterface $inquiry
  215.      * @param string $formType
  216.      * @param string $sessionName
  217.      * @param array $formOptions
  218.      * @return void
  219.      * @throws \Psr\Container\ContainerExceptionInterface
  220.      * @throws \Psr\Container\NotFoundExceptionInterface
  221.      */
  222.     public function saveMock(
  223.         Request $request,
  224.         InquiryInterface $inquiry,
  225.         string $formType,
  226.         string $sessionName,
  227.         array $formOptions = []
  228.     ): void {
  229.         $form $this->getForm($formType$inquiry$formOptions);
  230.         $data FormUtil::getArrayData($form);
  231.         $request->getSession()->set($sessionName$data);
  232.     }
  233.     /**
  234.      * Formの内容をセッションに保存して確認ページ用のからのフォームを返す
  235.      * @param Request $request
  236.      * @param FormInterface $form
  237.      * @param string $sessionName
  238.      * @return FormInterface
  239.      */
  240.     public function saveAndGetConfirmForm(
  241.         Request $request,
  242.         FormInterface $form,
  243.         string $sessionName
  244.     ): FormInterface {
  245.         $request->getSession()->set($sessionNameFormUtil::getViewData($form));
  246.         return $this->getConfirmForm();
  247.     }
  248.     /**
  249.      * 送信失敗時にリトライ用のフォームを作成する
  250.      * @param string $formType
  251.      * @param InquiryInterface $inquiry
  252.      * @param string|null $errorMessage
  253.      * @param array $formOptions
  254.      * @return FormInterface
  255.      * @throws \Psr\Container\ContainerExceptionInterface
  256.      * @throws \Psr\Container\NotFoundExceptionInterface
  257.      */
  258.     public function createRetryForm(
  259.         string $formType,
  260.         InquiryInterface $inquiry,
  261.         string $errorMessage null,
  262.         array $formOptions = array()
  263.     ): FormInterface
  264.     {
  265.         $form $this->getForm($formType$inquiry$formOptions);
  266.         $form->get('agreement')->setData(true);
  267.         if($errorMessage) {
  268.             $form->addError(new FormError($errorMessage));
  269.         }
  270.         return $form;
  271.     }
  272. }