Explorar el Código

add SubdomainDataPersister

Olivier Massot hace 3 años
padre
commit
ffba7b08ea
Se han modificado 1 ficheros con 50 adiciones y 39 borrados
  1. 50 39
      src/DataPersister/Organization/SubdomainDataPersister.php

+ 50 - 39
src/DataPersister/Organization/SubdomainDataPersister.php

@@ -3,68 +3,79 @@ declare(strict_types=1);
 
 namespace App\DataPersister\Organization;
 
-use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
-use App\Entity\Organization\Parameters;
-use App\Message\Command\Parameters\AverageChange;
-use App\Service\OnChange\Organization\OnParametersChange;
+use App\DataPersister\BaseDataPersister;
+use App\Entity\Organization\Subdomain;
+use App\Service\Organization\Utils;
+use App\Service\Typo3\BindFileService;
+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 SubdomainDataPersister extends BaseDataPersister
 {
     public function __construct(
-        private ContextAwareDataPersisterInterface $decorated,
-        private MessageBusInterface $messageBus,
-        private OnParametersChange $onParametersChange
+        private Typo3Service $typo3Service,
+        private BindFileService $bindFileService
     )
-    { }
+    {}
 
     public function supports($data, array $context = []): bool
     {
-        return $data instanceof Parameters;
+        return $data instanceof Subdomain;
     }
 
     /**
-     * @param Parameters $data
-     * @param array $context
-     * @return object|void
+     * @param Subdomain $subdomain
      */
-    public function persist($data, array $context = [])
+    protected function prePersist($subdomain): void
     {
-        $this->prePersist($context['previous_data'], $data);
-
-        $result = $this->decorated->persist($data, $context);
-
-        $this->postPersist($context['previous_data'], $data);
-
-        return $result;
+        if ($this->isPostRequest()) {
+            // Ensure we do not exceed the limit of 3 subdomains per organization
+            if (count($subdomain->getOrganization()->getSubdomains()) >= 3) {
+                throw new \RuntimeException('This organization has already registered 3 subdomains');
+            }
+        }
     }
 
-    public function remove($data, array $context = [])
+    /**
+     * @param Subdomain $subdomain
+     */
+    protected function postPersist($subdomain): void
     {
-        throw new Exception('not supported', 500);
-    }
+        // Ensure it is the only active subdomain of this organization
+        if ($subdomain->isActive()) {
+            foreach ($subdomain->getOrganization()->getSubdomains() as $other) {
+                if ($other !== $subdomain) {
+                    $other->setActive(false);
+                }
+            }
+        }
 
-    public function prePersist(Parameters $previousParameters, Parameters $parameters): void{
-        if($previousParameters->getAdvancedEducationNotationType() != $parameters->getAdvancedEducationNotationType()){
-            $this->onParametersChange->onAdvancedEducationNotationTypeChange($parameters);
+        // Register into the BindFile
+        if ($this->isPostRequest()) {
+            $this->bindFileService->registerSubdomain($subdomain->getSubdomain());
         }
 
-        //La date de début d'activité change
-        if($previousParameters->getMusicalDate() != $parameters->getMusicalDate()){
-            $this->onParametersChange->onMusicalDateChange($parameters->getOrganization(), $previousParameters->getMusicalDate());
+        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 ($subdomain->isActive()) {
+                Utils::updateAdminUsername($subdomain->getOrganization());
+            }
         }
     }
 
-    public function postPersist(Parameters $previousParameters, Parameters $parameters): void{
-        //La note maximale du suivi pédagogique change
-        if($previousParameters->getAverage() != $parameters->getAverage()){
-            $this->messageBus->dispatch(
-                new AverageChange($parameters->getId())
-            );
-        }
+    public function remove($data, array $context = []): void
+    {
+        throw new Exception('not supported', 500);
     }
-}
+}