<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\RegistrationFormType;
use App\Repository\UserRepository;
use App\Security\LoginFormAuthenticator;
use App\Service\MangoPayService;
use DateTime;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
use App\Service\CalculNotation;
use App\Service\BankInfoManage;
use App\Service\ProfilePictureManage;
use App\Service\LockManage;
use App\Service\StatisticManagement;
class RegistrationController extends AbstractController
{
private UserPasswordHasherInterface $passwordEncoder;
private UserAuthenticatorInterface $guardHandler;
private LoginFormAuthenticator $authenticator;
private MangoPayService $mangopay;
private CalculNotation $notation;
private BankInfoManage $bankInfo;
private ProfilePictureManage $profilePicture;
private LockManage $lockManage;
private StatisticManagement $statistic;
private UserRepository $userRep;
public function __construct(
UserPasswordHasherInterface $passwordEncoder,
UserAuthenticatorInterface $guardHandler,
LoginFormAuthenticator $authenticator,
MangoPayService $mangopay,
CalculNotation $notation,
BankInfoManage $bankInfo,
ProfilePictureManage $profilePicture,
LockManage $lockManage,
StatisticManagement $statistic,
UserRepository $userRep
) {
$this->passwordEncoder = $passwordEncoder;
$this->guardHandler = $guardHandler;
$this->authenticator = $authenticator;
$this->mangopay = $mangopay;
$this->notation = $notation;
$this->bankInfo = $bankInfo;
$this->profilePicture = $profilePicture;
$this->lockManage = $lockManage;
$this->statistic = $statistic;
$this->userRep = $userRep;
}
#[Route('/register', name: 'app_register')]
public function register(Request $request): Response
{
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password
$checkMail = $this->userRep->findBy(['email' => $form['email']->getData()]);
if (!$checkMail || empty($checkMail)) {
$checkFirstUser = $this->userRep->findAll();
if (!$checkFirstUser) {
$roles = ['ROLE_SUPER_ADMIN'];
} else {
$roles = ['ROLE_CLIENT'];
}
$user->setRoles($roles);
$user->setUsername($form['email']->getData());
$user->setPassword(
$this->passwordEncoder->hashPassword(
$user,
$form->get('plainPassword')->getData()
)
);
$registrationDate = new DateTime(date('d-m-Y H:i:s'));
$user->setRegistrationDate($registrationDate);
$this->userRep->add($user, true);
//initialisation compte mangopay
$this->bankInfo->createUserBank($user);
$this->statistic->createStatisticUser($user);
$this->mangopay->setMangoClientUser($user);
$this->profilePicture->createUserProfilePicture($user);
$this->notation->createNotation($user);
$this->lockManage->initiateStatut($user);
return $this->guardHandler->authenticateUser(
$user,
$this->authenticator,
$request
);
} else {
$type = 'warning';
$message = "Adresse mail déjà utilisée par un autre utilisateur";
$this->addFlash($type, $message);
return $this->redirectToRoute('app_register');
}
}
return $this->render('registration/register.html.twig', [
'registrationForm' => $form->createView(),
]);
}
}