- <?php
- /**
-  * This file is part of ApproveCustomer42
-  *
-  * Copyright(c) Akira Kurozumi <info@a-zumi.net>
-  *
-  *  https://a-zumi.net
-  *
-  * For the full copyright and license information, please view the LICENSE
-  * file that was distributed with this source code.
-  */
- namespace Plugin\ApproveCustomer42;
- use Eccube\Entity\BaseInfo;
- use Eccube\Entity\Customer;
- use Eccube\Event\EccubeEvents;
- use Eccube\Service\MailService;
- use Plugin\ApproveCustomer42\Entity\Config;
- use Plugin\ApproveCustomer42\Repository\ConfigRepository;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- use Eccube\Event\EventArgs;
- use Eccube\Repository\BaseInfoRepository;
- use Eccube\Repository\MailTemplateRepository;
- use Symfony\Component\HttpFoundation\RedirectResponse;
- use Symfony\Component\Mailer\MailerInterface;
- use Symfony\Component\Mime\Address;
- use Symfony\Component\Mime\Email;
- use Symfony\Component\Routing\RouterInterface;
- use Twig\Environment;
- class Event implements EventSubscriberInterface
- {
-     /**
-      * @var BaseInfo
-      */
-     private $BaseInfo;
-     /**
-      * @var Environment
-      */
-     private $twig;
-     /**
-      * @var MailTemplateRepository
-      */
-     private $mailTemplateRepository;
-     /**
-      * @var RouterInterface
-      */
-     private $router;
-     /**
-      * @var MailerInterface
-      */
-     private $mailer;
-     /**
-      * @var Config
-      */
-     private $config;
-     /**
-      * @var MailService
-      */
-     private $mailService;
-     public function __construct(
-         BaseInfoRepository $baseInfoRepository,
-         Environment $twig,
-         MailTemplateRepository $mailTemplateRepository,
-         RouterInterface $router,
-         MailerInterface $mailer,
-         ConfigRepository $configRepository,
-         MailService $mailService
-     )
-     {
-         $this->BaseInfo = $baseInfoRepository->get();
-         $this->twig = $twig;
-         $this->mailTemplateRepository = $mailTemplateRepository;
-         $this->router = $router;
-         $this->mailer = $mailer;
-         $this->config = $configRepository->get();
-         $this->mailService = $mailService;
-     }
-     /**
-      * @return array
-      */
-     public static function getSubscribedEvents()
-     {
-         return [
-             EccubeEvents::FRONT_ENTRY_INDEX_COMPLETE => ["onFrontEntryIndexComplete", 10],
-             EccubeEvents::MAIL_CUSTOMER_CONFIRM => ["onMailCustomerConfirm", 10],
-             EccubeEvents::MAIL_ADMIN_CUSTOMER_CONFIRM => ["onMailCustomerConfirm", 10],
-         ];
-     }
-     /**
-      * @param EventArgs $event
-      */
-     public function onFrontEntryIndexComplete(EventArgs $event)
-     {
-         if (false === $this->BaseInfo->isOptionCustomerActivate()) {
-             return;
-         }
-         $event->setResponse(
-             new RedirectResponse($this->router->generate('plugin_approve_customer4_entry_complete'))
-         );
-     }
-     /**
-      * @param EventArgs $event
-      * @throws \Twig\Error\LoaderError
-      * @throws \Twig\Error\RuntimeError
-      * @throws \Twig\Error\SyntaxError
-      */
-     public function onMailCustomerConfirm(EventArgs $event)
-     {
-         /** @var Email $message */
-         $message = $event->getArgument('message');
-         $Customer = $event->getArgument('Customer');
-         $BaseInfo = $event->getArgument('BaseInfo');
-         $activateUrl = $event->getArgument('activateUrl');
-         if (false === $BaseInfo->isOptionCustomerActivate()) {
-             return;
-         }
-         $MailTemplate = $this->mailTemplateRepository->findOneBy([
-             "file_name" => "Mail/ApproveCustomer42/default/Mail/Entry/confirm.twig"
-         ]);
-         if (!$MailTemplate) {
-             log_error('メールテンプレートが登録されていません。');
-             return;
-         }
-         $body = $this->twig->render($MailTemplate->getFileName(), [
-             'Customer' => $Customer,
-             'BaseInfo' => $BaseInfo,
-             'activateUrl' => $activateUrl,
-         ]);
-         $message->subject('[' . $BaseInfo->getShopName() . '] ' . $MailTemplate->getMailSubject());
-         $message->to($this->mailService->convertRFCViolatingEmail($BaseInfo->getEmail01()));
-         $message->text($body);
-         if($this->config->isSendCustomerMail()) {
-             $this->sendCustomerMail($BaseInfo, $Customer);
-         }
-     }
-     protected function sendCustomerMail(BaseInfo $baseInfo, Customer $customer)
-     {
-         $MailTemplate = $this->mailTemplateRepository->findOneBy([
-             "file_name" => "Mail/ApproveCustomer42/default/Mail/Entry/customer.twig"
-         ]);
-         if (!$MailTemplate) {
-             log_error('メールテンプレートが登録されていません。');
-             return;
-         }
-         $body = $this->twig->render($MailTemplate->getFileName(), [
-             'Customer' => $customer,
-             'BaseInfo' => $baseInfo,
-         ]);
-         $message = (new Email())
-             ->subject('['.$baseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
-             ->from(new Address($baseInfo->getEmail01(), $baseInfo->getShopName()))
-             ->to($this->mailService->convertRFCViolatingEmail($customer->getEmail()))
-             ->replyTo($baseInfo->getEmail03())
-             ->returnPath($baseInfo->getEmail04())
-             ->text($body);
-         $this->mailer->send($message);
-     }
- }
-