src/Controller/Company/CompanyController.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Company;
  3. use App\Form\Company\GouvType;
  4. use App\Entity\Company\Company;
  5. use App\Form\Company\CompanyType;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use App\Entity\Utils\AccountVerification;
  8. use App\Message\Auth\Company\ConfirmAccount;
  9. use App\Repository\Company\CompanyRepository;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use App\Repository\Company\PresentationRepository;
  14. use Symfony\Component\Messenger\MessageBusInterface;
  15. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. /**
  18.  * @Route("/company")
  19.  */
  20. class CompanyController extends AbstractController
  21. {
  22.     private $entityManager;
  23.     private $companyRepo;
  24.     private $bus;
  25.     
  26.     public function __construct(EntityManagerInterface $entityManager,CompanyRepository $companyRepoMessageBusInterface $bus)
  27.     {
  28.         $this->entityManager $entityManager;
  29.         $this->companyRepo $companyRepo;
  30.         $this->bus $bus;
  31.     }
  32.     
  33.     /**
  34.      * @Route("/select-type", name="company.select-type", methods={"GET","POST"})
  35.      */
  36.     public function selectType(Request $requestPresentationRepository $presentationRepo): Response
  37.     {
  38.         return $this->render('company/company/select-type.html.twig', [
  39.             'presentations' => $presentationRepo->findAll(),
  40.         ]);
  41.     }
  42.     /**
  43.      * @Route("/new", name="company.new", methods={"GET","POST"})
  44.      * @IsGranted("IS_AUTHENTICATED_FULLY")
  45.      */
  46.     public function new(Request $requestPresentationRepository $prRepo): Response
  47.     {
  48.         $companyRf $this->companyRepo->findByOwner($this->getUser()->getSlug());
  49.         
  50.         if (!empty($companyRf)) {
  51.             return $this->render('company/company/creating-refused.html.twig', [
  52.                 'company' => $companyRf,
  53.             ]);
  54.         }
  55.         
  56.         $companyType $prRepo->findOneBy(['slug' => $request->query->get('type')]);
  57.         if($request->query->get('type') && !empty($companyType) ){
  58.             $company = new Company();
  59.             if ($companyType->getAbbreviation() === "EG") {
  60.                 $form $this->createForm(GouvType::class, $company);
  61.             }else{
  62.                 $form $this->createForm(CompanyType::class, $company);
  63.             }
  64.             $form->handleRequest($request);
  65.             if ($form->isSubmitted() && $form->isValid()) {
  66.                 $company->setAccountVerification(new AccountVerification());
  67.                 // $company->getJuridiction()->setNature($companyType);
  68.                 $company->addOwner($this->getUser());
  69.                 $this->getUser()->setRoles(['ROLE_COMPANY_OWNER']);
  70.                 $this->entityManager->persist($company);
  71.                 $this->entityManager->flush();
  72.                 // $this->bus->dispatch(new ConfirmationSMSMessage($company));
  73.                 // $this->bus->dispatch(new ConfirmationEmailMessage($company));
  74.                 $this->dispatchMessage(new ConfirmAccount($company->getSlug()));
  75.                 // Send an notification to the administrator and the company's owner
  76.                 return $this->redirectToRoute('company.checking.email',[
  77.                     'slug' => $company->getSlug()
  78.                 ]);
  79.             }else{
  80.                 return $this->render('company/company/new.html.twig', [
  81.                     'type' => $companyType,
  82.                     'form' => $form->createView(),
  83.                 ]);
  84.             }
  85.         }else{
  86.             return $this->redirectToRoute('company.select-type');
  87.         }
  88.     }
  89.     /**
  90.      * @Route("/{id}", name="company.show", methods={"GET"})
  91.      */
  92.     public function show(Company $company): Response
  93.     {
  94.         
  95.         return $this->render('company/company/show.html.twig', [
  96.             'company' => $company,
  97.         ]);
  98.     }
  99.     /**
  100.      * @Route("/{username}/edit", name="company.edit", methods={"GET","POST"})
  101.      * @IsGranted("IS_AUTHENTICATED_FULLY")
  102.      */
  103.     public function edit(Request $requestCompany $company): Response
  104.     {
  105.         $form $this->createForm(CompanyType::class, $company);
  106.         $form->handleRequest($request);
  107.         if ($form->isSubmitted() && $form->isValid()) {
  108.             $this->getDoctrine()->getManager()->flush();
  109.             return $this->redirectToRoute('company.profile',['username' => $company->getUsername()]);
  110.         }
  111.         return $this->render('company/company/edit.html.twig', [
  112.             'company' => $company,
  113.             'form' => $form->createView(),
  114.         ]);
  115.     }
  116.     /**
  117.      * @Route("/{id}", name="company.delete", methods={"DELETE"})
  118.      */
  119.     public function delete(Request $requestCompany $company): Response
  120.     {
  121.         if ($this->isCsrfTokenValid('delete'.$company->getId(), $request->request->get('_token'))) {
  122.             // $this->entityManager->remove($company);
  123.             $this->entityManager->flush();
  124.         }
  125.         return $this->redirectToRoute('company.index');
  126.     }
  127. }