<?php
namespace App\Controller;
use App\Form\RegistrationProfessionalFormType;
use App\Repository\UserRepository;
use App\Security\LoginFormAuthenticator;
use App\Service\BankInfoManage;
use App\Service\CalculNotation;
use App\Service\LockManage;
use App\Service\MangoPayService;
use App\Service\ProfilePictureManage;
use App\Service\StatisticManagement;
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\Entity\User;
use App\Service\SireneApiRequest;
class RegistrationProfessionalController extends AbstractController
{
private UserPasswordHasherInterface $passwdEncoder;
private UserAuthenticatorInterface $guardHandler;
private LoginFormAuthenticator $loginFormAuth;
private MangoPayService $mangopay;
private CalculNotation $notation;
private BankInfoManage $bankInfo;
private ProfilePictureManage $profilePicture;
private LockManage $lockManage;
private StatisticManagement $statistic;
private SireneApiRequest $sireneApi;
private UserRepository $userRep;
public function __construct(
UserPasswordHasherInterface $passwdEncoder,
UserAuthenticatorInterface $guardHandler,
LoginFormAuthenticator $loginFormAuth,
MangoPayService $mangopay,
CalculNotation $notation,
BankInfoManage $bankInfo,
ProfilePictureManage $profilePicture,
LockManage $lockManage,
StatisticManagement $statistic,
SireneApiRequest $sireneApi,
UserRepository $userRep
) {
$this->passwdEncoder = $passwdEncoder;
$this->guardHandler = $guardHandler;
$this->loginFormAuth = $loginFormAuth;
$this->mangopay = $mangopay;
$this->notation = $notation;
$this->bankInfo = $bankInfo;
$this->profilePicture = $profilePicture;
$this->lockManage = $lockManage;
$this->statistic = $statistic;
$this->sireneApi = $sireneApi;
$this->userRep = $userRep;
}
#[Route('/register/prestataire', name: 'app_register_prestataire')]
public function register(Request $request): Response
{
$user = new User();
$form = $this->createForm(RegistrationProfessionalFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$checkMail = $this->userRep->findBy(['email' => $form['email']->getData()]);
if (!$checkMail || empty($checkMail)) {
if ($this->sireneApi->checkSirenNewUser(
$form->get('compagny')->get('identificationNumberCompagny')->getData()
)) {
// encode the plain password
$checkFirstUser = $this->userRep->findAll();
if (!$checkFirstUser) {
$roles = ['ROLE_SUPER_ADMIN'];
} else {
$roles = ['ROLE_FOURNISSEUR'];
}
$user->setRoles($roles);
$user->setUsername($form['email']->getData());
$user->setPassword(
$this->passwdEncoder->hashPassword(
$user,
$form->get('plainPassword')->getData()
)
);
$registrationDate = new DateTime(date('d-m-Y H:i:s'));
$user->setRegistrationDate($registrationDate);
$user->setCompagny($form->get('compagny')->getData());
$user->getCompagny()->setDomainActivity($form->get('compagny')->get('domainActivity')->getData());
$user->getCompagny()->setJob($form->get('compagny')->get('job')->getData());
$user->getCompagny()->setUser($user);
$user->getCompagny()->setLegalType($form->get('compagny')->get('typeBusiness')->getData());
$user->getCompagny()->setPourcentageFees(10);
$this->userRep->add($user, true);
$this->bankInfo->createUserBank($user);
$this->statistic->createStatisticUser($user);
$this->mangopay->setMangoProfessionnalUser($user);
$this->profilePicture->createUserProfilePicture($user);
$this->profilePicture->createUserProfessionnalProfilePicture($user);
$this->notation->createNotation($user);
$this->lockManage->initiateStatut($user);
return $this->guardHandler->authenticateUser(
$user,
$this->loginFormAuth,
$request
);
} else {
$type = 'warning';
$message = "Siren inconnu auprès de l'insee";
$this->addFlash($type, $message);
return $this->redirectToRoute('app_register_prestataire');
}
} else {
$type = 'warning';
$message = "Adresse mail déjà utilisée par un autre utilisateur";
$this->addFlash($type, $message);
return $this->redirectToRoute('app_register_prestataire');
}
}
return $this->render('registration/registerProfessional.html.twig', [
'registrationForm' => $form->createView(),
]);
}
}