<?php
namespace App\Controller;
use App\Business\Company;
use App\Business\Domain\City;
use App\Extern\XrmService;
use App\Form\CompanyRegistrationType;
use App\Services\CompanyRegistrationService;
use App\Services\DomainService;
use App\Services\UserService;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
class CompanyRegistrationController extends AbstractController
{
/** @var UserService $userService */
protected $userService;
/** @var CompanyRegistrationService $companyRegistrationService */
protected $companyRegistrationService;
/** @var DomainService $domainService */
protected $domainService;
/** @var XrmService $xrmService */
protected $xrmService;
public function __construct(
UserService $userService,
CompanyRegistrationService $companyRegistrationService,
DomainService $domainService,
XrmService $xrmService
)
{
$this->userService = $userService;
$this->companyRegistrationService = $companyRegistrationService;
$this->domainService = $domainService;
$this->xrmService = $xrmService;
}
/**
* @Route("/company-registration", name="company_registration_form", options={"expose" = true}, methods={"GET", "POST"})
* @param Request|null $request
* @return Response
*/
public function companyRegistrationAction(Request $request = null)
{
$this->companyRegistrationService->addCompanyRegistrationLog($this->getUser(), "ACCESS", null);
$domainAssociatedCompanies = null;
if (!empty($this->getUser()) && !empty($this->getUser()->getContactMail())) {
$parts = explode('@', $this->getUser()->getEmail());
$domain = array_pop($parts);
$domainAssociatedCompanies = $this->xrmService->getOrganizationsRelatedToSpecificEmailDomain($domain);
}
$company = new Company();
$uniqIdForm = uniqid();
$form = $this->createForm(CompanyRegistrationType::class, $company, ['uniqIdForm' => $uniqIdForm]);
$form->handleRequest($request);
$response = null;
if ($form->isSubmitted() && $form->isValid()) {
$this->companyRegistrationService->addCompanyRegistrationLog($this->getUser(), "SUBMIT", $company);
// Create organization XRM
$cityObject = $this->domainService->getCity($company->city);
if (empty($cityObject)) {
$cityObject = new City();
$cityObject->name = $company->city;
$cityObject->code = $company->city;
}
$company->city = $cityObject;
$response = $this->xrmService->createOrganization($company, $this->getUser()->getContactMail());
if ($response->getStatus() === 'success') {
$this->companyRegistrationService->sendEmailCompanyRegistration($company, $this->getUser(), $response->getData());
$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.']);
} else {
$this->addFlash('company_registration_error', ['title' => 'There was an error with your company registration!', 'message' => $response->getMessage()]);
}
return $this->redirectToRoute('company_registration_form');
}
return $this->render(
'company-registration/company-registration.html.twig',
[
'form' => $form->createView(),
'user' => $this->getUser(),
'domainAssociatedCompanies' => $domainAssociatedCompanies,
'uniqIdForm' => $uniqIdForm,
'cities' => $this->domainService->getCities(),
]
);
}
}