|
|
@@ -3,12 +3,197 @@
|
|
|
namespace App\Service\Organization;
|
|
|
|
|
|
use App\ApiResources\Organization\OrganizationCreationRequest;
|
|
|
+use App\Entity\Access\Access;
|
|
|
+use App\Entity\Core\AddressPostal;
|
|
|
+use App\Entity\Core\ContactPoint;
|
|
|
+use App\Entity\Education\Cycle;
|
|
|
+use App\Entity\Network\NetworkOrganization;
|
|
|
use App\Entity\Organization\Organization;
|
|
|
+use App\Entity\Organization\OrganizationAddressPostal;
|
|
|
+use App\Entity\Organization\Parameters;
|
|
|
+use App\Entity\Organization\Settings;
|
|
|
+use App\Entity\Person\Person;
|
|
|
+use App\Enum\Core\ContactPointTypeEnum;
|
|
|
+use App\Enum\Education\CycleEnum;
|
|
|
+use App\Enum\Organization\AddressPostalOrganizationTypeEnum;
|
|
|
+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 libphonenumber\PhoneNumber;
|
|
|
+use Symfony\Component\String\ByteString;
|
|
|
+use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
|
|
+use Throwable;
|
|
|
|
|
|
class OrganizationFactory
|
|
|
{
|
|
|
- public function create(OrganizationCreationRequest $organizationCreationRequest)
|
|
|
+ public function __construct(
|
|
|
+ private readonly SubdomainService $subdomainService,
|
|
|
+ private readonly OrganizationRepository $organizationRepository,
|
|
|
+ private readonly CountryRepository $countryRepository,
|
|
|
+ private readonly NetworkRepository $networkRepository,
|
|
|
+ private readonly Typo3Service $typo3Service,
|
|
|
+ private readonly DolibarrApiService $dolibarrApiService,
|
|
|
+ private readonly BindFileService $bindFileService
|
|
|
+ ) {}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Créé une nouvelle organisation à partir des données contenues dans une OrganizationCreationRequest
|
|
|
+ *
|
|
|
+ * @param OrganizationCreationRequest $organizationCreationRequest
|
|
|
+ * @return Organization
|
|
|
+ * @throws TransportExceptionInterface
|
|
|
+ * @throws Throwable
|
|
|
+ */
|
|
|
+ public function create(OrganizationCreationRequest $organizationCreationRequest): Organization
|
|
|
{
|
|
|
+ // Création de l'organisation
|
|
|
+ $organization = new Organization();
|
|
|
+ $organization->setName($organizationCreationRequest->getName());
|
|
|
+ $organization->setLegalStatus($organizationCreationRequest->getLegalStatus());
|
|
|
+
|
|
|
+ // Création des Parameters
|
|
|
+ $parameters = new Parameters();
|
|
|
+ $organization->setParameters($parameters);
|
|
|
+
|
|
|
+ // Création des Settings
|
|
|
+ $settings = new Settings();
|
|
|
+ $settings->setProduct($organizationCreationRequest->getProduct());
|
|
|
+ $organization->setSettings($settings);
|
|
|
+
|
|
|
+ // Création de l'adresse postale
|
|
|
+ $addressPostal = new AddressPostal();
|
|
|
+ $addressPostal->setStreetAddress($organizationCreationRequest->getStreetAddress1());
|
|
|
+ $addressPostal->setStreetAddressSecond($organizationCreationRequest->getStreetAddress2());
|
|
|
+ $addressPostal->setStreetAddressThird($organizationCreationRequest->getStreetAddress3());
|
|
|
+ $addressPostal->setPostalCode($organizationCreationRequest->getPostalCode());
|
|
|
+ $addressPostal->setAddressCity($organizationCreationRequest->getCity());
|
|
|
+
|
|
|
+ $country = $this->countryRepository->find($organizationCreationRequest->getCountryId());
|
|
|
+ $addressPostal->setAddressCountry($country);
|
|
|
+
|
|
|
+ $organizationAddressPostal = new OrganizationAddressPostal();
|
|
|
+ $organizationAddressPostal->setAddressPostal($addressPostal);
|
|
|
+ $organizationAddressPostal->setType(AddressPostalOrganizationTypeEnum::ADDRESS_HEAD_OFFICE);
|
|
|
+ $organization->addOrganizationAddressPostal($organizationAddressPostal);
|
|
|
+
|
|
|
+ // Création du point de contact
|
|
|
+ $phoneNumber = new PhoneNumber();
|
|
|
+ $phoneNumber->unserialize($organizationCreationRequest->getPhoneNumber());
|
|
|
+
|
|
|
+ $contactPoint = new ContactPoint();
|
|
|
+ $contactPoint->setContactType(ContactPointTypeEnum::PRINCIPAL);
|
|
|
+ $contactPoint->setEmail($organizationCreationRequest->getEmail());
|
|
|
+ $contactPoint->setTelphone($phoneNumber);
|
|
|
+ $organization->addContactPoint($contactPoint);
|
|
|
+
|
|
|
+ // Rattachement au réseau
|
|
|
+ $parent = $this->organizationRepository->find($organizationCreationRequest->getParentId());
|
|
|
+ if (!$parent) {
|
|
|
+ throw new \RuntimeException('No parent organization found for id ' . $organizationCreationRequest->getParentId());
|
|
|
+ }
|
|
|
+
|
|
|
+ $network = $this->networkRepository->find($organizationCreationRequest->getNetworkId());
|
|
|
+ if (!$network) {
|
|
|
+ throw new \RuntimeException('No network found for id ' . $organizationCreationRequest->getNetworkId());
|
|
|
+ }
|
|
|
+
|
|
|
+ $networkOrganization = new NetworkOrganization();
|
|
|
+ $networkOrganization->setParent($parent);
|
|
|
+ $networkOrganization->setNetwork($network);
|
|
|
+ $networkOrganization->setStartDate(DatesUtils::new());
|
|
|
+ $organization->addNetworkOrganization($networkOrganization);
|
|
|
+
|
|
|
+ // Créé l'admin
|
|
|
+ $admin = new Person();
|
|
|
+ $admin->setUsername('admin' . strtolower($organizationCreationRequest->getSubdomain()));
|
|
|
+ $randomString = ByteString::fromRandom(32)->toString();
|
|
|
+ $admin->setPassword($randomString);
|
|
|
+
|
|
|
+ $adminAccess = new Access();
|
|
|
+ $adminAccess->setAdminAccess(true);
|
|
|
+ $adminAccess->setPerson($admin);
|
|
|
+ $adminAccess->setOrganization($organization);
|
|
|
+
|
|
|
+ // Création des cycles
|
|
|
+ $cyclesData = [
|
|
|
+ ['Cycle initiation', 10, CycleEnum::INITIATION_CYCLE],
|
|
|
+ ['Cycle 1', 20, CycleEnum::CYCLE_1],
|
|
|
+ ['Cycle 2', 30, CycleEnum::CYCLE_2],
|
|
|
+ ['Cycle 3', 40, CycleEnum::CYCLE_3],
|
|
|
+ ['Cycle 4', 50, CycleEnum::CYCLE_4],
|
|
|
+ ['Hors cycle', 60, CycleEnum::OUT_CYCLE],
|
|
|
+ ];
|
|
|
+
|
|
|
+ foreach ($cyclesData as $cycleData) {
|
|
|
+ $cycle = new Cycle();
|
|
|
+ $cycle->setLabel($cycleData[0]);
|
|
|
+ $cycle->setOrder($cycleData[1]);
|
|
|
+ $cycle->setCycleEnum($cycleData[2]);
|
|
|
+ $cycle->setIsSystem(false);
|
|
|
+ $cycle->setOrganization($organization);
|
|
|
+ $organization->addCycle($cycle);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Création du sous domaine
|
|
|
+ $this->subdomainService->addNewSubdomain(
|
|
|
+ $organization,
|
|
|
+ $organizationCreationRequest->getSubdomain(),
|
|
|
+ true
|
|
|
+ );
|
|
|
+
|
|
|
+ // Création du président (si renseigné)
|
|
|
+ $presidentCreationRequest = $organizationCreationRequest->getPresident();
|
|
|
+ if ($presidentCreationRequest !== null) {
|
|
|
+ $president = new Person();
|
|
|
+
|
|
|
+ $president->setUsername(
|
|
|
+ strtolower(str_replace(' ', '-', $presidentCreationRequest->getName()))
|
|
|
+ );
|
|
|
+ $president->setPassword(ByteString::fromRandom(32)->toString());
|
|
|
+
|
|
|
+ $president->setGender($presidentCreationRequest->getGender());
|
|
|
+ $president->setName($presidentCreationRequest->getName());
|
|
|
+ $president->setGivenName($presidentCreationRequest->getGivenName());
|
|
|
+
|
|
|
+ $presidentAccess = new Access();
|
|
|
+ $presidentAccess->setPerson($president);
|
|
|
+ $organization->addAccess($presidentAccess);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Création du directeur (si renseigné)
|
|
|
+ if ($organizationCreationRequest->getDirector() !== null) {
|
|
|
+ $director = new Person();
|
|
|
+
|
|
|
+ $director->setUsername(
|
|
|
+ strtolower(str_replace(' ', '-', $presidentCreationRequest->getName()))
|
|
|
+ );
|
|
|
+ $director->setPassword(ByteString::fromRandom(32)->toString());
|
|
|
+
|
|
|
+ $director->setGender($presidentCreationRequest->getGender());
|
|
|
+ $director->setName($presidentCreationRequest->getName());
|
|
|
+ $director->setGivenName($presidentCreationRequest->getGivenName());
|
|
|
+
|
|
|
+ $directorAccess = new Access();
|
|
|
+ $directorAccess->setPerson($director);
|
|
|
+ $organization->addAccess($directorAccess);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Création du site typo3
|
|
|
+ if ($organizationCreationRequest->getCreateWebsite()) {
|
|
|
+ $this->typo3Service->createSite($organization->getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ // Création de la société Dolibarr
|
|
|
+ $this->dolibarrApiService->createSociety($organization);
|
|
|
+
|
|
|
+ // Mise à jour du fichier Bind
|
|
|
+ $this->bindFileService->registerSubdomain($organizationCreationRequest->getSubdomain());
|
|
|
|
|
|
+ return $organization;
|
|
|
}
|
|
|
}
|