src/Eccube/Controller/EntryController.php line 134

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 Plugin\CustomerClassPrice42\Repository\CustomerClassRepository;
  21. use Eccube\Repository\Master\CustomerStatusRepository;
  22. use Eccube\Repository\PageRepository;
  23. use Eccube\Service\CartService;
  24. use Eccube\Service\MailService;
  25. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\HttpKernel\Exception as HttpException;
  28. use Symfony\Component\Routing\Annotation\Route;
  29. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  30. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  31. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  32. use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  33. use Symfony\Component\Validator\Constraints as Assert;
  34. use Symfony\Component\Validator\Validator\ValidatorInterface;
  35. class EntryController extends AbstractController
  36. {
  37.     /**
  38.      * @var CustomerStatusRepository
  39.      */
  40.     protected $customerStatusRepository;
  41.     /**
  42.      * @var ValidatorInterface
  43.      */
  44.     protected $recursiveValidator;
  45.     /**
  46.      * @var MailService
  47.      */
  48.     protected $mailService;
  49.     /**
  50.      * @var BaseInfo
  51.      */
  52.     protected $BaseInfo;
  53.     /**
  54.      * @var CustomerRepository
  55.      */
  56.     protected $customerRepository;
  57.     /**
  58.      * @var CustomerClassRepository
  59.      */
  60.     protected $customerClassRepository;
  61.     /**
  62.      * @var EncoderFactoryInterface
  63.      */
  64.     protected $encoderFactory;
  65.     /**
  66.      * @var TokenStorageInterface
  67.      */
  68.     protected $tokenStorage;
  69.     /**
  70.      * @var \Eccube\Service\CartService
  71.      */
  72.     protected $cartService;
  73.     /**
  74.      * @var PageRepository
  75.      */
  76.     protected $pageRepository;
  77.     /**
  78.      * EntryController constructor.
  79.      *
  80.      * @param CartService $cartService
  81.      * @param CustomerStatusRepository $customerStatusRepository
  82.      * @param MailService $mailService
  83.      * @param BaseInfoRepository $baseInfoRepository
  84.      * @param CustomerRepository $customerRepository
  85.      * @param CustomerClassRepository $customerClassRepository
  86.      * @param ValidatorInterface $validatorInterface
  87.      * @param TokenStorageInterface $tokenStorage
  88.      */
  89.     public function __construct(
  90.         CartService $cartService,
  91.         CustomerStatusRepository $customerStatusRepository,
  92.         MailService $mailService,
  93.         BaseInfoRepository $baseInfoRepository,
  94.         CustomerRepository $customerRepository,
  95.         CustomerClassRepository $customerClassRepository,
  96.         EncoderFactoryInterface $encoderFactory,
  97.         ValidatorInterface $validatorInterface,
  98.         TokenStorageInterface $tokenStorage,
  99.         PageRepository $pageRepository
  100.     ) {
  101.         $this->customerStatusRepository $customerStatusRepository;
  102.         $this->mailService $mailService;
  103.         $this->BaseInfo $baseInfoRepository->get();
  104.         $this->customerRepository $customerRepository;
  105.         $this->customerClassRepository $customerClassRepository;
  106.         $this->encoderFactory $encoderFactory;
  107.         $this->recursiveValidator $validatorInterface;
  108.         $this->tokenStorage $tokenStorage;
  109.         $this->cartService $cartService;
  110.         $this->pageRepository $pageRepository;
  111.     }
  112.     /**
  113.      * 会員登録画面.
  114.      *
  115.      * @Route("/entry", name="entry", methods={"GET", "POST"})
  116.      * @Route("/entry", name="entry_confirm", methods={"GET", "POST"})
  117.      * @Template("Entry/index.twig")
  118.      */
  119.     public function index(Request $request)
  120.     {
  121.         if ($this->isGranted('ROLE_USER')) {
  122.             log_info('認証済のためログイン処理をスキップ');
  123.             return $this->redirectToRoute('mypage');
  124.         }
  125.         /** @var $Customer \Eccube\Entity\Customer */
  126.         $Customer $this->customerRepository->newCustomer();
  127.         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  128.         $builder $this->formFactory->createBuilder(EntryType::class, $Customer);
  129.         $event = new EventArgs(
  130.             [
  131.                 'builder' => $builder,
  132.                 'Customer' => $Customer,
  133.             ],
  134.             $request
  135.         );
  136.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_INDEX_INITIALIZE);
  137.         /* @var $form \Symfony\Component\Form\FormInterface */
  138.         $form $builder->getForm();
  139.         $form->handleRequest($request);
  140.         if ($form->isSubmitted() && $form->isValid()) {
  141.             switch ($request->get('mode')) {
  142.                 case 'confirm':
  143.                     log_info('会員登録確認開始');
  144.                     log_info('会員登録確認完了');
  145.                     return $this->render(
  146.                         'Entry/confirm.twig',
  147.                         [
  148.                             'form' => $form->createView(),
  149.                             'Page' => $this->pageRepository->getPageByRoute('entry_confirm'),
  150.                         ]
  151.                     );
  152.                 case 'complete':
  153.                     log_info('会員登録開始');
  154.                     $encoder $this->encoderFactory->getEncoder($Customer);
  155.                     $salt $encoder->createSalt();
  156.                     $password $encoder->encodePassword($Customer->getPlainPassword(), $salt);
  157.                     $secretKey $this->customerRepository->getUniqueSecretKey();
  158.                     $CustomerClass $this->customerClassRepository->find(1);
  159.                     $Customer
  160.                         ->setSalt($salt)
  161.                         ->setPassword($password)
  162.                         ->setSecretKey($secretKey);
  163.                     if (method_exists($Customer'setPlgCcpCustomerClass')) {
  164.                         $Customer->setPlgCcpCustomerClass($CustomerClass);
  165.                     }
  166.                     $Customer->setPoint(0);
  167.                     $this->entityManager->persist($Customer);
  168.                     $this->entityManager->flush();
  169.                     log_info('会員登録完了');
  170.                     $event = new EventArgs(
  171.                         [
  172.                             'form' => $form,
  173.                             'Customer' => $Customer,
  174.                         ],
  175.                         $request
  176.                     );
  177.                     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_INDEX_COMPLETE);
  178.                     $activateFlg $this->BaseInfo->isOptionCustomerActivate();
  179.                     // 仮会員設定が有効な場合は、確認メールを送信し完了画面表示.
  180.                     if ($activateFlg) {
  181.                         $activateUrl $this->generateUrl('entry_activate', ['secret_key' => $Customer->getSecretKey()], UrlGeneratorInterface::ABSOLUTE_URL);
  182.                         // メール送信
  183.                         $this->mailService->sendCustomerConfirmMail($Customer$activateUrl);
  184.                         if ($event->hasResponse()) {
  185.                             return $event->getResponse();
  186.                         }
  187.                         log_info('仮会員登録完了画面へリダイレクト');
  188.                         return $this->redirectToRoute('entry_complete');
  189.                     } else {
  190.                         // 仮会員設定が無効な場合は、会員登録を完了させる.
  191.                         $qtyInCart $this->entryActivate($request$Customer->getSecretKey());
  192.                         // URLを変更するため完了画面にリダイレクト
  193.                         return $this->redirectToRoute('entry_activate', [
  194.                             'secret_key' => $Customer->getSecretKey(),
  195.                             'qtyInCart' => $qtyInCart,
  196.                         ]);
  197.                     }
  198.             }
  199.         }
  200.         return [
  201.             'form' => $form->createView(),
  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.         return [];
  213.     }
  214.     /**
  215.      * 会員のアクティベート(本会員化)を行う.
  216.      *
  217.      * @Route("/entry/activate/{secret_key}/{qtyInCart}", name="entry_activate", methods={"GET"})
  218.      * @Template("Entry/activate.twig")
  219.      */
  220.     public function activate(Request $request$secret_key$qtyInCart null)
  221.     {
  222.         $errors $this->recursiveValidator->validate(
  223.             $secret_key,
  224.             [
  225.                 new Assert\NotBlank(),
  226.                 new Assert\Regex(
  227.                     [
  228.                         'pattern' => '/^[a-zA-Z0-9]+$/',
  229.                     ]
  230.                 ),
  231.             ]
  232.         );
  233.         if (!$this->session->has('eccube.login.target.path')) {
  234.             $this->setLoginTargetPath($this->generateUrl('mypage', [], UrlGeneratorInterface::ABSOLUTE_URL));
  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.         if ($qtyInCart) {
  285.             $this->cartService->save();
  286.         }
  287.         return $qtyInCart;
  288.     }
  289. }