src/Controller/CompanyRegistrationController.php line 61

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Business\Company;
  4. use App\Business\Domain\City;
  5. use App\Extern\XrmService;
  6. use App\Form\CompanyRegistrationType;
  7. use App\Services\CompanyRegistrationService;
  8. use App\Services\DomainService;
  9. use App\Services\UserService;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  15. class CompanyRegistrationController extends AbstractController
  16. {
  17.     /** @var UserService $userService */
  18.     protected $userService;
  19.     /** @var CompanyRegistrationService $companyRegistrationService */
  20.     protected $companyRegistrationService;
  21.     /** @var DomainService $domainService */
  22.     protected $domainService;
  23.     /** @var XrmService $xrmService */
  24.     protected $xrmService;
  25.     public function __construct(
  26.         UserService $userService,
  27.         CompanyRegistrationService $companyRegistrationService,
  28.         DomainService $domainService,
  29.         XrmService $xrmService
  30.     )
  31.     {
  32.         $this->userService $userService;
  33.         $this->companyRegistrationService $companyRegistrationService;
  34.         $this->domainService $domainService;
  35.         $this->xrmService $xrmService;
  36.     }
  37.     /**
  38.      * @Route("/company-registration", name="company_registration_form", options={"expose" = true}, methods={"GET", "POST"})
  39.      * @param Request|null $request
  40.      * @return Response
  41.      */
  42.     public function companyRegistrationAction(Request $request null)
  43.     {
  44.         $this->companyRegistrationService->addCompanyRegistrationLog($this->getUser(), "ACCESS"null);
  45.         $domainAssociatedCompanies null;
  46.         if (!empty($this->getUser()) && !empty($this->getUser()->getContactMail())) {
  47.             $parts explode('@'$this->getUser()->getEmail());
  48.             $domain array_pop($parts);
  49.             $domainAssociatedCompanies $this->xrmService->getOrganizationsRelatedToSpecificEmailDomain($domain);
  50.         }
  51.         $company = new Company();
  52.         $uniqIdForm uniqid();
  53.         $form $this->createForm(CompanyRegistrationType::class, $company, ['uniqIdForm' => $uniqIdForm]);
  54.         $form->handleRequest($request);
  55.         $response null;
  56.         if ($form->isSubmitted() && $form->isValid()) {
  57.             $this->companyRegistrationService->addCompanyRegistrationLog($this->getUser(), "SUBMIT"$company);
  58.             // Create organization XRM
  59.             $cityObject $this->domainService->getCity($company->city);
  60.             if (empty($cityObject)) {
  61.                 $cityObject = new City();
  62.                 $cityObject->name $company->city;
  63.                 $cityObject->code $company->city;
  64.             }
  65.             $company->city $cityObject;
  66.             $response $this->xrmService->createOrganization($company$this->getUser()->getContactMail());
  67.             if ($response->getStatus() === 'success') {
  68.                 $this->companyRegistrationService->sendEmailCompanyRegistration($company$this->getUser(), $response->getData());
  69.                 $this->addFlash('company_registration_success', ['title' => 'Your company registration has been sent!''message' => 'You will receive an email confirmation (including the operational code) once the registration process is completed.']);
  70.             } else {
  71.                 $this->addFlash('company_registration_error', ['title' => 'There was an error with your company registration!''message' => $response->getMessage()]);
  72.             }
  73.             return $this->redirectToRoute('company_registration_form');
  74.         }
  75.         return $this->render(
  76.             'company-registration/company-registration.html.twig',
  77.             [
  78.                 'form' => $form->createView(),
  79.                 'user' => $this->getUser(),
  80.                 'domainAssociatedCompanies' => $domainAssociatedCompanies,
  81.                 'uniqIdForm' => $uniqIdForm,
  82.                 'cities' => $this->domainService->getCities(),
  83.             ]
  84.         );
  85.     }
  86. }