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 App\Utils\ParameterBagUtil;
  7. use Psr\Container\ContainerInterface;
  8. use Psr\Log\LoggerInterface;
  9. use ReCaptcha\ReCaptcha;
  10. use Symfony\Component\Form\FormError;
  11. use Symfony\Component\Form\FormInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Mime\Email;
  14. use TripleE\Utilities\FormUtil;
  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 $checkRecaptcha true
  100.     ): bool {
  101.         $isValid true;
  102.         if($form->isSubmitted() && $form->isValid()) {
  103.             $inquiry->setIp($request->getClientIp());
  104.             if (
  105.                 ParameterBagUtil::$bag->get('kernel.environment') === "dev" &&
  106.                 ParameterBagUtil::$bag->get('inquiry.form_validation') === "false"
  107.             ) {
  108.                 return true;
  109.             }
  110.             if ($repository->isContinuePost($inquiry$thresholdTime)) {
  111.                 $form->addError(new FormError("連続送信は時間をおいてください"));
  112.                 $isValid false;
  113.             }
  114.             if($checkRecaptcha) {
  115.                 $reCaptchaResponse $this->reCaptcha->verify(
  116.                     $request->request->get('g-recaptcha-response'),
  117.                     $request->getClientIp()
  118.                 );
  119.                 if (!$reCaptchaResponse->isSuccess()) {
  120.                     $form->addError(new FormError("不正な処理を感知しました"));
  121.                     $this->logger->notice(
  122.                         "reCaptcha invalid. "print_r($reCaptchaResponse->getErrorCodes(), true)
  123.                     );
  124.                     $isValid false;
  125.                 }
  126.             }
  127.         } else {
  128.             $form->addError(new FormError("入力内容に不備がありました"));
  129.             $isValid false;
  130.         }
  131.         return $isValid;
  132.     }
  133.     /**
  134.      * 送信前のCSRFトークンチェック
  135.      * @param Request $request
  136.      * @return bool
  137.      */
  138.     public function handleConfirmForm(Request $request): bool
  139.     {
  140.         $form $this->getConfirmForm();
  141.         $form->handleRequest($request);
  142.         return ($form->isSubmitted() && $form->isValid());
  143.     }
  144.     public function getConfirmForm(): FormInterface
  145.     {
  146.         return $this->container->get('form.factory')->create();
  147.     }
  148.     /**
  149.      * セッションに保存されているデータを持たせたフォームを返す
  150.      * @param Request $request
  151.      * @param InquiryInterface $inquiry
  152.      * @param string $formType
  153.      * @param string $sessionName
  154.      * @param array $formOptions
  155.      * @return FormInterface|null
  156.      * @throws \Psr\Container\ContainerExceptionInterface
  157.      * @throws \Psr\Container\NotFoundExceptionInterface
  158.      */
  159.     public function loadAndGetForm(
  160.         Request $request,
  161.         InquiryInterface $inquiry,
  162.         string $formType,
  163.         string $sessionName,
  164.         array $formOptions = []
  165.     ): ?FormInterface
  166.     {
  167.         if(!$request->getSession()->has($sessionName)) {
  168.             return null;
  169.         }
  170.         $form $this->container->get('form.factory')->create($formType$inquiryarray_merge(
  171.             $formOptions,
  172.             [
  173.                 "csrf_protection" => false
  174.             ]
  175.         ));
  176.         $form->submit($request->getSession()->get($sessionName));
  177.         return $form;
  178.     }
  179.     /**
  180.      * メールを送信する
  181.      * @param MailConfigureInterface $configure
  182.      * @param string $type
  183.      * @param InquiryInterface $inquiry
  184.      * @param FormInterface $form
  185.      * @param array $twigAssign
  186.      * @return Email|null
  187.      */
  188.     public function mailSend(
  189.         MailConfigureInterface $configure,
  190.         string $type,
  191.         InquiryInterface $inquiry,
  192.         FormInterface $form,
  193.         array $twigAssign = []
  194.     ): ?Email {
  195.         $twigAssign["form"] = $form->createView();
  196.         $config $configure->getOption($type$inquiry$twigAssign);
  197.         try {
  198.             return $configure->getMailService()->send($config);
  199.         } catch (\Throwable $e) {
  200.             $this->logger->error($e->getMessage());
  201.             return null;
  202.         }
  203.     }
  204.     /**
  205.      * モックデータの作成
  206.      * @param string $className
  207.      * @return InquiryInterface
  208.      */
  209.     public function createMock(string $className): InquiryInterface
  210.     {
  211.         return (new $className)
  212.             ->setEmail('info@triple-e.inc')
  213.             ->setName('飯見裕一郎')
  214.             ->setKana('いいみゆういちろう')
  215.             ->setPhone("052-325-3648")
  216.             ->setMessage('お問合せ内容が表示されます。お問合せ内容が表示されます。
  217. お問合せ内容が表示されます。お問合せ内容が表示されます。お問合せ内容が表示されます。お問合せ内容が表示されます。お問合せ内容が表示されます。お問合せ内容が表示されます。お問合せ内容が表示されます。お問合せ内容が表示されます。お問合せ内容が表示されます。お問合せ内容が表示されます。お問合せ内容が表示されます。お問合せ内容が表示されます。')
  218.             ;
  219.     }
  220.     /**
  221.      * @param Request $request
  222.      * @param InquiryInterface $inquiry
  223.      * @param string $formType
  224.      * @param string $sessionName
  225.      * @param array $formOptions
  226.      * @return void
  227.      * @throws \Psr\Container\ContainerExceptionInterface
  228.      * @throws \Psr\Container\NotFoundExceptionInterface
  229.      */
  230.     public function saveMock(
  231.         Request $request,
  232.         InquiryInterface $inquiry,
  233.         string $formType,
  234.         string $sessionName,
  235.         array $formOptions = []
  236.     ): void {
  237.         $form $this->getForm($formType$inquiry$formOptions);
  238.         $data FormUtil::getArrayData($form);
  239.         $request->getSession()->set($sessionName$data);
  240.     }
  241.     /**
  242.      * Formの内容をセッションに保存して確認ページ用のからのフォームを返す
  243.      * @param Request $request
  244.      * @param FormInterface $form
  245.      * @param string $sessionName
  246.      * @return FormInterface
  247.      */
  248.     public function saveAndGetConfirmForm(
  249.         Request $request,
  250.         FormInterface $form,
  251.         string $sessionName
  252.     ): FormInterface {
  253.         $request->getSession()->set($sessionNameFormUtil::getViewData($form));
  254.         return $this->getConfirmForm();
  255.     }
  256.     /**
  257.      * 送信失敗時にリトライ用のフォームを作成する
  258.      * @param string $formType
  259.      * @param InquiryInterface $inquiry
  260.      * @param string|null $errorMessage
  261.      * @param array $formOptions
  262.      * @return FormInterface
  263.      * @throws \Psr\Container\ContainerExceptionInterface
  264.      * @throws \Psr\Container\NotFoundExceptionInterface
  265.      */
  266.     public function createRetryForm(
  267.         string $formType,
  268.         InquiryInterface $inquiry,
  269.         string $errorMessage null,
  270.         array $formOptions = array()
  271.     ): FormInterface
  272.     {
  273.         $form $this->getForm($formType$inquiry$formOptions);
  274.         $form->get('agreement')->setData(true);
  275.         if($errorMessage) {
  276.             $form->addError(new FormError($errorMessage));
  277.         }
  278.         return $form;
  279.     }
  280. }