app/Customize/Controller/CustomEntryController.php line 146

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 Customize\Repository\CustomerInvitationHistoryRepository;
  14. use Customize\Service\CustomMailService;
  15. // use Eccube\Common\Constant;
  16. use Eccube\Controller\AbstractController;
  17. use Eccube\Entity\BaseInfo;
  18. use Eccube\Entity\Master\CustomerStatus;
  19. use Eccube\Event\EccubeEvents;
  20. use Eccube\Event\EventArgs;
  21. use Eccube\Form\Type\Front\EntryType;
  22. use Eccube\Repository\BaseInfoRepository;
  23. use Eccube\Repository\CustomerRepository;
  24. use Eccube\Repository\Master\CustomerStatusRepository;
  25. use Eccube\Repository\PageRepository;
  26. use Eccube\Service\CartService;
  27. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  28. use Symfony\Component\HttpFoundation\Request;
  29. use Symfony\Component\HttpKernel\Exception as HttpException;
  30. use Symfony\Component\Routing\Annotation\Route;
  31. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  32. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  33. use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  34. use Symfony\Component\Validator\Constraints as Assert;
  35. use Symfony\Component\Validator\Validator\ValidatorInterface;
  36. // use Plugin\Coupon42\Repository\CouponRepository;
  37. // use Plugin\Coupon42\Service\CouponService;
  38. // use Plugin\Coupon42\Entity\Coupon;
  39. class CustomEntryController extends AbstractController
  40. {
  41.     /**
  42.      * @var CustomerStatusRepository
  43.      */
  44.     protected $customerStatusRepository;
  45.     /**
  46.      * @var ValidatorInterface
  47.      */
  48.     protected $recursiveValidator;
  49.     /**
  50.      * @var CustomMailService
  51.      */
  52.     protected $mailService;
  53.     /**
  54.      * @var BaseInfo
  55.      */
  56.     protected $BaseInfo;
  57.     /**
  58.      * @var CustomerRepository
  59.      */
  60.     protected $customerRepository;
  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.      * @var CustomerInvitationHistoryRepository
  79.      */
  80.     protected $customerInvitationHistoryRepository;
  81.     // protected $couponRepository;
  82.     // protected $couponService;
  83.     /**
  84.      * EntryController constructor.
  85.      *
  86.      * @param CartService $cartService
  87.      * @param CustomerStatusRepository $customerStatusRepository
  88.      * @param MailService $mailService
  89.      * @param BaseInfoRepository $baseInfoRepository
  90.      * @param CustomerRepository $customerRepository
  91.      * @param EncoderFactoryInterface $encoderFactory
  92.      * @param ValidatorInterface $validatorInterface
  93.      * @param TokenStorageInterface $tokenStorage
  94.      */
  95.     public function __construct(
  96.         CartService $cartService,
  97.         CustomerStatusRepository $customerStatusRepository,
  98.         CustomMailService $mailService,
  99.         BaseInfoRepository $baseInfoRepository,
  100.         CustomerRepository $customerRepository,
  101.         EncoderFactoryInterface $encoderFactory,
  102.         ValidatorInterface $validatorInterface,
  103.         TokenStorageInterface $tokenStorage,
  104.         PageRepository $pageRepository,
  105.         CustomerInvitationHistoryRepository $customerInvitationHistoryRepository,
  106.         // CouponRepository $couponRepository,
  107.         // CouponService $couponService
  108.     ) {
  109.         $this->customerStatusRepository $customerStatusRepository;
  110.         $this->mailService $mailService;
  111.         $this->BaseInfo $baseInfoRepository->get();
  112.         $this->customerRepository $customerRepository;
  113.         $this->encoderFactory $encoderFactory;
  114.         $this->recursiveValidator $validatorInterface;
  115.         $this->tokenStorage $tokenStorage;
  116.         $this->cartService $cartService;
  117.         $this->pageRepository $pageRepository;
  118.         $this->customerInvitationHistoryRepository $customerInvitationHistoryRepository;
  119.         // $this->couponRepository = $couponRepository;
  120.         // $this->couponService = $couponService;
  121.     }
  122.     /**
  123.      * 会員登録画面.
  124.      *
  125.      * @Route("/entry", name="entry", methods={"GET", "POST"})
  126.      * @Route("/entry", name="entry_confirm", methods={"GET", "POST"})
  127.      * @Template("Entry/index.twig")
  128.      */
  129.     public function index(Request $request)
  130.     {
  131.         if ($this->isGranted('ROLE_USER')) {
  132.             log_info('認証済のためログイン処理をスキップ');
  133.             return $this->redirectToRoute('mypage');
  134.         }
  135.         /** @var $Customer \Eccube\Entity\Customer */
  136.         $Customer $this->customerRepository->newCustomer();
  137.         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  138.         $builder $this->formFactory->createBuilder(EntryType::class, $Customer);
  139.         $event = new EventArgs(
  140.             [
  141.                 'builder' => $builder,
  142.                 'Customer' => $Customer,
  143.             ],
  144.             $request
  145.         );
  146.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_INDEX_INITIALIZE);
  147.         /* @var $form \Symfony\Component\Form\FormInterface */
  148.         $form $builder->getForm();
  149.         $form->handleRequest($request);
  150.         if ($form->isSubmitted() && $form->isValid()) {
  151.             switch ($request->get('mode')) {
  152.                 case 'confirm':
  153.                     log_info('会員登録確認開始');
  154.                     log_info('会員登録確認完了');
  155.                     return $this->render(
  156.                         'Entry/confirm.twig',
  157.                         [
  158.                             'form' => $form->createView(),
  159.                             'Page' => $this->pageRepository->getPageByRoute('entry_confirm'),
  160.                         ]
  161.                     );
  162.                 case 'complete':
  163.                     log_info('会員登録開始');
  164.                     $encoder $this->encoderFactory->getEncoder($Customer);
  165.                     $salt $encoder->createSalt();
  166.                     $password $encoder->encodePassword($Customer->getPlainPassword(), $salt);
  167.                     $secretKey $this->customerRepository->getUniqueSecretKey();
  168.                     $Customer
  169.                         ->setSalt($salt)
  170.                         ->setPassword($password)
  171.                         ->setSecretKey($secretKey)
  172.                         ->setPoint(0);
  173.                     $this->entityManager->persist($Customer);
  174.                     $this->entityManager->flush();
  175.                     log_info('会員登録完了');
  176.                     $event = new EventArgs(
  177.                         [
  178.                             'form' => $form,
  179.                             'Customer' => $Customer,
  180.                         ],
  181.                         $request
  182.                     );
  183.                     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_INDEX_COMPLETE);
  184.                     $activateFlg $this->BaseInfo->isOptionCustomerActivate();
  185.                     // 仮会員設定が有効な場合は、確認メールを送信し完了画面表示.
  186.                     if ($activateFlg) {
  187.                         $activateUrl $this->generateUrl('entry_activate', ['secret_key' => $Customer->getSecretKey()], UrlGeneratorInterface::ABSOLUTE_URL);
  188.                         // 招待コード利用済みに更新
  189.                         $code $form->get('invitation_code')->getData();
  190.                         if (!empty($code)) {
  191.                             $Invitation $this->customerInvitationHistoryRepository->getInvitationByCode($code);
  192.                             $Invitation->setGuestCustomerId($Customer->getId());
  193.                             $this->entityManager->persist($Invitation);
  194.                             $this->entityManager->flush();
  195.                             // 招待元ユーザにクーポンを付与 (一旦無効化)
  196.                             // $this->activateCoupon($Invitation);
  197.                         }
  198.                         // メール送信
  199.                         $this->mailService->sendCustomerConfirmMail($Customer$activateUrl);
  200.                         if ($event->hasResponse()) {
  201.                             return $event->getResponse();
  202.                         }
  203.                         log_info('仮会員登録完了画面へリダイレクト');
  204.                         return $this->redirectToRoute('entry_complete');
  205.                     } else {
  206.                         // 仮会員設定が無効な場合は、会員登録を完了させる.
  207.                         $qtyInCart $this->entryActivate($request$Customer->getSecretKey());
  208.                         // URLを変更するため完了画面にリダイレクト
  209.                         return $this->redirectToRoute('entry_activate', [
  210.                             'secret_key' => $Customer->getSecretKey(),
  211.                             'qtyInCart' => $qtyInCart,
  212.                         ]);
  213.                     }
  214.             }
  215.         }
  216.         return [
  217.             'form' => $form->createView(),
  218.         ];
  219.     }
  220.     /**
  221.      * 会員登録完了画面.
  222.      *
  223.      * @Route("/entry/complete", name="entry_complete", methods={"GET"})
  224.      * @Template("Entry/complete.twig")
  225.      */
  226.     public function complete()
  227.     {
  228.         return [];
  229.     }
  230.     /**
  231.      * 会員のアクティベート(本会員化)を行う.
  232.      *
  233.      * @Route("/entry/activate/{secret_key}/{qtyInCart}", name="entry_activate", methods={"GET"})
  234.      * @Template("Entry/activate.twig")
  235.      */
  236.     public function activate(Request $request$secret_key$qtyInCart null)
  237.     {
  238.         $errors $this->recursiveValidator->validate(
  239.             $secret_key,
  240.             [
  241.                 new Assert\NotBlank(),
  242.                 new Assert\Regex(
  243.                     [
  244.                         'pattern' => '/^[a-zA-Z0-9]+$/',
  245.                     ]
  246.                 ),
  247.             ]
  248.         );
  249.         if (!$this->session->has('eccube.login.target.path')) {
  250.             $this->setLoginTargetPath($this->generateUrl('mypage', [], UrlGeneratorInterface::ABSOLUTE_URL));
  251.         }
  252.         if (!is_null($qtyInCart)) {
  253.             return [
  254.                 'qtyInCart' => $qtyInCart,
  255.             ];
  256.         } elseif ($request->getMethod() === 'GET' && count($errors) === 0) {
  257.             // 会員登録処理を行う
  258.             $qtyInCart $this->entryActivate($request$secret_key);
  259.             return [
  260.                 'qtyInCart' => $qtyInCart,
  261.             ];
  262.         }
  263.         throw new HttpException\NotFoundHttpException();
  264.     }
  265.     /**
  266.      * 会員登録処理を行う
  267.      *
  268.      * @param Request $request
  269.      * @param $secret_key
  270.      *
  271.      * @return \Eccube\Entity\Cart|mixed
  272.      */
  273.     private function entryActivate(Request $request$secret_key)
  274.     {
  275.         log_info('本会員登録開始');
  276.         $Customer $this->customerRepository->getProvisionalCustomerBySecretKey($secret_key);
  277.         if (is_null($Customer)) {
  278.             return $this->redirectToRoute('homepage');
  279.         }
  280.         $CustomerStatus $this->customerStatusRepository->find(CustomerStatus::REGULAR);
  281.         $Customer->setStatus($CustomerStatus);
  282.         $this->entityManager->persist($Customer);
  283.         $this->entityManager->flush();
  284.         log_info('本会員登録完了');
  285.         $event = new EventArgs(
  286.             [
  287.                 'Customer' => $Customer,
  288.             ],
  289.             $request
  290.         );
  291.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_ACTIVATE_COMPLETE);
  292.         // 招待コード作成
  293.         $code $this->customerInvitationHistoryRepository->createInvitations($Customer);
  294.         // メール送信
  295.         $this->mailService->sendCustomerCompleteMail($Customer$code);
  296.         // Assign session carts into customer carts
  297.         $Carts $this->cartService->getCarts();
  298.         $qtyInCart 0;
  299.         foreach ($Carts as $Cart) {
  300.             $qtyInCart += $Cart->getTotalQuantity();
  301.         }
  302.         if ($qtyInCart) {
  303.             $this->cartService->save();
  304.         }
  305.         return $qtyInCart;
  306.     }
  307.     /*
  308.     private function activateCoupon($Invitation)
  309.     {
  310.         $Coupon = new Coupon();
  311.         $Coupon->setEnableFlag(Constant::ENABLED);
  312.         $Coupon->setVisible(true);
  313.         $couponCd = $this->couponService->generateCouponCd();
  314.         $Coupon->setCouponCd($couponCd);
  315.         $Coupon->setCouponType(Coupon::ALL);
  316.         $Coupon->setDiscountType(Coupon::DISCOUNT_RATE);
  317.         $Coupon->setDiscountRate(5);
  318.         $Coupon->setCouponName('招待者クーポン');
  319.         $Coupon->setCouponUseTime(1);
  320.         $Coupon->setCouponRelease(1);
  321.         $Coupon->setCouponMember(0);
  322.         $Coupon->setAvailableFromDate(new \DateTime('-1 year'));
  323.         $Coupon->setAvailableToDate(new \DateTime('+100 year'));
  324.         $this->entityManager->persist($Coupon);
  325.         $this->entityManager->flush();
  326.         $hostCustomer = $this->customerRepository->find($Invitation->getHostCustomerId());
  327.         $this->mailService->sendCouponMail($hostCustomer, $couponCd);
  328.     }
  329.     */
  330. }