<?php
namespace App\Controller;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\Form\Factory\FactoryInterface;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
/**
* Controller managing the registration.
*
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
* @author Christophe Coevoet <stof@notk.org>
*/
class RegistrationController extends Controller
{
const SESSION_KEY = 'xearts_daiko_king_bundle_register_controller_user';
private $eventDispatcher;
private $formFactory;
private $userManager;
public function __construct(EventDispatcherInterface $eventDispatcher, FactoryInterface $formFactory, UserManagerInterface $userManager)
{
$this->eventDispatcher = $eventDispatcher;
$this->formFactory = $formFactory;
$this->userManager = $userManager;
}
/**
* @param Request $request
*
* @return Response
* @Route("/register/", name="fos_user_registration_register")
*/
public function register(Request $request): Response
{
$user = $this->userManager->createUser();
$user->setEnabled(true);
$event = new GetResponseUserEvent($user, $request);
$this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
$form = $this->formFactory->createForm();
$form->setData($user);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$this->saveSubmitedData($request->getSession(), $request->get($form->getName()));
return $this->redirectToRoute('fos_user_registration_register_confirmation');
}
$event = new FormEvent($form, $request);
$this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_FAILURE, $event);
if (null !== $response = $event->getResponse()) {
return $response;
}
}
return $this->render('@FOSUser/Registration/register.html.twig', array(
'form' => $form->createView(),
));
}
/**
* 確認画面を表示する
* @param Request $request
* @return Response
* @Route("/register/confirmation", name="fos_user_registration_register_confirmation")
*/
public function registerConfirmation(Request $request)
{
$submitData = $this->loadSubmitedData($request->getSession());
if (!$submitData || !is_array($submitData)) {
return $this->redirectToRoute('fos_user_registration_register');
}
$user = $this->userManager->createUser();
$user->setEnabled(true);
$event = new GetResponseUserEvent($user, $request);
$this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
// セッションに保存したデータをチェックする
$inputForm = $this->formFactory->createForm();
$inputForm->setData($user);
$inputForm->submit($submitData);
if (!$inputForm->isValid()) {
return $this->redirectToRoute('fos_user_registration_register');
}
$form = $this->createConfirmationForm();
if ($request->isMethod('POST')) {
$form->handleRequest($request);
if ($form->isValid()) {
$this->saveSubmitedData($request->getSession(), null);
$event = new FormEvent($inputForm, $request);
$this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
$this->userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_registration_confirmed');
$response = new RedirectResponse($url);
}
$this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
}
return $this->render('@FOSUser/Registration/register_confirmation.html.twig', array(
'user' => $user,
'form' => $form->createView(),
));
}
/**
* @return \Symfony\Component\Form\FormInterface
*/
private function createConfirmationForm()
{
return $this->createFormBuilder()->getForm();
}
private function loadSubmitedData(SessionInterface $session)
{
return $session->get(self::SESSION_KEY);
}
private function saveSubmitedData(SessionInterface $session, $submitData)
{
$session->set(self::SESSION_KEY, $submitData);
}
}