src/Eccube/Controller/EntryController.php line 117

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 Eccube\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. class EntryController extends AbstractController
  34. {
  35.     /**
  36.      * @var CustomerStatusRepository
  37.      */
  38.     protected $customerStatusRepository;
  39.     /**
  40.      * @var ValidatorInterface
  41.      */
  42.     protected $recursiveValidator;
  43.     /**
  44.      * @var MailService
  45.      */
  46.     protected $mailService;
  47.     /**
  48.      * @var BaseInfo
  49.      */
  50.     protected $BaseInfo;
  51.     /**
  52.      * @var CustomerRepository
  53.      */
  54.     protected $customerRepository;
  55.     /**
  56.      * @var EncoderFactoryInterface
  57.      */
  58.     protected $encoderFactory;
  59.     /**
  60.      * @var TokenStorageInterface
  61.      */
  62.     protected $tokenStorage;
  63.     /**
  64.      * @var \Eccube\Service\CartService
  65.      */
  66.     protected $cartService;
  67.     /**
  68.      * EntryController constructor.
  69.      *
  70.      * @param CartService $cartService
  71.      * @param CustomerStatusRepository $customerStatusRepository
  72.      * @param MailService $mailService
  73.      * @param BaseInfoRepository $baseInfoRepository
  74.      * @param CustomerRepository $customerRepository
  75.      * @param EncoderFactoryInterface $encoderFactory
  76.      * @param ValidatorInterface $validatorInterface
  77.      * @param TokenStorageInterface $tokenStorage
  78.      */
  79.     public function __construct(
  80.         CartService $cartService,
  81.         CustomerStatusRepository $customerStatusRepository,
  82.         MailService $mailService,
  83.         BaseInfoRepository $baseInfoRepository,
  84.         CustomerRepository $customerRepository,
  85.         EncoderFactoryInterface $encoderFactory,
  86.         ValidatorInterface $validatorInterface,
  87.         TokenStorageInterface $tokenStorage
  88.     ) {
  89.         $this->customerStatusRepository $customerStatusRepository;
  90.         $this->mailService $mailService;
  91.         $this->BaseInfo $baseInfoRepository->get();
  92.         $this->customerRepository $customerRepository;
  93.         $this->encoderFactory $encoderFactory;
  94.         $this->recursiveValidator $validatorInterface;
  95.         $this->tokenStorage $tokenStorage;
  96.         $this->cartService $cartService;
  97.     }
  98.     /**
  99.      * 会員登録画面.
  100.      *
  101.      * @Route("/entry", name="entry")
  102.      * @Template("Entry/index.twig")
  103.      */
  104.     public function index(Request $request)
  105.     {
  106.         if ($this->isGranted('ROLE_USER')) {
  107.             log_info('認証済のためログイン処理をスキップ');
  108.             return $this->redirectToRoute('mypage');
  109.         }
  110.         /** @var $Customer \Eccube\Entity\Customer */
  111.         $Customer $this->customerRepository->newCustomer();
  112.         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  113.         $builder $this->formFactory->createBuilder(EntryType::class, $Customer);
  114.         $event = new EventArgs(
  115.             [
  116.                 'builder' => $builder,
  117.                 'Customer' => $Customer,
  118.             ],
  119.             $request
  120.         );
  121.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_INDEX_INITIALIZE$event);
  122.         /* @var $form \Symfony\Component\Form\FormInterface */
  123.         $form $builder->getForm();
  124.         $form->handleRequest($request);
  125.         if ($form->isSubmitted() && $form->isValid()) {
  126.             switch ($request->get('mode')) {
  127.                 case 'confirm':
  128.                     log_info('会員登録確認開始');
  129.                     log_info('会員登録確認完了');
  130.                     return $this->render(
  131.                         'Entry/confirm.twig',
  132.                         [
  133.                             'form' => $form->createView(),
  134.                         ]
  135.                     );
  136.                 case 'complete':
  137.                     log_info('会員登録開始');
  138.                     $encoder $this->encoderFactory->getEncoder($Customer);
  139.                     $salt $encoder->createSalt();
  140.                     $password $encoder->encodePassword($Customer->getPassword(), $salt);
  141.                     $secretKey $this->customerRepository->getUniqueSecretKey();
  142.                     $Customer
  143.                         ->setSalt($salt)
  144.                         ->setPassword($password)
  145.                         ->setSecretKey($secretKey)
  146.                         ->setPoint(0);
  147.                     $this->entityManager->persist($Customer);
  148.                     $this->entityManager->flush();
  149.                     
  150.                     log_info('会員登録完了');
  151.                     $event = new EventArgs(
  152.                         [
  153.                             'form' => $form,
  154.                             'Customer' => $Customer,
  155.                         ],
  156.                         $request
  157.                     );
  158.                     $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_INDEX_COMPLETE$event);
  159.                     
  160.                     $activateFlg $this->BaseInfo->isOptionCustomerActivate();
  161.                     
  162.                     // 仮会員設定が有効な場合は、確認メールを送信し完了画面表示.
  163.                     if ($activateFlg) {
  164.                         $activateUrl $this->generateUrl('entry_activate', ['secret_key' => $Customer->getSecretKey()], UrlGeneratorInterface::ABSOLUTE_URL);
  165.                         // メール送信
  166.                         $this->mailService->sendCustomerConfirmMail($Customer$activateUrl);
  167.                         if ($event->hasResponse()) {
  168.                             return $event->getResponse();
  169.                         }
  170.                         log_info('仮会員登録完了画面へリダイレクト');
  171.                         return $this->redirectToRoute('entry_complete');
  172.                     } else {
  173.                         // 仮会員設定が無効な場合は、会員登録を完了させる.
  174.                         $qtyInCart $this->entryActivate($request$Customer->getSecretKey());
  175.                         // URLを変更するため完了画面にリダイレクト
  176.                         return $this->redirectToRoute('entry_activate', [
  177.                             'secret_key' => $Customer->getSecretKey(),
  178.                             'qtyInCart' => $qtyInCart,
  179.                         ]);
  180.                     }
  181.             }
  182.         }
  183.         return [
  184.             'form' => $form->createView(),
  185.         ];
  186.     }
  187.     /**
  188.      * 会員登録完了画面.
  189.      *
  190.      * @Route("/entry/complete", name="entry_complete")
  191.      * @Template("Entry/complete.twig")
  192.      */
  193.     public function complete()
  194.     {
  195.         return [];
  196.     }
  197.     /**
  198.      * 会員のアクティベート(本会員化)を行う.
  199.      *
  200.      * @Route("/entry/activate/{secret_key}/{qtyInCart}", name="entry_activate")
  201.      * @Template("Entry/activate.twig")
  202.      */
  203.     public function activate(Request $request$secret_key$qtyInCart null)
  204.     {
  205.         $errors $this->recursiveValidator->validate(
  206.             $secret_key,
  207.             [
  208.                 new Assert\NotBlank(),
  209.                 new Assert\Regex(
  210.                     [
  211.                         'pattern' => '/^[a-zA-Z0-9]+$/',
  212.                     ]
  213.                 ),
  214.             ]
  215.         );
  216.         if(!is_null($qtyInCart)) {
  217.             return [
  218.                 'qtyInCart' => $qtyInCart,
  219.             ];
  220.         } elseif ($request->getMethod() === 'GET' && count($errors) === 0) {
  221.             // 会員登録処理を行う
  222.             $qtyInCart $this->entryActivate($request$secret_key);
  223.             return [
  224.                 'qtyInCart' => $qtyInCart,
  225.             ];
  226.         }
  227.         throw new HttpException\NotFoundHttpException();
  228.     }
  229.     /**
  230.      * 会員登録処理を行う
  231.      *
  232.      * @param Request $request
  233.      * @param $secret_key
  234.      * @return \Eccube\Entity\Cart|mixed
  235.      */
  236.     private function entryActivate(Request $request$secret_key)
  237.     {
  238.         log_info('本会員登録開始');
  239.         $Customer $this->customerRepository->getProvisionalCustomerBySecretKey($secret_key);
  240.         if (is_null($Customer)) {
  241.             throw new HttpException\NotFoundHttpException();
  242.         }
  243.         $CustomerStatus $this->customerStatusRepository->find(CustomerStatus::REGULAR);
  244.         $Customer->setStatus($CustomerStatus);
  245.         $this->entityManager->persist($Customer);
  246.         $this->entityManager->flush();
  247.         
  248.         log_info('本会員登録完了');
  249.         $event = new EventArgs(
  250.             [
  251.                 'Customer' => $Customer,
  252.             ],
  253.             $request
  254.         );
  255.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_ACTIVATE_COMPLETE$event);
  256.         // メール送信
  257.         $this->mailService->sendCustomerCompleteMail($Customer);
  258.         // Assign session carts into customer carts
  259.         $Carts $this->cartService->getCarts();
  260.         $qtyInCart 0;
  261.         foreach ($Carts as $Cart) {
  262.             $qtyInCart += $Cart->getTotalQuantity();
  263.         }
  264.         // 本会員登録してログイン状態にする
  265.         $token = new UsernamePasswordToken($Customernull'customer', ['ROLE_USER']);
  266.         $this->tokenStorage->setToken($token);
  267.         $request->getSession()->migrate(true);
  268.         if ($qtyInCart) {
  269.             $this->cartService->save();
  270.         }
  271.         log_info('ログイン済に変更', [$this->getUser()->getId()]);
  272.         return $qtyInCart;
  273.     }
  274. }