src/Authentication/Controller/RegistrationController.php line 29

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Authentication\Controller;
  3. use App\Customer\Service\RegistrationService;
  4. use App\Framework\Controller\APIController;
  5. use App\Framework\Exception\APIException;
  6. use Doctrine\ORM\ORMException;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  9. use App\Authentication\Repository\CloudflareTurnstileRepository;
  10. use App\Authentication\Exception\InvalidTurnstileTokenException;
  11. use Psr\Log\LoggerInterface;
  12. #[Route(path'/api/registration')]
  13. class RegistrationController extends APIController
  14. {
  15.     public function __construct(
  16.         private LoggerInterface $logger,
  17.         private CloudflareTurnstileRepository $cfRepository,
  18.     ) {}
  19.     /**
  20.      * @throws ORMException
  21.      *
  22.      * @return array<string, int|bool>
  23.      */
  24.     #[Route(path''methods: ['POST'])]
  25.     public function registerAction(
  26.         AuthorizationCheckerInterface $authChecker,
  27.         RegistrationService $registrationService,
  28.     ): array {
  29.         if ($authChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
  30.             throw new APIException(400'User is already logged in');
  31.         }
  32.         // Parse the body
  33.         $data $this->getRequestBody();
  34.         $firstName $data['firstName'];
  35.         $lastName $data['lastName'];
  36.         $email $data['email'];
  37.         $password $data['password'];
  38.         $cfToken $data['cfToken'];
  39.         // Validate Cloudflare Turnstile
  40.         $isTokenValid $this->cfRepository->validateToken($cfToken);
  41.         if (!$isTokenValid) {
  42.             $this->logger->info("Token $cfToken is invalid");
  43.             throw new InvalidTurnstileTokenException();
  44.         } else {
  45.             $this->logger->info("Token $cfToken validated successfully");
  46.         }
  47.         // Create the user with the data
  48.         $registrationService->registerCustomer($firstName$lastName$email$password);
  49.         return [
  50.             'httpCode' => 201,
  51.             'status' => true,
  52.         ];
  53.     }
  54. }