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