소스 검색

add organizationFactory delete method (ongoing)

Olivier Massot 1 년 전
부모
커밋
077b4a195e
1개의 변경된 파일128개의 추가작업 그리고 0개의 파일을 삭제
  1. 128 0
      src/Service/Organization/OrganizationFactory.php

+ 128 - 0
src/Service/Organization/OrganizationFactory.php

@@ -3,10 +3,13 @@
 namespace App\Service\Organization;
 
 use App\ApiResources\Organization\OrganizationCreationRequest;
+use App\ApiResources\Organization\OrganizationDeletionRequest;
 use App\ApiResources\Organization\OrganizationMemberCreationRequest;
 use App\Entity\Access\Access;
+use App\Entity\Billing\BillingSetting;
 use App\Entity\Core\AddressPostal;
 use App\Entity\Core\ContactPoint;
+use App\Entity\Education\CriteriaNotation;
 use App\Entity\Education\Cycle;
 use App\Entity\Network\NetworkOrganization;
 use App\Entity\Organization\Organization;
@@ -29,8 +32,10 @@ use App\Service\Typo3\BindFileService;
 use App\Service\Typo3\SubdomainService;
 use App\Service\Typo3\Typo3Service;
 use App\Service\Utils\DatesUtils;
+use Doctrine\Common\Collections\Collection;
 use Doctrine\ORM\EntityManagerInterface;
 use Elastica\Param;
+use Exception;
 use libphonenumber\NumberParseException;
 use libphonenumber\PhoneNumberUtil;
 use Psr\Log\LoggerInterface;
@@ -583,4 +588,127 @@ class OrganizationFactory
 
         return null;
     }
+
+    /**
+     * /!\ Danger zone /!\
+     *
+     * @param Organization $organization
+     * @return void
+     * @throws Exception
+     */
+    public function delete(OrganizationDeletionRequest $organizationDeletionRequest): void
+    {
+        $organization = $this->organizationRepository->find($organizationDeletionRequest->getOrganizationId());
+
+        $this->logger->info(
+            "Start the deletion of organization '" . $organization->getName() . "' [" . $organization->getId() . "]"
+        );
+
+        $this->entityManager->beginTransaction();
+
+        try {
+            $this->deleteOrganizationAccesses($organization);
+            $this->deleteNetworkOrganizationsChildren($organization);
+            $this->deleteParameters($organization);
+            $this->deleteOrganizationFiles($organization);
+
+            $this->entityManager->remove($organization);
+
+            $this->entityManager->flush();
+        } catch (\Exception $e) {
+            $this->logger->critical("An error happened, operation cancelled\n" . $e);
+            $this->entityManager->rollback();
+            throw $e;
+        }
+
+        $this->deleteTypo3Website($organization);
+
+        $this->deleteDolibarrSociety($organization);
+
+        $this->deleteDirectories($organization);
+    }
+
+    /**
+     * Supprime tous les Access d'une organisation, ainsi que la Person
+     * rattachée (si celle-ci n'est pas liée à d'autres Access)
+     *
+     * @param Organization $organization
+     * @return void
+     */
+    protected function deleteOrganizationAccesses(Organization $organization): void
+    {
+        foreach ($organization->getAccesses() as $access) {
+            $person = $access->getPerson();
+            if ($person->getAccesses()->count() === 1) {
+                $this->deletePerson($person);
+            }
+            $this->entityManager->remove($access);
+        }
+    }
+
+    protected function deletePerson(Person $person): void {
+        foreach ($person->getPersonFiles() as $personFile) {
+            $this->entityManager->remove($personFile);
+        }
+
+        $this->entityManager->remove($person);
+    }
+
+    protected function deleteNetworkOrganizationsChildren(Organization $organization): void {
+        foreach ($organization->getNetworkOrganizationChildren() as $networkOrganization) {
+            $this->entityManager->remove($networkOrganization);
+        }
+    }
+
+    protected function deleteParameters(Organization $organization): void {
+        $this->entityManager->remove($organization->getParameters());
+    }
+
+    // TODO: nettoyer les fichiers sur le disque aussi
+    protected function deleteOrganizationFiles(Organization $organization): void {
+        foreach ($organization->getFiles() as $file) {
+            $this->entityManager->remove($file);
+        }
+    }
+
+    // TODO: à revoir, c'est du many to many
+//    protected function removeTypeOfPractices(Organization $organization): void {
+//        foreach ($organization->getTypeOfPractices() as $typeOfPractice) {
+//            $organization->removeTypeOfPractice($typeOfPractice);
+//        }
+//    }
+
+    // TODO: à revoir, c'est du many to many
+//    protected function deleteContactPoints(Organization $organization): void
+//    {
+//        foreach ($organization->getContactPoints() as $contactPoint) {
+//            $this->entityManager->remove($contactPoint);
+//        }
+//    }
+
+    // TODO: à revoir, c'est du many to many
+//    protected function deleteBankAccounts(Organization $organization): void {
+//        foreach ($organization->getBankAccounts() as $bankAccount) {
+//            $this->entityManager->remove($bankAccount);
+//        }
+//    }
+
+    protected function deleteTypo3Website(Organization $organization): void
+    {
+        $this->typo3Service->deleteSite($organization->getId());
+    }
+
+    protected function deleteDolibarrSociety(Organization $organization): void
+    {
+        // TODO: implement
+    }
+
+    protected function deleteDirectories(Organization $organization): void
+    {
+        // TODO: implement
+    }
+
+
+
+
 }