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.         $Customer->setSex($this->entityManager->getRepository('Eccube\Entity\Master\Sex')->find(2));
  121.         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  122.         $builder $this->formFactory->createBuilder(EntryType::class, $Customer);
  123.         $event = new EventArgs(
  124.             [
  125.                 'builder' => $builder,
  126.                 'Customer' => $Customer,
  127.             ],
  128.             $request
  129.         );
  130.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_INDEX_INITIALIZE);
  131.         /* @var $form \Symfony\Component\Form\FormInterface */
  132.         $form $builder->getForm();
  133.         $form->handleRequest($request);
  134.         if ($form->isSubmitted() && $form->isValid()) {
  135.             switch ($request->get('mode')) {
  136.                 case 'confirm':
  137.                     log_info('会員登録確認開始');
  138.                     log_info('会員登録確認完了');
  139.                     return $this->render(
  140.                         'Entry/confirm.twig',
  141.                         [
  142.                             'form' => $form->createView(),
  143.                             'Page' => $this->pageRepository->getPageByRoute('entry_confirm'),
  144.                         ]
  145.                     );
  146.                 case 'complete':
  147.                     log_info('会員登録開始');
  148.                     $encoder $this->encoderFactory->getEncoder($Customer);
  149.                     $salt $encoder->createSalt();
  150.                     $password $encoder->encodePassword($Customer->getPlainPassword(), $salt);
  151.                     $secretKey $this->customerRepository->getUniqueSecretKey();
  152.                     $Customer
  153.                         ->setSalt($salt)
  154.                         ->setPassword($password)
  155.                         ->setSecretKey($secretKey)
  156.                         ->setPoint(0);
  157.                     $this->entityManager->persist($Customer);
  158.                     $this->entityManager->flush();
  159.                     log_info('会員登録完了');
  160.                     $event = new EventArgs(
  161.                         [
  162.                             'form' => $form,
  163.                             'Customer' => $Customer,
  164.                         ],
  165.                         $request
  166.                     );
  167.                     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_INDEX_COMPLETE);
  168.                     $activateFlg $this->BaseInfo->isOptionCustomerActivate();
  169.                     // 仮会員設定が有効な場合は、確認メールを送信し完了画面表示.
  170.                     if ($activateFlg) {
  171.                         $activateUrl $this->generateUrl('entry_activate', ['secret_key' => $Customer->getSecretKey()], UrlGeneratorInterface::ABSOLUTE_URL);
  172.                         // メール送信
  173.                         $this->mailService->sendCustomerConfirmMail($Customer$activateUrl);
  174.                         if ($event->hasResponse()) {
  175.                             return $event->getResponse();
  176.                         }
  177.                         log_info('仮会員登録完了画面へリダイレクト');
  178.                         return $this->redirectToRoute('entry_complete');
  179.                     } else {
  180.                         // 仮会員設定が無効な場合は、会員登録を完了させる.
  181.                         $qtyInCart $this->entryActivate($request$Customer->getSecretKey());
  182.                         // URLを変更するため完了画面にリダイレクト
  183.                         return $this->redirectToRoute('entry_activate', [
  184.                             'secret_key' => $Customer->getSecretKey(),
  185.                             'qtyInCart' => $qtyInCart,
  186.                         ]);
  187.                     }
  188.             }
  189.         }
  190.         return [
  191.             'form' => $form->createView(),
  192.         ];
  193.     }
  194.     /**
  195.      * 会員登録完了画面.
  196.      *
  197.      * @Route("/entry/complete", name="entry_complete", methods={"GET"})
  198.      * @Template("Entry/complete.twig")
  199.      */
  200.     public function complete()
  201.     {
  202.         return [];
  203.     }
  204.     /**
  205.      * 会員のアクティベート(本会員化)を行う.
  206.      *
  207.      * @Route("/entry/activate/{secret_key}/{qtyInCart}", name="entry_activate", methods={"GET"})
  208.      * @Template("Entry/activate.twig")
  209.      */
  210.     public function activate(Request $request$secret_key$qtyInCart null)
  211.     {
  212.         $errors $this->recursiveValidator->validate(
  213.             $secret_key,
  214.             [
  215.                 new Assert\NotBlank(),
  216.                 new Assert\Regex(
  217.                     [
  218.                         'pattern' => '/^[a-zA-Z0-9]+$/',
  219.                     ]
  220.                 ),
  221.             ]
  222.         );
  223.         if (!$this->session->has('eccube.login.target.path')) {
  224.             $this->setLoginTargetPath($this->generateUrl('mypage', [], UrlGeneratorInterface::ABSOLUTE_URL));
  225.         }
  226.         if (!is_null($qtyInCart)) {
  227.             return [
  228.                 'qtyInCart' => $qtyInCart,
  229.             ];
  230.         } elseif ($request->getMethod() === 'GET' && count($errors) === 0) {
  231.             // 会員登録処理を行う
  232.             $qtyInCart $this->entryActivate($request$secret_key);
  233.             return [
  234.                 'qtyInCart' => $qtyInCart,
  235.             ];
  236.         }
  237.         throw new HttpException\NotFoundHttpException();
  238.     }
  239.     /**
  240.      * 会員登録処理を行う
  241.      *
  242.      * @param Request $request
  243.      * @param $secret_key
  244.      *
  245.      * @return \Eccube\Entity\Cart|mixed
  246.      */
  247.     private function entryActivate(Request $request$secret_key)
  248.     {
  249.         log_info('本会員登録開始');
  250.         $Customer $this->customerRepository->getProvisionalCustomerBySecretKey($secret_key);
  251.         if (is_null($Customer)) {
  252.             throw new HttpException\NotFoundHttpException();
  253.         }
  254.         $CustomerStatus $this->customerStatusRepository->find(CustomerStatus::REGULAR);
  255.         $Customer->setStatus($CustomerStatus);
  256.         $this->entityManager->persist($Customer);
  257.         $this->entityManager->flush();
  258.         log_info('本会員登録完了');
  259.         $event = new EventArgs(
  260.             [
  261.                 'Customer' => $Customer,
  262.             ],
  263.             $request
  264.         );
  265.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_ACTIVATE_COMPLETE);
  266.         // メール送信
  267.         $this->mailService->sendCustomerCompleteMail($Customer);
  268.         // Assign session carts into customer carts
  269.         $Carts $this->cartService->getCarts();
  270.         $qtyInCart 0;
  271.         foreach ($Carts as $Cart) {
  272.             $qtyInCart += $Cart->getTotalQuantity();
  273.         }
  274.         if ($qtyInCart) {
  275.             $this->cartService->save();
  276.         }
  277.         return $qtyInCart;
  278.     }
  279. }