瀏覽代碼

refactor data persisters and fix subdomainDataPersister

Olivier Massot 3 年之前
父節點
當前提交
128a9c326f

+ 2 - 6
config/services.yaml

@@ -10,6 +10,8 @@ services:
         bind:
             $opentalentConfig: '%kernel.project_dir%%env(OPENTALENT_CONFIG)%'
             $internalFilesUploadUri: '%env(INTERNAL_FILES_DOWNLOAD_URI)%'
+            $bindfileBufferFile: '%env(BINDFILE_BUFFER_FILE)'
+            $contextAwareDataPersister: '@api_platform.doctrine.orm.data_persister'
 
     # Logging: a shorter version of the default monolog line formatter
     monolog.formatter.message:
@@ -90,9 +92,3 @@ services:
     App\EventListener\DoctrineFilter\DoctrineFilterListener:
         tags:
             - { name: kernel.event_listener, event: kernel.request }
-
-    #########################################
-    ##  DATAPERSISTER ##
-    App\DataPersister\BaseDataPersister:
-        calls:
-            - setDecorated: ['@api_platform.doctrine.orm.data_persister']

+ 18 - 8
src/DataPersister/BaseDataPersister.php

@@ -3,19 +3,27 @@
 namespace App\DataPersister;
 
 use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
+use Doctrine\ORM\EntityManagerInterface;
+use Symfony\Contracts\Service\Attribute\Required;
 
-abstract class BaseDataPersister implements ContextAwareDataPersisterInterface
+class BaseDataPersister implements ContextAwareDataPersisterInterface
 {
-    private ContextAwareDataPersisterInterface $decorated;
     protected array $context = [];
 
-    public function setDecorated(ContextAwareDataPersisterInterface $decorated): void
+    // dependencies injections
+    protected EntityManagerInterface $entityManager;
+    protected ContextAwareDataPersisterInterface $contextAwareDataPersister;
+
+    #[Required]
+    public function setContextAwareDataPersister(ContextAwareDataPersisterInterface $contextAwareDataPersister): void {$this->contextAwareDataPersister = $contextAwareDataPersister;}
+    #[Required]
+    public function setEntityManager(EntityManagerInterface $entityManager): void {$this->entityManager = $entityManager;}
+
+    public function supports($data, array $context = []): bool
     {
-        $this->decorated = $decorated;
+        return false;
     }
 
-    abstract public function supports($data, array $context = []): bool;
-
     public function persist($data, array $context = [])
     {
         $this->context = $context;
@@ -24,7 +32,7 @@ abstract class BaseDataPersister implements ContextAwareDataPersisterInterface
 
         $this->prePersist($data);
 
-        $result = $this->decorated->persist($data, $context);
+        $result = $this->contextAwareDataPersister->persist($data, $context);
 
         $this->postPersist($data);
 
@@ -41,7 +49,9 @@ abstract class BaseDataPersister implements ContextAwareDataPersisterInterface
     protected function postPersist($data): void {
     }
 
-    abstract public function remove($data, array $context = []): void;
+    public function remove($data, array $context = []): void {
+        throw new \RuntimeException('not supported', 500);
+    }
 
     protected function isPostRequest(): bool {
         return $this->context['collection_operation_name'] ?? null === 'post';

+ 2 - 2
src/DataPersister/Organization/OrganizationDataPersister.php

@@ -3,7 +3,6 @@ declare(strict_types=1);
 
 namespace App\DataPersister\Organization;
 
-use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
 use App\DataPersister\BaseDataPersister;
 use App\Entity\Organization\Organization;
 use App\Service\OnChange\Organization\OnOrganizationChange;
@@ -17,7 +16,8 @@ class OrganizationDataPersister extends BaseDataPersister
     public function __construct(
         private OnOrganizationChange $onOrganizationChange
     )
-    {}
+    {
+    }
 
     public function supports($data, array $context = []): bool
     {

+ 0 - 1
src/DataPersister/Organization/ParametersDataPersister.php

@@ -3,7 +3,6 @@ declare(strict_types=1);
 
 namespace App\DataPersister\Organization;
 
-use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
 use App\DataPersister\BaseDataPersister;
 use App\Entity\Organization\Parameters;
 use App\Message\Command\Parameters\AverageChange;

+ 22 - 12
src/DataPersister/Organization/SubdomainDataPersister.php

@@ -19,7 +19,8 @@ class SubdomainDataPersister extends BaseDataPersister
         private Typo3Service $typo3Service,
         private BindFileService $bindFileService
     )
-    {}
+    {
+    }
 
     public function supports($data, array $context = []): bool
     {
@@ -41,14 +42,18 @@ class SubdomainDataPersister extends BaseDataPersister
 
     /**
      * @param Subdomain $subdomain
+     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
      */
     protected function postPersist($subdomain): void
     {
+        $shallPersist = false;
+
         // Ensure it is the only active subdomain of this organization
         if ($subdomain->isActive()) {
             foreach ($subdomain->getOrganization()->getSubdomains() as $other) {
-                if ($other !== $subdomain) {
+                if ($other !== $subdomain && $other->isActive()) {
                     $other->setActive(false);
+                    $shallPersist = true;
                 }
             }
         }
@@ -58,18 +63,23 @@ class SubdomainDataPersister extends BaseDataPersister
             $this->bindFileService->registerSubdomain($subdomain->getSubdomain());
         }
 
-        if ($this->isPutRequest()) {
-            // Update the typo3 website
-            if (
-                $this->previousData() &&
-                $subdomain->isActive() && !$this->previousData()->isActive()
-            ) {
-                $this->typo3Service->updateSite($subdomain->getOrganization()->getId());
+        // Update the admin username
+        if ($this->isPutRequest() && $this->previousData()) {
+            if ($subdomain->isActive() && !$this->previousData()->isActive()) {
+                Utils::updateAdminUsername($subdomain->getOrganization());
+                $shallPersist = true;
             }
+        }
 
-            // update the admin username
-            if ($subdomain->isActive()) {
-                Utils::updateAdminUsername($subdomain->getOrganization());
+        if ($shallPersist) {
+            $this->entityManager->flush();
+        }
+
+        // Update the typo3 website
+        // /!\ This has to be executed after everything has been persisted
+        if ($this->isPutRequest() && $this->previousData()) {
+            if ($subdomain->isActive() && !$this->previousData()->isActive()) {
+                $this->typo3Service->updateSite($subdomain->getOrganization()->getId());
             }
         }
     }

+ 1 - 1
src/Entity/Organization/Subdomain.php

@@ -33,7 +33,7 @@ use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
 #[ORM\Entity(repositoryClass: SubdomainRepository::class)]
 #[OrganizationDefaultValue(fieldName: "organization")]
 #[ApiFilter(SearchFilter::class, properties: ['subdomain' => 'exact'])]
-#[UniqueEntity('email')]
+#[UniqueEntity('subdomain')]
 class Subdomain
 {
     #[ORM\Id]