<?php
namespace App\Controller;
use App\Entity\Group;
use App\Entity\ParentStudent;
use App\Entity\Student;
use App\Entity\StudentsToGroup;
use App\Entity\User;
use App\Message\RegisterMessage;
use App\Service\GoogleSheet\GoogleSheetHandler;
use App\Service\PaymentService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Messenger\MessageBusInterface;
use App\Service\UserService;
use Symfony\Component\Security\Core\Security;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpClient\HttpClient;
use App\Service\GmailService;
class SignUpController extends AbstractController
{
public function __construct(
private UserService $userService,
private MessageBusInterface $bus
) {}
#[Route('grupa/{groupId}/zarejestruj', name: 'app_sign_up_with_group')]
#[Route('grupa/zarejestruj/{courseId}/{type}', name: 'app_sign_up', defaults: ['type' => null])]
public function index(
int $groupId = null,
SessionInterface $session,
Security $security,
?string $courseId,
?string $type = 'regular'
): Response {
if ($security->isGranted('IS_AUTHENTICATED_FULLY')) {
$userId = $security->getUser()->getId();
if ($groupId) {
$this->userService->signUpStudentToCourse($userId, $groupId);
return $this->redirectToRoute('dashboard');
}
$this->userService->addStudentToWaitingList($userId, $courseId, $type);
return $this->render('sign_up/waiting_list.html.twig');
}
return $this->render('sign_up/index.html.twig', [
'group_id' => $groupId,
'course_id' => $session->get('selected_course'),
'type' => $type
]);
}
#[Route('grupa/register/success', name: 'register_success')]
public function registerSuccess(): Response
{
return $this->render('sign_up/register_success.html.twig');
}
#[Route('grupa/register/waiting_list', name: 'register_waiting_list')]
public function registerWaitingList(): Response
{
return $this->render('sign_up/register_waiting_list.html.twig');
}
#[Route('grupa/register/account_exsist', name: 'account_exsist')]
public function accountExsist(): Response
{
return $this->render('sign_up/account_exsist.html.twig');
}
#[Route('/zapisz_na_kurs', name: 'app_sign_up_potential_client')]
public function signUpOnCourse(
GmailService $mailer,
ManagerRegistry $doctrine,
Request $request,
EntityManagerInterface $entityManager,
UserPasswordHasherInterface $passwordHasher,
PaymentService $paymentService,
GoogleSheetHandler $googleSheetHandler
): JsonResponse {
$clientData = json_decode($request->getContent(), true);
$status = 'error2';
if (!$this->isRecaptchaValid($clientData['g-recaptcha-response'] ?? '')) {
return new JsonResponse(['status' => $status]);
}
if (!is_array($clientData) || empty($clientData)) {
return new JsonResponse(['status' => 'error']);
}
$group = null;
if (!empty($clientData['group'])) {
$group = $doctrine->getRepository(Group::class)->find((int) $clientData['group']);
}
if ($this->userService->checkUserExist($clientData['email'], $clientData['phone'])) {
return new JsonResponse(['status' => 'error3']);
}
$uuid = Uuid::uuid4()->toString();
$clientData['uuid'] = $uuid;
$user = (new User())
->setEmail($clientData['email'])
->setPhone($clientData['phone'])
->setPassword($passwordHasher->hashPassword(new User(), $clientData['password']))
->setDistrict($clientData['district']);
$parent = (new ParentStudent())
->setEmail($clientData['email'])
->setPhone($clientData['phone'])
->setUser($user)
->setName($clientData['parent_name'])
->setSurname($clientData['parent_surname'])
->setUuid($uuid);
$student = (new Student())
->setName($clientData['child_name'])
->setSurname($clientData['child_surname'])
->setUser($user)
->setUuid($uuid);
$entityManager->persist($user);
$entityManager->persist($parent);
$entityManager->persist($student);
if ($group) {
$studentToGroup = (new StudentsToGroup())
->setStudent($student)
->setClass($group);
$entityManager->persist($studentToGroup);
}
try {
$entityManager->flush();
} catch (\Exception $e) {
throw new \RuntimeException('Nieoczekiwany błąd przy zapisie. ' . $e->getMessage(), 0, $e);
}
$googleSheetHandler->saveToGoogleSheet($clientData['district'], $clientData);
$mailer->sendTwigEmail(
$clientData['email'],
'Test maila',
'register',
['name' => $clientData['parent_name'] . ' ' . $clientData['parent_surname']]
);
//$this->bus->dispatch(new RegisterMessage($parent, $group));
return new JsonResponse(['status' => 'success']);
}
private function isRecaptchaValid(string $recaptchaResponse): bool
{
$secretKey = '6Lc4NSQrAAAAAD4anMSet7SJ-gTbqUiL_i8MVDBa';
$client = HttpClient::create();
$response = $client->request('POST', 'https://www.google.com/recaptcha/api/siteverify', [
'body' => [
'secret' => $secretKey,
'response' => $recaptchaResponse,
],
]);
$responseData = $response->toArray();
return $responseData['success'] ?? false;
}
#[Route('/zarejestruj', name: 'register')]
public function register(SessionInterface $session, int $groupId = null, ?string $type = 'regular'): Response
{
$courseId = $session->get('selected_course');
return $this->render('sign_up/index.html.twig', [
'group_id' => $groupId,
'course_id' => $courseId,
'type' => $type
]);
}
#[Route('/{slug}', name: 'register_district', requirements: ['slug' => 'krakow|krak|krk|lodz|poznan|slask|kurs|zapis|pzn|gdansk|rzeszow'])]
public function registerDistrict(Request $request): Response
{
$slug = trim($request->getPathInfo(), '/');
return $this->render('sign_up/index.html.twig', [
'district' => $slug
]);
}
}