app/Customize/Controller/EntryController.php line 125

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