infinito/application/src/Controller/RegistrationController.php
2018-09-12 20:00:35 +02:00

62 lines
2.1 KiB
PHP

<?php
namespace App\Controller;
use App\Form\UserType;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Form\FormInterface;
class RegistrationController extends AbstractController
{
/**
*
* @var User
*/
private $user;
/**
*
* @Route("/register", name="user_register")
*/
public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder,TranslatorInterface $translator): Response
{
$this->user = new User();
$form = $this->createForm(UserType::class, $this->user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->encodePassword($passwordEncoder);
$this->saveUser($translator);
return $this->redirectToRoute('login');
}
return $this->render("user/register.html.twig", array(
'form' => $form->createView()
));
}
public function encodePassword(UserPasswordEncoderInterface $passwordEncoder): void
{
$password = $passwordEncoder->encodePassword($this->user, $this->user->getPlainPassword());
$this->user->setPassword($password);
}
private function saveUser(TranslatorInterface $translator): void
{
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($this->user);
try {
$entityManager->flush();
$this->addFlash('success', $translator->trans('User "%username%" created!',['%username%'=>$this->user->getUsername()]));
} catch (\Exception $exception) {
$this->addFlash('danger', $translator->trans('User "%username%" could not be created!',['%username%'=>$this->user->getUsername()]));
}
}
}