OrganizationFactory.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. <?php
  2. namespace App\Service\Organization;
  3. use App\ApiResources\Organization\OrganizationCreationRequest;
  4. use App\ApiResources\Organization\OrganizationMemberCreationRequest;
  5. use App\Entity\Access\Access;
  6. use App\Entity\Core\AddressPostal;
  7. use App\Entity\Core\ContactPoint;
  8. use App\Entity\Education\Cycle;
  9. use App\Entity\Network\NetworkOrganization;
  10. use App\Entity\Organization\Organization;
  11. use App\Entity\Organization\OrganizationAddressPostal;
  12. use App\Entity\Organization\Parameters;
  13. use App\Entity\Organization\Settings;
  14. use App\Entity\Organization\Subdomain;
  15. use App\Entity\Person\Person;
  16. use App\Entity\Person\PersonAddressPostal;
  17. use App\Enum\Core\ContactPointTypeEnum;
  18. use App\Enum\Education\CycleEnum;
  19. use App\Enum\Network\NetworkEnum;
  20. use App\Enum\Organization\AddressPostalOrganizationTypeEnum;
  21. use App\Enum\Organization\SettingsProductEnum;
  22. use App\Enum\Person\AddressPostalPersonTypeEnum;
  23. use App\Repository\Core\CountryRepository;
  24. use App\Repository\Organization\OrganizationRepository;
  25. use App\Repository\Person\PersonRepository;
  26. use App\Service\Dolibarr\DolibarrApiService;
  27. use App\Service\Typo3\BindFileService;
  28. use App\Service\Typo3\SubdomainService;
  29. use App\Service\Typo3\Typo3Service;
  30. use App\Service\Utils\DatesUtils;
  31. use Doctrine\ORM\EntityManagerInterface;
  32. use Elastica\Param;
  33. use libphonenumber\NumberParseException;
  34. use libphonenumber\PhoneNumberUtil;
  35. use Psr\Log\LoggerInterface;
  36. use Symfony\Component\HttpFoundation\Response;
  37. use Symfony\Component\String\ByteString;
  38. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  39. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  40. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  41. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  42. use Symfony\Contracts\Service\Attribute\Required;
  43. use Throwable;
  44. use App\Service\Organization\Utils as OrganizationUtils;
  45. class OrganizationFactory
  46. {
  47. private LoggerInterface $logger;
  48. public function __construct(
  49. private readonly SubdomainService $subdomainService,
  50. private readonly OrganizationRepository $organizationRepository,
  51. private readonly CountryRepository $countryRepository,
  52. private readonly OrganizationUtils $organizationUtils,
  53. private readonly Typo3Service $typo3Service,
  54. private readonly DolibarrApiService $dolibarrApiService,
  55. private readonly EntityManagerInterface $entityManager,
  56. private readonly PersonRepository $personRepository,
  57. private readonly BindFileService $bindFileService,
  58. ) {}
  59. #[Required]
  60. /** @see https://symfony.com/doc/current/logging/channels_handlers.html#how-to-autowire-logger-channels */
  61. public function setLoggerInterface(LoggerInterface $adminLogger): void
  62. {
  63. $this->logger = $adminLogger;
  64. }
  65. /**
  66. * Créé une nouvelle organisation à partir des données contenues dans une OrganizationCreationRequest
  67. *
  68. * @param OrganizationCreationRequest $organizationCreationRequest
  69. * @return Organization
  70. * @throws TransportExceptionInterface
  71. * @throws Throwable
  72. */
  73. public function create(OrganizationCreationRequest $organizationCreationRequest): Organization
  74. {
  75. $this->logger->info(
  76. "Start the creation of a new organization named '" . $organizationCreationRequest->getName() . "'"
  77. );
  78. $this->entityManager->beginTransaction();
  79. try {
  80. // On vérifie si cette organisation n'existe pas déjà
  81. if ($this->isExistingOrganization($organizationCreationRequest)) {
  82. throw new \RuntimeException('An organization named ' . $organizationCreationRequest->getName() . ' already exists');
  83. }
  84. // On vérifie la validité et la disponibilité du sous domaine
  85. $this->validateSubdomain($organizationCreationRequest->getSubdomain());
  86. $this->logger->info("Subdomain is valid and available : '" . $organizationCreationRequest->getSubdomain() . "'");
  87. // On construit l'organisation et ses relations
  88. $organization = $this->makeOrganizationWithRelations($organizationCreationRequest);
  89. $this->logger->info("Organization created with all its relations");
  90. // On persiste et on commit
  91. $this->entityManager->persist($organization);
  92. $this->entityManager->flush();
  93. // Création de la société Dolibarr
  94. $dolibarrId = $this->dolibarrApiService->createSociety(
  95. $organization,
  96. $organizationCreationRequest->isClient()
  97. );
  98. $this->logger->info("New dolibarr structure created (uid : " . $dolibarrId . ")");
  99. $this->entityManager->commit();
  100. $this->logger->debug(" - New entities committed in DB");
  101. $this->logger->info("Organization persisted in the DB");
  102. } catch (\Exception $e) {
  103. $this->logger->critical("An error happened, operation cancelled\n" . $e);
  104. $this->entityManager->rollback();
  105. throw $e;
  106. }
  107. // Register the subdomain into the BindFile (takes up to 5min to take effect)
  108. $this->bindFileService->registerSubdomain($organizationCreationRequest->getSubdomain());
  109. $this->logger->info("Subdomain registered");
  110. // Création du site typo3 (on est obligé d'attendre que l'organisation soit persistée en base)
  111. if ($organizationCreationRequest->getCreateWebsite()) {
  112. $this->createTypo3Website($organization);
  113. } else {
  114. $this->logger->warning("Typo3 website creation was not required");
  115. }
  116. return $organization;
  117. }
  118. /**
  119. * Une organisation du même nom existe-t-elle déjà à la même adresse ?
  120. *
  121. * @param OrganizationCreationRequest $organizationCreationRequest
  122. * @return bool
  123. */
  124. protected function isExistingOrganization(OrganizationCreationRequest $organizationCreationRequest): bool
  125. {
  126. return $this
  127. ->organizationRepository
  128. ->count(
  129. [
  130. 'name' => $organizationCreationRequest->getName(),
  131. ]
  132. ) > 0;
  133. }
  134. /**
  135. * Vérifie la disponibilité et la validité d'un sous domaine
  136. *
  137. * @param string $subdomainValue
  138. * @return void
  139. * @throws \Exception
  140. */
  141. protected function validateSubdomain(string $subdomainValue): void
  142. {
  143. if (!$this->subdomainService->isValidSubdomain($subdomainValue)) {
  144. throw new \RuntimeException("Not a valid subdomain : " . $subdomainValue);
  145. }
  146. if ($this->subdomainService->isReservedSubdomain($subdomainValue)) {
  147. throw new \RuntimeException("This subdomain is not available : " . $subdomainValue);
  148. }
  149. if ($this->subdomainService->isRegistered($subdomainValue)) {
  150. throw new \RuntimeException("This subdomain is already registered : " . $subdomainValue);
  151. }
  152. }
  153. /**
  154. * Créé une nouvelle instance d'organisation, et toutes les instances liées (paramètres, contact, adresses, ...),
  155. * selon le contenu de la requête de création.
  156. *
  157. * @param OrganizationCreationRequest $organizationCreationRequest
  158. * @return Organization
  159. * @throws Throwable
  160. */
  161. protected function makeOrganizationWithRelations(
  162. OrganizationCreationRequest $organizationCreationRequest
  163. ): Organization
  164. {
  165. // Création de l'organisation
  166. $organization = $this->makeOrganization($organizationCreationRequest);
  167. $this->logger->debug(" - Organization created");
  168. // Création des Parameters
  169. $parameters = $this->makeParameters($organizationCreationRequest);
  170. $organization->setParameters($parameters);
  171. $this->logger->debug(" - Parameters created");
  172. // Création des Settings
  173. $settings = $this->makeSettings($organizationCreationRequest);
  174. $organization->setSettings($settings);
  175. $this->logger->debug(" - Settings created");
  176. // Création de l'adresse postale
  177. $organizationAddressPostal = $this->makePostalAddress($organizationCreationRequest);
  178. $organization->addOrganizationAddressPostal($organizationAddressPostal);
  179. $this->logger->debug(" - OrganizationAddressPostal created");
  180. // Création du point de contact
  181. $contactPoint = $this->makeContactPoint($organizationCreationRequest);
  182. $organization->addContactPoint($contactPoint);
  183. $this->logger->debug(" - ContactPoint created");
  184. // Rattachement au réseau
  185. $networkOrganization = $this->makeNetworkOrganization($organizationCreationRequest);
  186. $organization->addNetworkOrganization($networkOrganization);
  187. $this->logger->debug(" - NetworkOrganization created");
  188. // Créé l'admin
  189. $adminAccess = $this->makeAdminAccess($organizationCreationRequest);
  190. $organization->addAccess($adminAccess);
  191. $this->logger->debug(" - Admin access created");
  192. // Création des cycles
  193. foreach ($this->makeCycles() as $cycle) {
  194. $organization->addCycle($cycle);
  195. }
  196. $this->logger->debug(" - Cycles created");
  197. // Création du président (si renseigné)
  198. $presidentCreationRequest = $organizationCreationRequest->getPresident();
  199. if ($presidentCreationRequest !== null) {
  200. $presidentAccess = $this->makeAccess($presidentCreationRequest);
  201. $organization->addAccess($presidentAccess);
  202. $this->logger->debug(" - President access created");
  203. }
  204. // Création du directeur (si renseigné)
  205. $directorCreationRequest = $organizationCreationRequest->getDirector();
  206. if ($directorCreationRequest !== null) {
  207. $directorAccess = $this->makeAccess($directorCreationRequest);
  208. $organization->addAccess($directorAccess);
  209. $this->logger->debug(" - Director access created");
  210. }
  211. // Création du sous-domaine
  212. $subdomain = $this->makeSubdomain($organizationCreationRequest);
  213. $subdomain->setOrganization($organization);
  214. // <--- Pour la rétrocompatibilité avec la v1 ; pourra être supprimé lorsque la migration sera achevée
  215. $parameters = $organization->getParameters();
  216. $parameters->setSubDomain($organizationCreationRequest->getSubdomain());
  217. $parameters->setOtherWebsite('https://' . $organizationCreationRequest->getSubdomain() . '.opentalent.fr');
  218. $this->entityManager->persist($parameters);
  219. // --->
  220. $this->logger->debug(" - Subdomain created");
  221. return $organization;
  222. }
  223. /**
  224. * Créé une nouvelle instance d'organisation
  225. *
  226. * @param OrganizationCreationRequest $organizationCreationRequest
  227. * @return Organization
  228. */
  229. protected function makeOrganization(OrganizationCreationRequest $organizationCreationRequest): Organization
  230. {
  231. // Création de l'organisation
  232. $organization = new Organization();
  233. $organization->setName($organizationCreationRequest->getName());
  234. $organization->setLegalStatus($organizationCreationRequest->getLegalStatus());
  235. $organization->setPrincipalType($organizationCreationRequest->getPrincipalType());
  236. $organization->setIdentifier($organizationCreationRequest->getIdentifier());
  237. $this->entityManager->persist($organization);
  238. return $organization;
  239. }
  240. /**
  241. * Create a new Parameters object from the data in an OrganizationCreationRequest
  242. *
  243. * @param OrganizationCreationRequest $organizationCreationRequest The organization creation request
  244. * @return Parameters The created Parameters object
  245. * @throws Throwable If there is an error
  246. */
  247. protected function makeParameters(OrganizationCreationRequest $organizationCreationRequest): Parameters
  248. {
  249. $parameters = new Parameters();
  250. $this->entityManager->persist($parameters);
  251. return $parameters;
  252. }
  253. /**
  254. * Creates a new instance of the Settings class based on the given OrganizationCreationRequest object.
  255. *
  256. * @param OrganizationCreationRequest $organizationCreationRequest The OrganizationCreationRequest object containing the required data.
  257. *
  258. * @return Settings The newly created instance of the Settings class.
  259. */
  260. protected function makeSettings(OrganizationCreationRequest $organizationCreationRequest): Settings
  261. {
  262. $settings = new Settings();
  263. $settings->setProduct($organizationCreationRequest->getProduct());
  264. $this->entityManager->persist($settings);
  265. return $settings;
  266. }
  267. /**
  268. * Creates a new instance of the OrganizationAddressPostal class based on the given OrganizationCreationRequest object.
  269. *
  270. * @param OrganizationCreationRequest $organizationCreationRequest The OrganizationCreationRequest object containing the required data.
  271. *
  272. * @return OrganizationAddressPostal The newly created instance of the OrganizationAddressPostal class.
  273. */
  274. protected function makePostalAddress(OrganizationCreationRequest $organizationCreationRequest): OrganizationAddressPostal
  275. {
  276. $country = $this->countryRepository->find($organizationCreationRequest->getCountryId());
  277. if (!$country) {
  278. throw new \RuntimeException('No country found for id ' . $organizationCreationRequest->getCountryId());
  279. }
  280. $addressPostal = new AddressPostal();
  281. $addressPostal->setStreetAddress($organizationCreationRequest->getStreetAddress1());
  282. $addressPostal->setStreetAddressSecond($organizationCreationRequest->getStreetAddress2());
  283. $addressPostal->setStreetAddressThird($organizationCreationRequest->getStreetAddress3());
  284. $addressPostal->setPostalCode($organizationCreationRequest->getPostalCode());
  285. $addressPostal->setAddressCity($organizationCreationRequest->getCity());
  286. $addressPostal->setAddressCountry($country);
  287. $this->entityManager->persist($addressPostal);
  288. $organizationAddressPostal = new OrganizationAddressPostal();
  289. $organizationAddressPostal->setAddressPostal($addressPostal);
  290. $organizationAddressPostal->setType(AddressPostalOrganizationTypeEnum::ADDRESS_HEAD_OFFICE);
  291. $this->entityManager->persist($organizationAddressPostal);
  292. return $organizationAddressPostal;
  293. }
  294. /**
  295. * Creates a new instance of the ContactPoint class based on the given OrganizationCreationRequest object.
  296. *
  297. * @param OrganizationCreationRequest $organizationCreationRequest The OrganizationCreationRequest object containing the required data.
  298. *
  299. * @return ContactPoint The newly created instance of the ContactPoint class.
  300. * @throws NumberParseException
  301. */
  302. protected function makeContactPoint(OrganizationCreationRequest $organizationCreationRequest): ContactPoint
  303. {
  304. $phoneUtil = PhoneNumberUtil::getInstance();
  305. if (!$phoneUtil->isPossibleNumber($organizationCreationRequest->getPhoneNumber())) {
  306. throw new \RuntimeException("Phone number is invalid or missing");
  307. }
  308. $phoneNumber = $phoneUtil->parse($organizationCreationRequest->getPhoneNumber());
  309. $contactPoint = new ContactPoint();
  310. $contactPoint->setContactType(ContactPointTypeEnum::PRINCIPAL);
  311. $contactPoint->setEmail($organizationCreationRequest->getEmail());
  312. $contactPoint->setTelphone($phoneNumber);
  313. $this->entityManager->persist($contactPoint);
  314. return $contactPoint;
  315. }
  316. /**
  317. * Creates a new instance of the NetworkOrganization class based on the given OrganizationCreationRequest object.
  318. *
  319. * @param OrganizationCreationRequest $organizationCreationRequest The OrganizationCreationRequest object containing the required data.
  320. *
  321. * @return NetworkOrganization The newly created instance of the NetworkOrganization class.
  322. *
  323. * @throws \RuntimeException|\Exception if no parent organization is found for the given parent ID or if no network is found for the given network ID.
  324. */
  325. protected function makeNetworkOrganization(OrganizationCreationRequest $organizationCreationRequest): NetworkOrganization
  326. {
  327. $parent = $this->organizationRepository->find($organizationCreationRequest->getParentId());
  328. if (!$parent) {
  329. throw new \RuntimeException('No parent organization found for id ' . $organizationCreationRequest->getParentId());
  330. }
  331. if ($parent->getSettings()->getProduct() !== SettingsProductEnum::MANAGER) {
  332. throw new \RuntimeException(
  333. "Parent organization must have the product 'manager' (actual product: " .
  334. $organizationCreationRequest->getParentId() . ")"
  335. );
  336. }
  337. $networkOrganization = $this->organizationUtils->getActiveNetworkOrganization($parent);
  338. if (!$networkOrganization) {
  339. throw new \RuntimeException('No network found for parent ' . $organizationCreationRequest->getParentId());
  340. }
  341. $network = $networkOrganization->getNetwork();
  342. // Si réseau CMF, on vérifie que le matricule est valide
  343. if ($network->getId() === NetworkEnum::CMF->value) {
  344. if (!preg_match("/FR\d{12}/", $organizationCreationRequest->getIdentifier())) {
  345. throw new \RuntimeException("CMF identifier is missing or invalid.");
  346. }
  347. if ($this
  348. ->organizationRepository
  349. ->count(
  350. ['identifier' => $organizationCreationRequest->getIdentifier()]
  351. ) > 0) {
  352. throw new \RuntimeException("CMF identifier is already registered : '" . $organizationCreationRequest->getIdentifier() ."'");
  353. }
  354. }
  355. $networkOrganization = new NetworkOrganization();
  356. $networkOrganization->setParent($parent);
  357. $networkOrganization->setNetwork($network);
  358. $networkOrganization->setStartDate(DatesUtils::new());
  359. $this->entityManager->persist($networkOrganization);
  360. return $networkOrganization;
  361. }
  362. /**
  363. * Creates a new instance of the Access class with admin access based on the given OrganizationCreationRequest object.
  364. *
  365. * @param OrganizationCreationRequest $organizationCreationRequest The OrganizationCreationRequest object containing the required data.
  366. *
  367. * @return Access The newly created instance of the Access class with admin access.
  368. */
  369. protected function makeAdminAccess(OrganizationCreationRequest $organizationCreationRequest): Access
  370. {
  371. $admin = new Person();
  372. $admin->setUsername('admin' . strtolower($organizationCreationRequest->getSubdomain()));
  373. $randomString = ByteString::fromRandom(32)->toString();
  374. $admin->setPassword($randomString);
  375. $this->entityManager->persist($admin);
  376. $adminAccess = new Access();
  377. $adminAccess->setAdminAccess(true);
  378. $adminAccess->setPerson($admin);
  379. $this->entityManager->persist($adminAccess);
  380. return $adminAccess;
  381. }
  382. /**
  383. * Creates an array of Cycle objects based on a predefined set of data.
  384. *
  385. * @return Cycle[] An array of Cycle objects.
  386. */
  387. protected function makeCycles(): array
  388. {
  389. $cyclesData = [
  390. ['Cycle initiation', 10, CycleEnum::INITIATION_CYCLE],
  391. ['Cycle 1', 20, CycleEnum::CYCLE_1],
  392. ['Cycle 2', 30, CycleEnum::CYCLE_2],
  393. ['Cycle 3', 40, CycleEnum::CYCLE_3],
  394. ['Cycle 4', 50, CycleEnum::CYCLE_4],
  395. ['Hors cycle', 60, CycleEnum::OUT_CYCLE],
  396. ];
  397. $cycles = [];
  398. foreach ($cyclesData as $cycleData) {
  399. $cycle = new Cycle();
  400. $cycle->setLabel($cycleData[0]);
  401. $cycle->setOrder($cycleData[1]);
  402. $cycle->setCycleEnum($cycleData[2]);
  403. $cycle->setIsSystem(false);
  404. $this->entityManager->persist($cycle);
  405. $cycles[] = $cycle;
  406. }
  407. return $cycles;
  408. }
  409. /**
  410. * Creates an Access object based on the given OrganizationMemberCreationRequest.
  411. *
  412. * @param int|OrganizationMemberCreationRequest $creationRequestData The request object containing the
  413. * necessary data for creating a Person object,
  414. * or the id of an existing one.
  415. * @return Access The created Access object.
  416. * @throws NumberParseException
  417. */
  418. protected function makeAccess(
  419. int | OrganizationMemberCreationRequest $creationRequestData
  420. ): Access
  421. {
  422. if (is_int($creationRequestData)) {
  423. $person = $this->personRepository->find($creationRequestData);
  424. } else {
  425. $person = new Person();
  426. $person->setUsername($creationRequestData->getUsername());
  427. $person->setPassword(ByteString::fromRandom(32)->toString());
  428. $person->setGender($creationRequestData->getGender());
  429. $person->setName(
  430. ucfirst(strtolower($creationRequestData->getName()))
  431. );
  432. $person->setGivenName(
  433. ucfirst(strtolower($creationRequestData->getGivenName()))
  434. );
  435. $personPostalAddress = $this->makePersonPostalAddress($creationRequestData);
  436. $person->addPersonAddressPostal($personPostalAddress);
  437. $contactPoint = $this->makePersonContactPoint($creationRequestData);
  438. $person->addContactPoint($contactPoint);
  439. $this->entityManager->persist($person);
  440. }
  441. $access = new Access();
  442. $access->setPerson($person);
  443. $this->entityManager->persist($access);
  444. return $access;
  445. }
  446. /**
  447. * Creates a PersonAddressPostal object based on the given OrganizationMemberCreationRequest.
  448. *
  449. * @param OrganizationMemberCreationRequest $organizationMemberCreationRequest The request object containing the
  450. * necessary data for creating a
  451. * PersonAddressPostal object.
  452. * @return PersonAddressPostal The created PersonAddressPostal object.
  453. */
  454. protected function makePersonPostalAddress(OrganizationMemberCreationRequest $organizationMemberCreationRequest): PersonAddressPostal
  455. {
  456. $addressPostal = new AddressPostal();
  457. $addressPostal->setStreetAddress($organizationMemberCreationRequest->getStreetAddress1());
  458. $addressPostal->setStreetAddressSecond($organizationMemberCreationRequest->getStreetAddress2());
  459. $addressPostal->setStreetAddressThird($organizationMemberCreationRequest->getStreetAddress3());
  460. $addressPostal->setPostalCode($organizationMemberCreationRequest->getPostalCode());
  461. $addressPostal->setAddressCity($organizationMemberCreationRequest->getCity());
  462. $country = $this->countryRepository->find($organizationMemberCreationRequest->getCountryId());
  463. $addressPostal->setAddressCountry($country);
  464. $this->entityManager->persist($addressPostal);
  465. $personAddressPostal = new PersonAddressPostal();
  466. $personAddressPostal->setAddressPostal($addressPostal);
  467. $personAddressPostal->setType(AddressPostalPersonTypeEnum::ADDRESS_PRINCIPAL);
  468. $this->entityManager->persist($personAddressPostal);
  469. return $personAddressPostal;
  470. }
  471. /**
  472. * Creates a new instance of the ContactPoint class based on the given OrganizationCreationRequest object.
  473. *
  474. * @param OrganizationMemberCreationRequest $organizationMemberCreationRequest The OrganizationMemberCreationRequest object containing the required data.
  475. *
  476. * @return ContactPoint The newly created instance of the ContactPoint class.
  477. * @throws NumberParseException
  478. */
  479. protected function makePersonContactPoint(OrganizationMemberCreationRequest $organizationMemberCreationRequest): ContactPoint
  480. {
  481. $phoneUtil = PhoneNumberUtil::getInstance();
  482. $phoneNumber = $phoneUtil->parse($organizationMemberCreationRequest->getPhone());
  483. $contactPoint = new ContactPoint();
  484. $contactPoint->setContactType(ContactPointTypeEnum::PRINCIPAL);
  485. $contactPoint->setEmail($organizationMemberCreationRequest->getEmail());
  486. $contactPoint->setTelphone($phoneNumber);
  487. if ($organizationMemberCreationRequest->getMobile() !== null) {
  488. $mobileNumber = $phoneUtil->parse($organizationMemberCreationRequest->getMobile());
  489. $contactPoint->setMobilPhone($mobileNumber);
  490. }
  491. $this->entityManager->persist($contactPoint);
  492. return $contactPoint;
  493. }
  494. protected function makeSubdomain(OrganizationCreationRequest $organizationCreationRequest): Subdomain
  495. {
  496. $subdomain = new Subdomain();
  497. $subdomain->setSubdomain($organizationCreationRequest->getSubdomain());
  498. $subdomain->setActive(true);
  499. $this->entityManager->persist($subdomain);
  500. return $subdomain;
  501. }
  502. /**
  503. * Créé le site Typo3 et retourne l'id de la page racine du site nouvellement créé, ou null en cas d'erreur
  504. *
  505. * @throws RedirectionExceptionInterface
  506. * @throws ClientExceptionInterface
  507. * @throws TransportExceptionInterface
  508. * @throws ServerExceptionInterface
  509. */
  510. protected function createTypo3Website(Organization $organization): ?int {
  511. $response = $this->typo3Service->createSite($organization->getId());
  512. $rootPageUid = json_decode($response->getContent(), true);
  513. if ($response->getStatusCode() === Response::HTTP_OK && $rootPageUid > 0) {
  514. // TODO: revoir l'utilité du champs cmsId
  515. $organization->setCmsId($rootPageUid);
  516. $this->entityManager->persist($organization);
  517. $this->entityManager->flush();
  518. return $rootPageUid;
  519. } else {
  520. $this->logger->critical("/!\ A critical error happened while creating the Typo3 website");
  521. $this->logger->debug($response->getContent());
  522. }
  523. return null;
  524. }
  525. }