src/Eccube/Controller/EntryController.php line 126

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.                         // プラザからの登録の場合、URLにプラザフラグを追加
  172.                         $layout $this->session->get('header_footer_layout''palace');
  173.                         if ($layout === 'plaza') {
  174.                             $activateUrl .= '?plaza=1';
  175.                         }
  176.                         // メール送信
  177.                         $this->mailService->sendCustomerConfirmMail($Customer$activateUrl);
  178.                         if ($event->hasResponse()) {
  179.                             return $event->getResponse();
  180.                         }
  181.                         log_info('仮会員登録完了画面へリダイレクト');
  182.                         return $this->redirectToRoute('entry_complete');
  183.                     } else {
  184.                         // 仮会員設定が無効な場合は、会員登録を完了させる.
  185.                         $qtyInCart $this->entryActivate($request$Customer->getSecretKey());
  186.                         // URLを変更するため完了画面にリダイレクト
  187.                         return $this->redirectToRoute('entry_activate', [
  188.                             'secret_key' => $Customer->getSecretKey(),
  189.                             'qtyInCart' => $qtyInCart,
  190.                         ]);
  191.                     }
  192.             }
  193.         }
  194.         return [
  195.             'form' => $form->createView(),
  196.         ];
  197.     }
  198.     /**
  199.      * 会員登録完了画面.
  200.      *
  201.      * @Route("/entry/complete", name="entry_complete", methods={"GET"})
  202.      * @Template("Entry/complete.twig")
  203.      */
  204.     public function complete()
  205.     {
  206.         return [];
  207.     }
  208.     /**
  209.      * 会員のアクティベート(本会員化)を行う.
  210.      *
  211.      * @Route("/entry/activate/{secret_key}/{qtyInCart}", name="entry_activate", methods={"GET"})
  212.      * @Template("Entry/activate.twig")
  213.      */
  214.     public function activate(Request $request$secret_key$qtyInCart null)
  215.     {
  216.         $errors $this->recursiveValidator->validate(
  217.             $secret_key,
  218.             [
  219.                 new Assert\NotBlank(),
  220.                 new Assert\Regex(
  221.                     [
  222.                         'pattern' => '/^[a-zA-Z0-9]+$/',
  223.                     ]
  224.                 ),
  225.             ]
  226.         );
  227.         // プラザフラグがURLにある場合、セッションにセット
  228.         if ($request->query->get('plaza') == '1') {
  229.             $this->session->set('header_footer_layout''plaza');
  230.             log_info('プラザフラグをセッションにセット');
  231.         }
  232.         if (!$this->session->has('eccube.login.target.path')) {
  233.             $this->setLoginTargetPath($this->generateUrl('mypage', [], UrlGeneratorInterface::ABSOLUTE_URL));
  234.         }
  235.         if (!is_null($qtyInCart)) {
  236.             return [
  237.                 'qtyInCart' => $qtyInCart,
  238.             ];
  239.         } elseif ($request->getMethod() === 'GET' && count($errors) === 0) {
  240.             // 会員登録処理を行う
  241.             $qtyInCart $this->entryActivate($request$secret_key);
  242.             return [
  243.                 'qtyInCart' => $qtyInCart,
  244.             ];
  245.         }
  246.         throw new HttpException\NotFoundHttpException();
  247.     }
  248.     /**
  249.      * 会員登録処理を行う
  250.      *
  251.      * @param Request $request
  252.      * @param $secret_key
  253.      *
  254.      * @return \Eccube\Entity\Cart|mixed
  255.      */
  256.     private function entryActivate(Request $request$secret_key)
  257.     {
  258.         log_info('本会員登録開始');
  259.         $Customer $this->customerRepository->getProvisionalCustomerBySecretKey($secret_key);
  260.         if (is_null($Customer)) {
  261.             throw new HttpException\NotFoundHttpException();
  262.         }
  263.         $CustomerStatus $this->customerStatusRepository->find(CustomerStatus::REGULAR);
  264.         $Customer->setStatus($CustomerStatus);
  265.         $this->entityManager->persist($Customer);
  266.         $this->entityManager->flush();
  267.         log_info('本会員登録完了');
  268.         $event = new EventArgs(
  269.             [
  270.                 'Customer' => $Customer,
  271.             ],
  272.             $request
  273.         );
  274.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_ACTIVATE_COMPLETE);
  275.         // メール送信
  276.         $this->mailService->sendCustomerCompleteMail($Customer);
  277.         // Assign session carts into customer carts
  278.         $Carts $this->cartService->getCarts();
  279.         $qtyInCart 0;
  280.         foreach ($Carts as $Cart) {
  281.             $qtyInCart += $Cart->getTotalQuantity();
  282.         }
  283.         if ($qtyInCart) {
  284.             $this->cartService->save();
  285.         }
  286.         return $qtyInCart;
  287.     }
  288. }