<?php
namespace App\Controller;
use App\Entity\User;
use App\Entity\UserData;
use App\EntityManager\UserManager;
use App\Form\User\RegisterType;
use App\Notifications\User\UserMailer;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
protected $userManager;
protected $userMailer;
public function __construct(UserManager $userManager, UserMailer $userMailer)
{
$this->userManager = $userManager;
$this->userMailer = $userMailer;
}
/**
* @Route("/login", name="security_login")
* @Template()
*/
public function login(AuthenticationUtils $helper): array
{
return [
'last_username' => $helper->getLastUsername(),
'error' => $helper->getLastAuthenticationError(),
];
}
/**
* @Route("/logout", name="security_logout")
*/
public function logout(): void
{
throw new \Exception('This should never be reached!');
}
/**
* @Route("/creer_un_compte", name="security_register")
* @Template()
*/
public function register(Request $request)
{
$user = new User();
$form = $this->createForm(RegisterType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$userData = new UserData();
$userData->setGender($form->get('gender')->getData());
$userData->setBirthdate($form->get('birthdate')->getData());
$userData->setCountry($form->get('country')->getData());
$userData->setCity($form->get('city')->getData());
$userData->setProvince($form->get('province')->getData());
$user->setStatus(User::STATUS_ACTIVE);
$user->setUserData($userData);
$this->userManager->update($user);
// send email
$this->userMailer->register($user);
$this->get('session')->getFlashBag()->set('success', 'user.register');
return $this->redirectToRoute('_home');
}
}
return [
'form' => $form->createView(),
];
}
}