app/Customize/Controller/EntryController.php line 235

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