app/Customize/Controller/EntryController.php line 118

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  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 Customize\Controller;
  13. use Eccube\Entity\BaseInfo;
  14. use Eccube\Entity\Master\CustomerStatus;
  15. use Eccube\Event\EccubeEvents;
  16. use Eccube\Event\EventArgs;
  17. use Eccube\Form\Type\Front\EntryType;
  18. use Eccube\Repository\BaseInfoRepository;
  19. use Eccube\Repository\CustomerRepository;
  20. use Eccube\Repository\Master\CustomerStatusRepository;
  21. use Eccube\Service\MailService;
  22. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpKernel\Exception as HttpException;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  27. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  28. use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  29. use Symfony\Component\Validator\Constraints as Assert;
  30. use Symfony\Component\Validator\Validator\ValidatorInterface;
  31. use Eccube\Service\CartService;
  32. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  33. use Eccube\Controller\AbstractController;
  34. class EntryController extends AbstractController
  35. {
  36.     /**
  37.      * @var CustomerStatusRepository
  38.      */
  39.     protected $customerStatusRepository;
  40.     /**
  41.      * @var ValidatorInterface
  42.      */
  43.     protected $recursiveValidator;
  44.     /**
  45.      * @var MailService
  46.      */
  47.     protected $mailService;
  48.     /**
  49.      * @var BaseInfo
  50.      */
  51.     protected $BaseInfo;
  52.     /**
  53.      * @var CustomerRepository
  54.      */
  55.     protected $customerRepository;
  56.     /**
  57.      * @var EncoderFactoryInterface
  58.      */
  59.     protected $encoderFactory;
  60.     /**
  61.      * @var TokenStorageInterface
  62.      */
  63.     protected $tokenStorage;
  64.     /**
  65.      * @var \Eccube\Service\CartService
  66.      */
  67.     protected $cartService;
  68.     /**
  69.      * EntryController constructor.
  70.      *
  71.      * @param CartService $cartService
  72.      * @param CustomerStatusRepository $customerStatusRepository
  73.      * @param MailService $mailService
  74.      * @param BaseInfoRepository $baseInfoRepository
  75.      * @param CustomerRepository $customerRepository
  76.      * @param EncoderFactoryInterface $encoderFactory
  77.      * @param ValidatorInterface $validatorInterface
  78.      * @param TokenStorageInterface $tokenStorage
  79.      */
  80.     public function __construct(
  81.         CartService $cartService,
  82.         CustomerStatusRepository $customerStatusRepository,
  83.         MailService $mailService,
  84.         BaseInfoRepository $baseInfoRepository,
  85.         CustomerRepository $customerRepository,
  86.         EncoderFactoryInterface $encoderFactory,
  87.         ValidatorInterface $validatorInterface,
  88.         TokenStorageInterface $tokenStorage
  89.     ) {
  90.         $this->customerStatusRepository $customerStatusRepository;
  91.         $this->mailService $mailService;
  92.         $this->BaseInfo $baseInfoRepository->get();
  93.         $this->customerRepository $customerRepository;
  94.         $this->encoderFactory $encoderFactory;
  95.         $this->recursiveValidator $validatorInterface;
  96.         $this->tokenStorage $tokenStorage;
  97.         $this->cartService $cartService;
  98.     }
  99.     /**
  100.      * 会員登録画面.
  101.      *
  102.      * @Route("/entry", name="entry")
  103.      * @Template("Entry/index.twig")
  104.      */
  105.     public function index(Request $request)
  106.     {
  107.         if ($this->isGranted('ROLE_USER')) {
  108.             log_info('認証済のためログイン処理をスキップ');
  109.             return $this->redirectToRoute('mypage');
  110.         }
  111.         // カスタマイズ:GET 'redir' が渡されたら、変数にいれる
  112.         // Completeページ後にページ遷移させる
  113.         $redir $request->request->get('redir') ? $request->request->get('redir') : $request->query->get('redir');
  114.         /** @var $Customer \Eccube\Entity\Customer */
  115.         $Customer $this->customerRepository->newCustomer();
  116.         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  117.         $builder $this->formFactory->createBuilder(EntryType::class, $Customer);
  118.         $event = new EventArgs(
  119.             [
  120.                 'builder' => $builder,
  121.                 'Customer' => $Customer,
  122.             ],
  123.             $request
  124.         );
  125.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_INDEX_INITIALIZE$event);
  126.         /* @var $form \Symfony\Component\Form\FormInterface */
  127.         $form $builder->getForm();
  128.         $form->handleRequest($request);
  129.         if ($form->isSubmitted() && $form->isValid()) {
  130.             switch ($request->get('mode')) {
  131.                 case 'confirm':
  132.                     log_info('会員登録確認開始');
  133.                     log_info('会員登録確認完了');
  134.                     return $this->render(
  135.                         'Entry/confirm.twig',
  136.                         [
  137.                             'form' => $form->createView(),
  138.                             'redir' => $redir,
  139.                         ]
  140.                     );
  141.                 case 'complete':
  142.                     log_info('会員登録開始');
  143.                     $encoder $this->encoderFactory->getEncoder($Customer);
  144.                     $salt $encoder->createSalt();
  145.                     $password $encoder->encodePassword($Customer->getPassword(), $salt);
  146.                     $secretKey $this->customerRepository->getUniqueSecretKey();
  147.                     $Customer
  148.                         ->setSalt($salt)
  149.                         ->setPassword($password)
  150.                         ->setSecretKey($secretKey)
  151.                         ->setPoint(0);
  152.                     $this->entityManager->persist($Customer);
  153.                     $this->entityManager->flush();
  154.                     log_info('会員登録完了');
  155.                     $event = new EventArgs(
  156.                         [
  157.                             'form' => $form,
  158.                             'Customer' => $Customer,
  159.                         ],
  160.                         $request
  161.                     );
  162.                     $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_INDEX_COMPLETE$event);
  163.                     $activateUrl $this->generateUrl('entry_activate', ['secret_key' => $Customer->getSecretKey()], UrlGeneratorInterface::ABSOLUTE_URL);
  164.                     $activateFlg $this->BaseInfo->isOptionCustomerActivate();
  165.                     // 仮会員設定が有効な場合は、確認メールを送信し完了画面表示.
  166.                     if ($activateFlg) {
  167.                         // メール送信
  168.                         $this->mailService->sendCustomerConfirmMail($Customer$activateUrl);
  169.                         if ($event->hasResponse()) {
  170.                             return $event->getResponse();
  171.                         }
  172.                         log_info('仮会員登録完了画面へリダイレクト');
  173.                         return $this->redirectToRoute('entry_complete');
  174.                     // 仮会員設定が無効な場合は認証URLへ遷移させ、会員登録を完了させる.
  175.                     } else {
  176.                         log_info('本会員登録画面へリダイレクト');
  177.                         if(empty($redir)) {
  178.                             return $this->redirect($activateUrl);
  179.                         }
  180.                         else {
  181.                             log_info('redir リダイレクト: ' $redir);
  182.                             return $this->redirect($activateUrl '?redir='.$redir);
  183.                         }
  184.                     }
  185.             }
  186.         }
  187.         return [
  188.             'form' => $form->createView(),
  189.             'redir' => $redir,
  190.         ];
  191.     }
  192.     /**
  193.      * 会員のアクティベート(本会員化)を行う.
  194.      *
  195.      * @Route("/entry/activate/{secret_key}", name="entry_activate")
  196.      * @Template("Entry/activate.twig")
  197.      */
  198.     public function activate(Request $request$secret_key)
  199.     {
  200.         $errors $this->recursiveValidator->validate(
  201.             $secret_key,
  202.             [
  203.                 new Assert\NotBlank(),
  204.                 new Assert\Regex(
  205.                     [
  206.                         'pattern' => '/^[a-zA-Z0-9]+$/',
  207.                     ]
  208.                 ),
  209.             ]
  210.         );
  211.         if ($request->getMethod() === 'GET' && count($errors) === 0) {
  212.             // カスタマイズ:GET 'redir' が渡されたら、変数にいれる
  213.             // Completeページ後にページ遷移させる
  214.             $redir $request->query->get('redir');
  215.             log_info('本会員登録開始');
  216.             $Customer $this->customerRepository->getProvisionalCustomerBySecretKey($secret_key);
  217.             if (is_null($Customer)) {
  218.                 throw new HttpException\NotFoundHttpException();
  219.             }
  220.             $CustomerStatus $this->customerStatusRepository->find(CustomerStatus::REGULAR);
  221.             $Customer->setStatus($CustomerStatus);
  222.             $this->entityManager->persist($Customer);
  223.             $this->entityManager->flush();
  224.             log_info('本会員登録完了');
  225.             $event = new EventArgs(
  226.                 [
  227.                     'Customer' => $Customer,
  228.                 ],
  229.                 $request
  230.             );
  231.             $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_ACTIVATE_COMPLETE$event);
  232.             // メール送信
  233.             $this->mailService->sendCustomerCompleteMail($Customer);
  234.             // Assign session carts into customer carts
  235.             $Carts $this->cartService->getCarts();
  236.             $qtyInCart array_reduce($Carts, function ($qty$Cart) {
  237.                 return $qty $Cart->getTotalQuantity();
  238.             });
  239.             // 本会員登録してログイン状態にする
  240.             $token = new UsernamePasswordToken($Customernull'customer', ['ROLE_USER']);
  241.             $this->tokenStorage->setToken($token);
  242.             $request->getSession()->migrate(true);
  243.             if ($qtyInCart) {
  244.                 $this->cartService->save();
  245.             }
  246.             log_info('ログイン済に変更', [$this->getUser()->getId()]);
  247.             // カスタマイズ:登録完了後のページ自動遷移
  248.             $redirUrl $pageBackText '';
  249.             switch ($redir) {
  250.                 // /lp/cp-202104-trial-special-set
  251.                 case 'cp-202104-trial-special-set':
  252.                     $redirUrl 'lp/cp-202104-trial-special-set#cart-box';
  253.                     $pageBackText 'キャンペーンページへ戻る';
  254.                     break;
  255.                 
  256.                 default:
  257.                     $redirUrl null;
  258.             }
  259.             return [
  260.                 'qtyInCart' => $qtyInCart,
  261.                 'redirUrl' => $redirUrl,
  262.                 'pageBackText' => $pageBackText,
  263.             ];
  264.         }
  265.         throw new HttpException\NotFoundHttpException();
  266.     }
  267. }