- <?php
- namespace Customize\Controller;
- use Eccube\Controller\EntryController as ControllerEntryController;
- use Eccube\Entity\Customer;
- use Eccube\Event\EccubeEvents;
- use Eccube\Event\EventArgs;
- use Eccube\Form\Type\Front\EntryType;
- use Eccube\Repository\BaseInfoRepository;
- use Eccube\Repository\CustomerRepository;
- use Eccube\Repository\Master\CustomerStatusRepository;
- use Eccube\Service\CartService;
- use Eccube\Service\MailService;
- use Symfony\Component\HttpFoundation\File\UploadedFile;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
- use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
- use Symfony\Component\Validator\Validator\ValidatorInterface;
- use Eccube\Repository\PageRepository;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Component\Routing\Annotation\Route;
- use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
- class EntryController extends ControllerEntryController{
-     
-     /** @var string */
-     protected $customerImageDir;
-     /** @var string */
-     protected $licenseImageDir;
-     /** @var string */
-     protected $tempDir;
-     /**
-      * EntryController constructor.
-      * @param CartService $cartService
-      * @param CustomerStatusRepository $customerStatusRepository
-      * @param MailService $mailService
-      * @param BaseInfoRepository $baseInfoRepository
-      * @param CustomerRepository $customerRepository
-      * @param EncoderFactoryInterface $encoderFactory
-      * @param ValidatorInterface $validatorInterface
-      * @param TokenStorageInterface $tokenStorage
-      */
-     public function __construct(
-         CartService $cartService,
-         CustomerStatusRepository $customerStatusRepository,
-         MailService $mailService,
-         BaseInfoRepository $baseInfoRepository,
-         CustomerRepository $customerRepository,
-         EncoderFactoryInterface $encoderFactory,
-         ValidatorInterface $validatorInterface,
-         TokenStorageInterface $tokenStorage,
-         PageRepository $pageRepository,
-         ContainerInterface $container
-     ) {
-         parent::__construct(
-             $cartService,
-             $customerStatusRepository,
-             $mailService,
-             $baseInfoRepository,
-             $customerRepository,
-             $encoderFactory,
-             $validatorInterface,
-             $tokenStorage,
-             $pageRepository
-         );
-         $projectDir = $container->getParameter('kernel.project_dir');
-         $this->customerImageDir = $projectDir . '/html/upload/customer_image';
-         $this->licenseImageDir = $projectDir . '/html/upload/license_image';
-         $this->tempDir = $projectDir . '/html/upload/temp_image';
-     }
-     
-     /**
-      * 会員登録画面.
-      *
-      * @Route("/entry", name="entry", methods={"GET", "POST"})
-      * @Route("/entry", name="entry_confirm", methods={"GET", "POST"})
-      * @Template("Entry/index.twig")
-      */
-     public function index(Request $request)
-     {
-         if ($this->isGranted('ROLE_USER')) {
-             log_info('認証済のためログイン処理をスキップ');
-             return $this->redirectToRoute('mypage');
-         }
-         /** @var Customer $Customer */
-         $Customer = $this->customerRepository->newCustomer();
-         $builder = $this->formFactory->createBuilder(EntryType::class, $Customer);
-         $event = new EventArgs(
-             [
-                 'builder' => $builder,
-                 'Customer' => $Customer,
-             ],
-             $request
-         );
-         $this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_ENTRY_INDEX_INITIALIZE);
-         $form = $builder->getForm();
-         $form->handleRequest($request);
-         
-         /* if($form->isSubmitted()){
-             if ($form->isValid()) {
-                 // 有効な場合の処理
-             } else {
-                 // エラーのダンプ
-                 $errors = $form->getErrors(true, true); // すべてのエラーを取得
-                 dd($errors); // エラー内容を確認
-             }
-         } */
-         if ($form->isSubmitted() && $form->isValid()) {
-             switch ($request->get('mode')) {
-                 case 'confirm':
-                     log_info('会員登録確認開始');
-                     // 写真の一時保存処理
-                     if($customerImage = $form['customer_image']->getData()){
-                         if ($customerImage instanceof UploadedFile) {
-                             $fileName = md5(uniqid()).'.'.$customerImage->getClientOriginalExtension();
-                             $customerImage->move($this->tempDir, $fileName);
-                             $Customer->setCustomerImage($fileName);
-                             $this->session->set('eccube.front.customer.customer_image', $fileName);
-                         }
-                     }
-                     if($licenseImage = $form['license_image']->getData()){
-                         if ($licenseImage instanceof UploadedFile) {
-                             $fileName = md5(uniqid()).'.'.$licenseImage->getClientOriginalExtension();
-                             $licenseImage->move($this->tempDir, $fileName);
-                             $Customer->setLicenseImage($fileName);
-                             $this->session->set('eccube.front.customer.license_image', $fileName);
-                         }
-                     }
-                     log_info('会員登録確認完了');
-                     return $this->render(
-                         'Entry/confirm.twig',
-                         [
-                             'form' => $form->createView(),
-                             'Page' => $this->pageRepository->getPageByRoute('entry_confirm'),
-                         ]
-                     );
-                 case 'complete':
-                     log_info('会員登録開始');
-                     $encoder = $this->encoderFactory->getEncoder($Customer);
-                     $salt = $encoder->createSalt();
-                     $password = $encoder->encodePassword($Customer->getPlainPassword(), $salt);
-                     $secretKey = $this->customerRepository->getUniqueSecretKey();
-                     $Customer
-                         ->setSalt($salt)
-                         ->setPassword($password)
-                         ->setSecretKey($secretKey)
-                         ->setPoint(0);
-                     // 写真の本保存処理
-                     if($customerImageName = $this->session->get('eccube.front.customer.customer_image')){
-                         if ($customerImageName && file_exists($this->tempDir.'/'.$customerImageName)) {
-                             rename(
-                                 $this->tempDir.'/'.$customerImageName,
-                                 $this->customerImageDir.'/'.$customerImageName
-                             );
-                             $Customer->setCustomerImage($customerImageName);
-                         }
-                     }
-                     if($licenseImageName = $this->session->get('eccube.front.customer.license_image')){
-                         if ($licenseImageName && file_exists($this->tempDir.'/'.$licenseImageName)) {
-                             rename(
-                                 $this->tempDir.'/'.$licenseImageName,
-                                 $this->licenseImageDir.'/'.$licenseImageName
-                             );
-                             $Customer->setLicenseImage($licenseImageName);
-                         }
-                     }
-                     $this->entityManager->persist($Customer);
-                     $this->entityManager->flush();
-                     log_info('会員登録完了');
-                     $event = new EventArgs(
-                         [
-                             'form' => $form,
-                             'Customer' => $Customer,
-                         ],
-                         $request
-                     );
-                     $this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_ENTRY_INDEX_COMPLETE);
-                     $activateFlg = $this->BaseInfo->isOptionCustomerActivate();
-                     if ($activateFlg) {
-                         $activateUrl = $this->generateUrl('entry_activate', ['secret_key' => $Customer->getSecretKey()], UrlGeneratorInterface::ABSOLUTE_URL);
-                         $this->mailService->sendCustomerConfirmMail($Customer, $activateUrl);
-                         if ($event->hasResponse()) {
-                             return $event->getResponse();
-                         }
-                         log_info('仮会員登録完了画面へリダイレクト');
-                         return $this->redirectToRoute('entry_complete');
-                     } else {
-                         $qtyInCart = $this->entryActivate($request, $Customer->getSecretKey());
-                         return $this->redirectToRoute('entry_activate', [
-                             'secret_key' => $Customer->getSecretKey(),
-                             'qtyInCart' => $qtyInCart,
-                         ]);
-                     }
-             }
-         }
-         return [
-             'form' => $form->createView(),
-         ];
-     }
- }
-