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