app/Plugin/ApproveCustomer42/Event.php line 121

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of ApproveCustomer42
  4.  *
  5.  * Copyright(c) Akira Kurozumi <info@a-zumi.net>
  6.  *
  7.  *  https://a-zumi.net
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\ApproveCustomer42;
  13. use Eccube\Entity\BaseInfo;
  14. use Eccube\Entity\Customer;
  15. use Eccube\Event\EccubeEvents;
  16. use Eccube\Service\MailService;
  17. use Plugin\ApproveCustomer42\Entity\Config;
  18. use Plugin\ApproveCustomer42\Repository\ConfigRepository;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Eccube\Event\EventArgs;
  21. use Eccube\Repository\BaseInfoRepository;
  22. use Eccube\Repository\MailTemplateRepository;
  23. use Symfony\Component\HttpFoundation\RedirectResponse;
  24. use Symfony\Component\Mailer\MailerInterface;
  25. use Symfony\Component\Mime\Address;
  26. use Symfony\Component\Mime\Email;
  27. use Symfony\Component\Routing\RouterInterface;
  28. use Twig\Environment;
  29. class Event implements EventSubscriberInterface
  30. {
  31.     /**
  32.      * @var BaseInfo
  33.      */
  34.     private $BaseInfo;
  35.     /**
  36.      * @var Environment
  37.      */
  38.     private $twig;
  39.     /**
  40.      * @var MailTemplateRepository
  41.      */
  42.     private $mailTemplateRepository;
  43.     /**
  44.      * @var RouterInterface
  45.      */
  46.     private $router;
  47.     /**
  48.      * @var MailerInterface
  49.      */
  50.     private $mailer;
  51.     /**
  52.      * @var Config
  53.      */
  54.     private $config;
  55.     /**
  56.      * @var MailService
  57.      */
  58.     private $mailService;
  59.     public function __construct(
  60.         BaseInfoRepository $baseInfoRepository,
  61.         Environment $twig,
  62.         MailTemplateRepository $mailTemplateRepository,
  63.         RouterInterface $router,
  64.         MailerInterface $mailer,
  65.         ConfigRepository $configRepository,
  66.         MailService $mailService
  67.     )
  68.     {
  69.         $this->BaseInfo $baseInfoRepository->get();
  70.         $this->twig $twig;
  71.         $this->mailTemplateRepository $mailTemplateRepository;
  72.         $this->router $router;
  73.         $this->mailer $mailer;
  74.         $this->config $configRepository->get();
  75.         $this->mailService $mailService;
  76.     }
  77.     /**
  78.      * @return array
  79.      */
  80.     public static function getSubscribedEvents()
  81.     {
  82.         return [
  83.             EccubeEvents::FRONT_ENTRY_INDEX_COMPLETE => ["onFrontEntryIndexComplete"10],
  84.             EccubeEvents::MAIL_CUSTOMER_CONFIRM => ["onMailCustomerConfirm"10],
  85.             EccubeEvents::MAIL_ADMIN_CUSTOMER_CONFIRM => ["onMailCustomerConfirm"10],
  86.         ];
  87.     }
  88.     /**
  89.      * @param EventArgs $event
  90.      */
  91.     public function onFrontEntryIndexComplete(EventArgs $event)
  92.     {
  93.         if (false === $this->BaseInfo->isOptionCustomerActivate()) {
  94.             return;
  95.         }
  96.         $event->setResponse(
  97.             new RedirectResponse($this->router->generate('plugin_approve_customer4_entry_complete'))
  98.         );
  99.     }
  100.     /**
  101.      * @param EventArgs $event
  102.      * @throws \Twig\Error\LoaderError
  103.      * @throws \Twig\Error\RuntimeError
  104.      * @throws \Twig\Error\SyntaxError
  105.      */
  106.     public function onMailCustomerConfirm(EventArgs $event)
  107.     {
  108.         /** @var Email $message */
  109.         $message $event->getArgument('message');
  110.         $Customer $event->getArgument('Customer');
  111.         $BaseInfo $event->getArgument('BaseInfo');
  112.         $activateUrl $event->getArgument('activateUrl');
  113.         if (false === $BaseInfo->isOptionCustomerActivate()) {
  114.             return;
  115.         }
  116.         $MailTemplate $this->mailTemplateRepository->findOneBy([
  117.             "file_name" => "Mail/ApproveCustomer42/default/Mail/Entry/confirm.twig"
  118.         ]);
  119.         if (!$MailTemplate) {
  120.             log_error('メールテンプレートが登録されていません。');
  121.             return;
  122.         }
  123.         $body $this->twig->render($MailTemplate->getFileName(), [
  124.             'Customer' => $Customer,
  125.             'BaseInfo' => $BaseInfo,
  126.             'activateUrl' => $activateUrl,
  127.         ]);
  128.         $message->subject('[' $BaseInfo->getShopName() . '] ' $MailTemplate->getMailSubject());
  129.         $message->to($this->mailService->convertRFCViolatingEmail($BaseInfo->getEmail01()));
  130.         $message->text($body);
  131.         if($this->config->isSendCustomerMail()) {
  132.             $this->sendCustomerMail($BaseInfo$Customer);
  133.         }
  134.     }
  135.     protected function sendCustomerMail(BaseInfo $baseInfoCustomer $customer)
  136.     {
  137.         $MailTemplate $this->mailTemplateRepository->findOneBy([
  138.             "file_name" => "Mail/ApproveCustomer42/default/Mail/Entry/customer.twig"
  139.         ]);
  140.         if (!$MailTemplate) {
  141.             log_error('メールテンプレートが登録されていません。');
  142.             return;
  143.         }
  144.         $body $this->twig->render($MailTemplate->getFileName(), [
  145.             'Customer' => $customer,
  146.             'BaseInfo' => $baseInfo,
  147.         ]);
  148.         $message = (new Email())
  149.             ->subject('['.$baseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
  150.             ->from(new Address($baseInfo->getEmail01(), $baseInfo->getShopName()))
  151.             ->to($this->mailService->convertRFCViolatingEmail($customer->getEmail()))
  152.             ->replyTo($baseInfo->getEmail03())
  153.             ->returnPath($baseInfo->getEmail04())
  154.             ->text($body);
  155.         $this->mailer->send($message);
  156.     }
  157. }