<?php
namespace App\Controller;
use App\Form\Model\Registration;
use App\Form\Type\RegistrationType;
use Doctrine\ODM\MongoDB\DocumentManager;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
class RegistrationController extends AbstractController
{
private DocumentManager $dm;
private LoggerInterface $logger;
public function __construct(DocumentManager $dm, LoggerInterface $logger)
{
$this->dm = $dm;
$this->logger = $logger;
}
#[Route('/registration', name: 'registration')]
public function register(): Response
{
$form = $this->createForm(RegistrationType::class, new Registration());
return $this->render('registration\register.html.twig', [
'form' => $form->createView(),
]);
}
#[Route('/create_account', name: 'create_account')]
public function create(Request $request): RedirectResponse|Response
{
$form = $this->createForm(RegistrationType::class, new Registration());
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$user = $form->getData()->getUser();
$user->setEnabled(false);
$this->dm->persist($user);
$this->dm->flush();
return $this->redirect('login');
} catch (\Exception $exception) {
$this->logger->error('Registration error', [$exception->getMessage(), $exception->getCode(),]);
}
}
return $this->render('registration\register.html.twig', [
'form' => $form->createView()
]);
}
}