src/Eccube/Controller/EntryController.php line 38

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