|
|
@@ -21,18 +21,22 @@ use App\Repository\Core\CountryRepository;
|
|
|
use App\Repository\Network\NetworkRepository;
|
|
|
use App\Repository\Organization\OrganizationRepository;
|
|
|
use App\Service\Dolibarr\DolibarrApiService;
|
|
|
-use App\Service\Typo3\BindFileService;
|
|
|
use App\Service\Typo3\SubdomainService;
|
|
|
use App\Service\Typo3\Typo3Service;
|
|
|
use App\Service\Utils\DatesUtils;
|
|
|
+use Doctrine\ORM\EntityManagerInterface;
|
|
|
use Elastica\Param;
|
|
|
-use libphonenumber\PhoneNumber;
|
|
|
+use libphonenumber\PhoneNumberUtil;
|
|
|
+use Psr\Log\LoggerInterface;
|
|
|
use Symfony\Component\String\ByteString;
|
|
|
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
|
|
+use Symfony\Contracts\Service\Attribute\Required;
|
|
|
use Throwable;
|
|
|
|
|
|
class OrganizationFactory
|
|
|
{
|
|
|
+ private LoggerInterface $logger;
|
|
|
+
|
|
|
public function __construct(
|
|
|
private readonly SubdomainService $subdomainService,
|
|
|
private readonly OrganizationRepository $organizationRepository,
|
|
|
@@ -40,9 +44,16 @@ class OrganizationFactory
|
|
|
private readonly NetworkRepository $networkRepository,
|
|
|
private readonly Typo3Service $typo3Service,
|
|
|
private readonly DolibarrApiService $dolibarrApiService,
|
|
|
- private readonly BindFileService $bindFileService
|
|
|
+ private readonly EntityManagerInterface $entityManager
|
|
|
) {}
|
|
|
|
|
|
+ #[Required]
|
|
|
+ /** @see https://symfony.com/doc/current/logging/channels_handlers.html#how-to-autowire-logger-channels */
|
|
|
+ public function setLoggerInterface(LoggerInterface $adminLogger): void
|
|
|
+ {
|
|
|
+ $this->logger = $adminLogger;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Créé une nouvelle organisation à partir des données contenues dans une OrganizationCreationRequest
|
|
|
*
|
|
|
@@ -53,74 +64,146 @@ class OrganizationFactory
|
|
|
*/
|
|
|
public function create(OrganizationCreationRequest $organizationCreationRequest): Organization
|
|
|
{
|
|
|
- // Création de l'organisation
|
|
|
- $organization = $this->makeOrganization($organizationCreationRequest);
|
|
|
-
|
|
|
- // Création des Parameters
|
|
|
- $parameters = $this->makeParameters($organizationCreationRequest);
|
|
|
- $organization->setParameters($parameters);
|
|
|
-
|
|
|
- // Création des Settings
|
|
|
- $settings = $this->makeSettings($organizationCreationRequest);
|
|
|
- $organization->setSettings($settings);
|
|
|
-
|
|
|
- // Création de l'adresse postale
|
|
|
- $organizationAddressPostal = $this->makePostalAddress($organizationCreationRequest);
|
|
|
- $organization->addOrganizationAddressPostal($organizationAddressPostal);
|
|
|
-
|
|
|
- // Création du point de contact
|
|
|
- $contactPoint = $this->makeContactPoint($organizationCreationRequest);
|
|
|
- $organization->addContactPoint($contactPoint);
|
|
|
-
|
|
|
- // Rattachement au réseau
|
|
|
- $networkOrganization = $this->makeNetworkOrganization($organizationCreationRequest);
|
|
|
- $organization->addNetworkOrganization($networkOrganization);
|
|
|
-
|
|
|
- // Créé l'admin
|
|
|
- $adminAccess = $this->makeAdminAccess($organizationCreationRequest);
|
|
|
- $adminAccess->setOrganization($organization);
|
|
|
+ $this->logger->info(
|
|
|
+ 'Start the creation of a new organization named ' . $organizationCreationRequest->getName()
|
|
|
+ );
|
|
|
|
|
|
- // Création des cycles
|
|
|
- foreach ($this->makeCycles() as $cycle) {
|
|
|
- $cycle->setOrganization($organization);
|
|
|
- $organization->addCycle($cycle);
|
|
|
+ $this->validateSubdomain($organizationCreationRequest->getSubdomain());
|
|
|
+ $this->logger->info("Subdomain is valid and available : " . $organizationCreationRequest->getSubdomain());
|
|
|
+
|
|
|
+ $this->entityManager->beginTransaction();
|
|
|
+
|
|
|
+ try {
|
|
|
+ // Création de l'organisation
|
|
|
+ $organization = $this->makeOrganization($organizationCreationRequest);
|
|
|
+ $this->entityManager->persist($organization);
|
|
|
+ $this->logger->debug(" - Organization created");
|
|
|
+
|
|
|
+ // Création des Parameters
|
|
|
+ $parameters = $this->makeParameters($organizationCreationRequest);
|
|
|
+ $organization->setParameters($parameters);
|
|
|
+ $parameters->setOrganization($organization);
|
|
|
+ $this->entityManager->persist($parameters);
|
|
|
+ $this->logger->debug(" - Parameters created");
|
|
|
+
|
|
|
+ // Création des Settings
|
|
|
+ $settings = $this->makeSettings($organizationCreationRequest);
|
|
|
+ $organization->setSettings($settings);
|
|
|
+ $this->entityManager->persist($settings);
|
|
|
+ $this->logger->debug(" - Settings created");
|
|
|
+
|
|
|
+ // Création de l'adresse postale
|
|
|
+ $organizationAddressPostal = $this->makePostalAddress($organizationCreationRequest);
|
|
|
+ $organization->addOrganizationAddressPostal($organizationAddressPostal);
|
|
|
+ $this->entityManager->persist($organizationAddressPostal);
|
|
|
+ $this->logger->debug(" - OrganizationAddressPostal created");
|
|
|
+
|
|
|
+ // Création du point de contact
|
|
|
+ $contactPoint = $this->makeContactPoint($organizationCreationRequest);
|
|
|
+ $organization->addContactPoint($contactPoint);
|
|
|
+ $this->entityManager->persist($contactPoint);
|
|
|
+ $this->logger->debug(" - ContactPoint created");
|
|
|
+
|
|
|
+ // Rattachement au réseau
|
|
|
+ $networkOrganization = $this->makeNetworkOrganization($organizationCreationRequest);
|
|
|
+ $organization->addNetworkOrganization($networkOrganization);
|
|
|
+ $this->entityManager->persist($networkOrganization);
|
|
|
+ $this->logger->debug(" - NetworkOrganization created");
|
|
|
+
|
|
|
+ // Créé l'admin
|
|
|
+ $adminAccess = $this->makeAdminAccess($organizationCreationRequest);
|
|
|
+ $adminAccess->setOrganization($organization);
|
|
|
+ $this->entityManager->persist($adminAccess);
|
|
|
+ $this->logger->debug(" - Admin access created");
|
|
|
+
|
|
|
+ // Création des cycles
|
|
|
+ foreach ($this->makeCycles() as $cycle) {
|
|
|
+ $cycle->setOrganization($organization);
|
|
|
+ $organization->addCycle($cycle);
|
|
|
+ $this->entityManager->persist($cycle);
|
|
|
+ }
|
|
|
+ $this->logger->debug(" - Cycles created");
|
|
|
+
|
|
|
+ // Création du président (si renseigné)
|
|
|
+ $presidentCreationRequest = $organizationCreationRequest->getPresident();
|
|
|
+ if ($presidentCreationRequest !== null) {
|
|
|
+ $presidentAccess = $this->makeAccess($presidentCreationRequest);
|
|
|
+ $organization->addAccess($presidentAccess);
|
|
|
+ $this->entityManager->persist($presidentAccess);
|
|
|
+ $this->logger->debug(" - President access created");
|
|
|
+ }
|
|
|
+
|
|
|
+ // Création du directeur (si renseigné)
|
|
|
+ $directorCreationRequest = $organizationCreationRequest->getDirector();
|
|
|
+ if ($directorCreationRequest !== null) {
|
|
|
+ $directorAccess = $this->makeAccess($directorCreationRequest);
|
|
|
+ $organization->addAccess($directorAccess);
|
|
|
+ $this->entityManager->persist($directorAccess);
|
|
|
+ $this->logger->debug(" - Director access created");
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->entityManager->flush();
|
|
|
+ $this->entityManager->commit();
|
|
|
+ $this->logger->debug(" - New records commited");
|
|
|
+ $this->logger->info("Organization created in the DB");
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ $this->logger->critical("An error happened, operation cancelled : " . $e);
|
|
|
+ $this->entityManager->rollback();
|
|
|
+ throw $e;
|
|
|
}
|
|
|
|
|
|
- // Création du sous domaine
|
|
|
+ // Création et enregistrement du sous-domaine
|
|
|
$this->subdomainService->addNewSubdomain(
|
|
|
$organization,
|
|
|
$organizationCreationRequest->getSubdomain(),
|
|
|
true
|
|
|
);
|
|
|
-
|
|
|
- // Création du président (si renseigné)
|
|
|
- $presidentCreationRequest = $organizationCreationRequest->getPresident();
|
|
|
- if ($presidentCreationRequest !== null) {
|
|
|
- $presidentAccess = $this->makeAccess($presidentCreationRequest);
|
|
|
- $organization->addAccess($presidentAccess);
|
|
|
- }
|
|
|
-
|
|
|
- // Création du directeur (si renseigné)
|
|
|
- $directorCreationRequest = $organizationCreationRequest->getDirector();
|
|
|
- if ($directorCreationRequest !== null) {
|
|
|
- $directorAccess = $this->makeAccess($directorCreationRequest);
|
|
|
- $organization->addAccess($directorAccess);
|
|
|
- }
|
|
|
+ $this->logger->info("Subdomain created and activated");
|
|
|
|
|
|
// Création du site typo3
|
|
|
if ($organizationCreationRequest->getCreateWebsite()) {
|
|
|
- $this->typo3Service->createSite($organization->getId());
|
|
|
+ $response = $this->typo3Service->createSite($organization->getId());
|
|
|
+
|
|
|
+ // TODO: revoir l'utilité du champs cmsId
|
|
|
+ $rootPageUid = json_decode($response->getContent(), true);
|
|
|
+ $organization->setCmsId($rootPageUid);
|
|
|
+ $this->entityManager->persist($organization);
|
|
|
+ $this->entityManager->flush();
|
|
|
+ $this->logger->info("New typo3 website created (root uid : " . $rootPageUid . ")");
|
|
|
+ } else {
|
|
|
+ $this->logger->warning("Typo3 website creation was not required");
|
|
|
}
|
|
|
|
|
|
// Création de la société Dolibarr
|
|
|
- $this->dolibarrApiService->createSociety($organization);
|
|
|
-
|
|
|
- // Mise à jour du fichier Bind
|
|
|
- $this->bindFileService->registerSubdomain($organizationCreationRequest->getSubdomain());
|
|
|
+ $dolibarrId = $this->dolibarrApiService->createSociety($organization);
|
|
|
+ $this->logger->info("New dolibarr structure created (uid : " . $dolibarrId . ")");
|
|
|
|
|
|
return $organization;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Vérifie la disponibilité et la validité d'un sous domaine
|
|
|
+ *
|
|
|
+ * @param string $subdomainValue
|
|
|
+ * @return void
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ protected function validateSubdomain(string $subdomainValue): void
|
|
|
+ {
|
|
|
+ if (!$this->subdomainService->isValidSubdomain($subdomainValue)) {
|
|
|
+ throw new \RuntimeException("Not a valid subdomain :" . $subdomainValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($this->subdomainService->isReservedSubdomain($subdomainValue)) {
|
|
|
+ throw new \RuntimeException("This subdomain is not available :" . $subdomainValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($this->subdomainService->isRegistered($subdomainValue)) {
|
|
|
+ throw new \RuntimeException("This subdomain is already registered :" . $subdomainValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Créé une nouvelle instance d'organisation
|
|
|
*
|
|
|
@@ -142,14 +225,11 @@ class OrganizationFactory
|
|
|
*
|
|
|
* @param OrganizationCreationRequest $organizationCreationRequest The organization creation request
|
|
|
* @return Parameters The created Parameters object
|
|
|
- * @throws TransportExceptionInterface If there is an error with the transport
|
|
|
* @throws Throwable If there is an error
|
|
|
*/
|
|
|
protected function makeParameters(OrganizationCreationRequest $organizationCreationRequest): Parameters
|
|
|
{
|
|
|
- $parameters = new Parameters();
|
|
|
-
|
|
|
- return $parameters;
|
|
|
+ return new Parameters();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -202,8 +282,8 @@ class OrganizationFactory
|
|
|
*/
|
|
|
protected function makeContactPoint(OrganizationCreationRequest $organizationCreationRequest): ContactPoint
|
|
|
{
|
|
|
- $phoneNumber = new PhoneNumber();
|
|
|
- $phoneNumber->unserialize($organizationCreationRequest->getPhoneNumber());
|
|
|
+ $phoneUtil = PhoneNumberUtil::getInstance();
|
|
|
+ $phoneNumber = $phoneUtil->parse($organizationCreationRequest->getPhoneNumber());
|
|
|
|
|
|
$contactPoint = new ContactPoint();
|
|
|
$contactPoint->setContactType(ContactPointTypeEnum::PRINCIPAL);
|