Ver código fonte

refactors context aware datapersisters

Olivier Massot 3 anos atrás
pai
commit
7907b45174

+ 43 - 9
src/DataPersister/BaseDataPersister.php

@@ -4,23 +4,57 @@ namespace App\DataPersister;
 
 use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
 
-class ContextAwareDataPersister implements ContextAwareDataPersisterInterface
+abstract class BaseDataPersister implements ContextAwareDataPersisterInterface
 {
-    public function __construct(
-        private ContextAwareDataPersisterInterface $decorated,
-    )
-    { }
+    private ContextAwareDataPersisterInterface $decorated;
+    protected array $context = [];
 
-    public function supports($data, array $context = []): bool
+    public function setDecorated(ContextAwareDataPersisterInterface $decorated): void
     {
-        return false;
+        $this->decorated = $decorated;
     }
 
+    abstract public function supports($data, array $context = []): bool;
+
     public function persist($data, array $context = [])
     {
+        $this->context = $context;
+
+        $data = $this->preProcess($data);
+
+        $this->prePersist($data);
+
+        $result = $this->decorated->persist($data, $context);
+
+        $this->postPersist($data);
+
+        return $result;
     }
 
-    public function remove($data, array $context = [])
-    {
+    protected function preProcess($data) {
+        return $data;
+    }
+
+    protected function prePersist($data): void {
+    }
+
+    protected function postPersist($data): void {
+    }
+
+    abstract public function remove($data, array $context = []): void;
+
+    protected function isPostRequest(): bool {
+        return $this->context['collection_operation_name'] ?? null === 'post';
+    }
+
+    protected function isPutRequest(): bool {
+        return $this->context['item_operation_name'] ?? null === 'put';
+    }
+
+    protected function previousData() {
+        if (!$this->isPutRequest()) {
+            return null;
+        }
+        return $this->context['previous_data'];
     }
 }

+ 10 - 20
src/DataPersister/Organization/OrganizationDataPersister.php

@@ -4,6 +4,7 @@ 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;
 use Exception;
@@ -11,13 +12,12 @@ use Exception;
 /**
  * Classe OrganizationDataPersister qui est un custom dataPersister gérant la resource Organization
  */
-class OrganizationDataPersister implements ContextAwareDataPersisterInterface
+class OrganizationDataPersister extends BaseDataPersister
 {
     public function __construct(
-        private ContextAwareDataPersisterInterface $decorated,
         private OnOrganizationChange $onOrganizationChange
     )
-    { }
+    {}
 
     public function supports($data, array $context = []): bool
     {
@@ -25,27 +25,17 @@ class OrganizationDataPersister implements ContextAwareDataPersisterInterface
     }
 
     /**
-     * @param Organization $data
-     * @param array $context
-     * @return object|void
+     * @param Organization $organization
      */
-    public function persist($data, array $context = [])
-    {
-        $this->prePersist($context['previous_data'], $data);
-
-        $result = $this->decorated->persist($data, $context);
-
-        return $result;
+    public function prePersist($organization): void{
+        if($this->previousData() && $this->previousData()->getLegalStatus() !== $organization->getLegalStatus()){
+            $this->onOrganizationChange->onLegalStatusChange($organization);
+        }
     }
 
-    public function remove($data, array $context = [])
+    public function remove($data, array $context = []): void
     {
         throw new Exception('not supported', 500);
     }
 
-    public function prePersist(Organization $previousOrganization, Organization $organization): void{
-        if($previousOrganization->getLegalStatus() != $organization->getLegalStatus()){
-            $this->onOrganizationChange->onLegalStatusChange($organization);
-        }
-    }
-}
+}

+ 57 - 30
src/DataPersister/Organization/ParametersDataPersister.php

@@ -4,23 +4,25 @@ 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;
 use App\Service\OnChange\Organization\OnParametersChange;
+use App\Service\Typo3\Typo3Service;
 use Exception;
 use Symfony\Component\Messenger\MessageBusInterface;
 
 /**
  * Classe ParametersDataPersister qui est un custom dataPersister gérant la resource Parameters
  */
-class ParametersDataPersister implements ContextAwareDataPersisterInterface
+class ParametersDataPersister extends BaseDataPersister
 {
     public function __construct(
-        private ContextAwareDataPersisterInterface $decorated,
         private MessageBusInterface $messageBus,
-        private OnParametersChange $onParametersChange
+        private OnParametersChange $onParametersChange,
+        private Typo3Service $typo3Service
     )
-    { }
+    {}
 
     public function supports($data, array $context = []): bool
     {
@@ -28,43 +30,68 @@ class ParametersDataPersister implements ContextAwareDataPersisterInterface
     }
 
     /**
-     * @param Parameters $data
-     * @param array $context
-     * @return object|void
+     * @param Parameters $parameters
+     * @throws Exception
      */
-    public function persist($data, array $context = [])
-    {
-        $this->prePersist($context['previous_data'], $data);
-
-        $result = $this->decorated->persist($data, $context);
-
-        $this->postPersist($context['previous_data'], $data);
-
-        return $result;
-    }
-
-    public function remove($data, array $context = [])
-    {
-        throw new Exception('not supported', 500);
-    }
-
-    public function prePersist(Parameters $previousParameters, Parameters $parameters): void{
-        if($previousParameters->getAdvancedEducationNotationType() != $parameters->getAdvancedEducationNotationType()){
+    public function prePersist($parameters): void{
+        if(
+            $this->previousData() &&
+            $this->previousData()->getAdvancedEducationNotationType() != $parameters->getAdvancedEducationNotationType()
+        ){
             $this->onParametersChange->onAdvancedEducationNotationTypeChange($parameters);
         }
 
         //La date de début d'activité change
-        if($previousParameters->getMusicalDate() != $parameters->getMusicalDate()){
-            $this->onParametersChange->onMusicalDateChange($parameters->getOrganization(), $previousParameters->getMusicalDate());
+        if(
+            $this->previousData() &&
+            $this->previousData()->getMusicalDate() != $parameters->getMusicalDate()
+        ){
+            $this->onParametersChange->onMusicalDateChange(
+                $parameters->getOrganization(),
+                $this->previousData()->getMusicalDate()
+            );
         }
     }
 
-    public function postPersist(Parameters $previousParameters, Parameters $parameters): void{
+    /**
+     * @param Parameters $parameters
+     */
+    public function postPersist($parameters): void{
         //La note maximale du suivi pédagogique change
-        if($previousParameters->getAverage() != $parameters->getAverage()){
+        if(
+            $this->previousData() &&
+            $this->previousData()->getAverage() != $parameters->getAverage()
+        ){
             $this->messageBus->dispatch(
                 new AverageChange($parameters->getId())
             );
         }
+
+        // Le custom domain a été modifié
+        if(
+            $this->previousData() &&
+            !$parameters->getDesactivateOpentalentSiteWeb() &&
+            $this->previousData()->getCustomDomain() !== $parameters->getCustomDomain()
+        ){
+            $this->typo3Service->updateSite($parameters->getOrganization()->getId());
+        }
+
+        // Le site web opentalent a été désactivé / réactivé
+        if(
+            $this->previousData() &&
+            $this->previousData()->getDesactivateOpentalentSiteWeb() !== $parameters->getDesactivateOpentalentSiteWeb()
+        ){
+            if ($parameters->getDesactivateOpentalentSiteWeb()) {
+                $this->typo3Service->deleteSite($parameters->getOrganization()->getId());
+            } else {
+                $this->typo3Service->undeleteSite($parameters->getOrganization()->getId());
+                $this->typo3Service->updateSite($parameters->getOrganization()->getId());
+            }
+        }
+    }
+
+    public function remove($data, array $context = []): void
+    {
+        throw new Exception('not supported', 500);
     }
-}
+}