Files
myep/src/Controller/RegistrationController.php
T

62 lines
2.3 KiB
PHP

<?php
namespace App\Controller;
use App\BusProNet\ApiClient;
use App\BusProNet\Exception\ApiClientException;
use App\BusProNet\Model\Notification;
use App\Form\Model\RegistrationDto;
use App\Form\RegistrationType;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class RegistrationController extends AbstractController
{
public function __construct(
private readonly ApiClient $apiClient,
private readonly LoggerInterface $logger,
) {
}
#[Route('/registration', name: 'app_registration')]
#[IsGranted('PUBLIC_ACCESS')]
public function index(Request $request): Response
{
$registrationData = new RegistrationDto();
$form = $this->createForm(RegistrationType::class, $registrationData, [
'action' => $this->generateUrl('app_registration'),
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$response = $this->apiClient->register($registrationData);
if ($response instanceof Notification && true === $response->isError()) {
$this->addFlash('error', 'Möglicherweise bist du bereits registriert. Bitte setze dein Passwort zurück.');
} else {
$this->addFlash('success', 'Du erhältst in Kürze eine E-Mail mit einem Link zum (Zurück)setzen deines Passworts.');
}
$this->logger->info('Initiated registration', [
'email' => $registrationData->email,
]);
} catch (ApiClientException $e) {
$this->addFlash('error', $e->getMessage());
$this->logger->error('Error initiating registration', [
'email' => $registrationData->email,
'error' => $e->getMessage(),
]);
}
return $this->redirectToRoute('app_login');
}
return $this->render('registration/index.html.twig', [
'form' => $form->createView(),
]);
}
}