src/Controller/RegistrationController.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use FOS\UserBundle\Event\FilterUserResponseEvent;
  4. use FOS\UserBundle\Event\FormEvent;
  5. use FOS\UserBundle\Event\GetResponseUserEvent;
  6. use FOS\UserBundle\Form\Factory\FactoryInterface;
  7. use FOS\UserBundle\FOSUserEvents;
  8. use FOS\UserBundle\Model\UserInterface;
  9. use FOS\UserBundle\Model\UserManagerInterface;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  11. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpFoundation\Session\Session;
  17. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  18. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  19. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  20. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  21. /**
  22.  * Controller managing the registration.
  23.  *
  24.  * @author Thibault Duplessis <thibault.duplessis@gmail.com>
  25.  * @author Christophe Coevoet <stof@notk.org>
  26.  */
  27. class RegistrationController extends Controller
  28. {
  29.     const SESSION_KEY 'xearts_daiko_king_bundle_register_controller_user';
  30.     private $eventDispatcher;
  31.     private $formFactory;
  32.     private $userManager;
  33.     public function __construct(EventDispatcherInterface $eventDispatcherFactoryInterface $formFactoryUserManagerInterface $userManager)
  34.     {
  35.         $this->eventDispatcher $eventDispatcher;
  36.         $this->formFactory $formFactory;
  37.         $this->userManager $userManager;
  38.     }
  39.     /**
  40.      * @param Request $request
  41.      *
  42.      * @return Response
  43.      * @Route("/register/", name="fos_user_registration_register")
  44.      */
  45.     public function register(Request $request): Response
  46.     {
  47.         $user $this->userManager->createUser();
  48.         $user->setEnabled(true);
  49.         $event = new GetResponseUserEvent($user$request);
  50.         $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE$event);
  51.         if (null !== $event->getResponse()) {
  52.             return $event->getResponse();
  53.         }
  54.         $form $this->formFactory->createForm();
  55.         $form->setData($user);
  56.         $form->handleRequest($request);
  57.         if ($form->isSubmitted()) {
  58.             if ($form->isValid()) {
  59.                 $this->saveSubmitedData($request->getSession(), $request->get($form->getName()));
  60.                 return $this->redirectToRoute('fos_user_registration_register_confirmation');
  61.             }
  62.             $event = new FormEvent($form$request);
  63.             $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_FAILURE$event);
  64.             if (null !== $response $event->getResponse()) {
  65.                 return $response;
  66.             }
  67.         }
  68.         return $this->render('@FOSUser/Registration/register.html.twig', array(
  69.             'form' => $form->createView(),
  70.         ));
  71.     }
  72.     /**
  73.      * 確認画面を表示する
  74.      * @param Request $request
  75.      * @return Response
  76.      * @Route("/register/confirmation", name="fos_user_registration_register_confirmation")
  77.      */
  78.     public function registerConfirmation(Request $request)
  79.     {
  80.         $submitData $this->loadSubmitedData($request->getSession());
  81.         if (!$submitData || !is_array($submitData)) {
  82.             return $this->redirectToRoute('fos_user_registration_register');
  83.         }
  84.         $user $this->userManager->createUser();
  85.         $user->setEnabled(true);
  86.         $event = new GetResponseUserEvent($user$request);
  87.         $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE$event);
  88.         if (null !== $event->getResponse()) {
  89.             return $event->getResponse();
  90.         }
  91.         // セッションに保存したデータをチェックする
  92.         $inputForm $this->formFactory->createForm();
  93.         $inputForm->setData($user);
  94.         $inputForm->submit($submitData);
  95.         if (!$inputForm->isValid()) {
  96.             return $this->redirectToRoute('fos_user_registration_register');
  97.         }
  98.         $form $this->createConfirmationForm();
  99.         if ($request->isMethod('POST')) {
  100.             $form->handleRequest($request);
  101.             if ($form->isValid()) {
  102.                 $this->saveSubmitedData($request->getSession(), null);
  103.                 $event = new FormEvent($inputForm$request);
  104.                 $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS$event);
  105.                 $this->userManager->updateUser($user);
  106.                 if (null === $response $event->getResponse()) {
  107.                     $url $this->generateUrl('fos_user_registration_confirmed');
  108.                     $response = new RedirectResponse($url);
  109.                 }
  110.                 $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user$request$response));
  111.                 return $response;
  112.             }
  113.         }
  114.         return $this->render('@FOSUser/Registration/register_confirmation.html.twig', array(
  115.             'user' => $user,
  116.             'form' => $form->createView(),
  117.         ));
  118.     }
  119.     /**
  120.      * @return \Symfony\Component\Form\FormInterface
  121.      */
  122.     private function createConfirmationForm()
  123.     {
  124.         return $this->createFormBuilder()->getForm();
  125.     }
  126.     private function loadSubmitedData(SessionInterface $session)
  127.     {
  128.         return $session->get(self::SESSION_KEY);
  129.     }
  130.     private function saveSubmitedData(SessionInterface $session$submitData)
  131.     {
  132.         $session->set(self::SESSION_KEY$submitData);
  133.     }
  134. }