<?php
namespace App\Controller\Company;
use App\Form\Company\GouvType;
use App\Entity\Company\Company;
use App\Form\Company\CompanyType;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Utils\AccountVerification;
use App\Message\Auth\Company\ConfirmAccount;
use App\Repository\Company\CompanyRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Repository\Company\PresentationRepository;
use Symfony\Component\Messenger\MessageBusInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
/**
* @Route("/company")
*/
class CompanyController extends AbstractController
{
private $entityManager;
private $companyRepo;
private $bus;
public function __construct(EntityManagerInterface $entityManager,CompanyRepository $companyRepo, MessageBusInterface $bus)
{
$this->entityManager = $entityManager;
$this->companyRepo = $companyRepo;
$this->bus = $bus;
}
/**
* @Route("/select-type", name="company.select-type", methods={"GET","POST"})
*/
public function selectType(Request $request, PresentationRepository $presentationRepo): Response
{
return $this->render('company/company/select-type.html.twig', [
'presentations' => $presentationRepo->findAll(),
]);
}
/**
* @Route("/new", name="company.new", methods={"GET","POST"})
* @IsGranted("IS_AUTHENTICATED_FULLY")
*/
public function new(Request $request, PresentationRepository $prRepo): Response
{
$companyRf = $this->companyRepo->findByOwner($this->getUser()->getSlug());
if (!empty($companyRf)) {
return $this->render('company/company/creating-refused.html.twig', [
'company' => $companyRf,
]);
}
$companyType = $prRepo->findOneBy(['slug' => $request->query->get('type')]);
if($request->query->get('type') && !empty($companyType) ){
$company = new Company();
if ($companyType->getAbbreviation() === "EG") {
$form = $this->createForm(GouvType::class, $company);
}else{
$form = $this->createForm(CompanyType::class, $company);
}
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$company->setAccountVerification(new AccountVerification());
// $company->getJuridiction()->setNature($companyType);
$company->addOwner($this->getUser());
$this->getUser()->setRoles(['ROLE_COMPANY_OWNER']);
$this->entityManager->persist($company);
$this->entityManager->flush();
// $this->bus->dispatch(new ConfirmationSMSMessage($company));
// $this->bus->dispatch(new ConfirmationEmailMessage($company));
$this->dispatchMessage(new ConfirmAccount($company->getSlug()));
// Send an notification to the administrator and the company's owner
return $this->redirectToRoute('company.checking.email',[
'slug' => $company->getSlug()
]);
}else{
return $this->render('company/company/new.html.twig', [
'type' => $companyType,
'form' => $form->createView(),
]);
}
}else{
return $this->redirectToRoute('company.select-type');
}
}
/**
* @Route("/{id}", name="company.show", methods={"GET"})
*/
public function show(Company $company): Response
{
return $this->render('company/company/show.html.twig', [
'company' => $company,
]);
}
/**
* @Route("/{username}/edit", name="company.edit", methods={"GET","POST"})
* @IsGranted("IS_AUTHENTICATED_FULLY")
*/
public function edit(Request $request, Company $company): Response
{
$form = $this->createForm(CompanyType::class, $company);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('company.profile',['username' => $company->getUsername()]);
}
return $this->render('company/company/edit.html.twig', [
'company' => $company,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="company.delete", methods={"DELETE"})
*/
public function delete(Request $request, Company $company): Response
{
if ($this->isCsrfTokenValid('delete'.$company->getId(), $request->request->get('_token'))) {
// $this->entityManager->remove($company);
$this->entityManager->flush();
}
return $this->redirectToRoute('company.index');
}
}