Browse Source

export datapersister: implement iterable support to get service

Olivier Massot 3 years ago
parent
commit
fe47db5896

+ 4 - 4
src/ApiResources/Export/ExportRequest.php

@@ -32,9 +32,9 @@ abstract class ExportRequest
 
     /**
      * The access requesting this export
-     * @var Access
+     * @var Access|null
      */
-    protected Access $requester;
+    protected ?Access $requester = null;
 
     /**
      * @return int
@@ -61,9 +61,9 @@ abstract class ExportRequest
     }
 
     /**
-     * @return Access
+     * @return Access|null
      */
-    public function getRequester(): Access
+    public function getRequester(): ?Access
     {
         return $this->requester;
     }

+ 1 - 1
src/ApiResources/Export/LicenceCmf/LicenceCmfOrganizationER.php

@@ -1,10 +1,10 @@
 <?php
+declare(strict_types=1);
 
 namespace App\ApiResources\Export\LicenceCmf;
 
 use ApiPlatform\Core\Annotation\ApiResource;
 use App\ApiResources\Export\ExportRequest;
-use App\Service\Export\LicenceCmfExporter;
 use Symfony\Component\Validator\Constraints as Assert;
 
 /**

+ 47 - 0
src/DataPersister/Export/LicenceCmf/ExportRequestDataPersister.php

@@ -0,0 +1,47 @@
+<?php
+declare(strict_types=1);
+
+namespace App\DataPersister\Export\LicenceCmf;
+
+use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
+use App\ApiResources\Export\ExportRequest;
+use App\Entity\Access\Access;
+use App\Service\Export\ExporterHandler;
+use Exception;
+use Symfony\Component\Security\Core\Security;
+
+class ExportRequestDataPersister implements ContextAwareDataPersisterInterface
+{
+    public function __construct(
+        private Security $security,
+        private ExporterHandler $handler
+    ) {}
+
+    public function supports($data, array $context = []): bool
+    {
+        return $data instanceof ExportRequest;
+    }
+
+    /**
+     * @param $exportRequest ExportRequest Une requête d'export
+     * @param array $context
+     * @throws Exception
+     */
+    public function persist($exportRequest, array $context = [])
+    {
+        /** @var Access $access */
+        $access = $this->security->getUser();
+        $exportRequest->setRequester($access);
+
+        $exportService = $this->handler->getServiceFor($exportRequest);
+        $exportService->export($exportRequest);
+    }
+
+    /**
+     * @throws Exception
+     */
+    public function remove($data, array $context = [])
+    {
+        throw new Exception('not supported', 500);
+    }
+}

+ 0 - 44
src/DataPersister/Export/LicenceCmf/LicenceCmfOrganizationERDataPersister.php

@@ -1,44 +0,0 @@
-<?php
-
-namespace App\DataPersister\Export\LicenceCmf;
-
-use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
-use App\ApiResources\Export\LicenceCmf\LicenceCmfOrganizationER;
-use App\Entity\Access\Access;
-use App\Service\Export\LicenceCmfExporter;
-use Symfony\Component\Security\Core\Security;
-
-class LicenceCmfOrganizationERDataPersister implements ContextAwareDataPersisterInterface
-{
-    public function __construct(
-        private LicenceCmfExporter $licenceCmfExporter,
-        private Security $security,
-    ) {}
-
-    public function supports($data, array $context = []): bool
-    {
-        return $data instanceof LicenceCmfOrganizationER;
-    }
-
-    /**
-     * @param $licenceCmfExportRequest LicenceCmfOrganizationER Une requête d'export d'une LicenceCmfOrganization
-     * @param array $context
-     */
-    public function persist($licenceCmfExportRequest, array $context = [])
-    {
-        /** @var Access $access */
-        $access = $this->security->getUser();
-
-        $licenceCmfExportRequest->setRequester($access);
-
-        $this->licenceCmfExporter->export($licenceCmfExportRequest);
-    }
-
-    /**
-     * @throws \Exception
-     */
-    public function remove($data, array $context = [])
-    {
-        throw new \Exception('not supported', 500);
-    }
-}

+ 0 - 8
src/Service/Export/BaseExporter.php

@@ -1,8 +0,0 @@
-<?php
-
-namespace App\Service\Export;
-
-abstract class BaseExporter
-{
-    protected function export($exportRequest) {}
-}

+ 38 - 0
src/Service/Export/ExporterHandler.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace App\Service\Export;
+
+use App\ApiResources\Export\ExportRequest;
+use Exception;
+
+/**
+ * Permet d'itérer sur les services d'export
+ */
+class ExporterHandler
+{
+    /**
+     * Pour l'injection des services, voir config/services.yaml, section 'TAG Services'
+     * @param iterable $exportServices
+     */
+    public function __construct(
+        private iterable $exportServices,
+    ) {}
+
+    /**
+     * Itère sur les services d'export disponibles et
+     * retourne le premier qui supporte ce type de requête.
+     *
+     * @param ExportRequest $exportRequest
+     * @return ExporterInterface
+     * @throws Exception
+     */
+    public function getServiceFor(ExportRequest $exportRequest): ExporterInterface
+    {
+        /** @var ExporterInterface $exportService */
+        foreach ($this->exportServices as $exportService){
+            if($exportService->support($exportRequest))
+                return $exportService;
+        }
+        throw new Exception('no export service found for this export request');
+    }
+}

+ 27 - 0
src/Service/Export/ExporterInterface.php

@@ -0,0 +1,27 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Service\Export;
+
+use App\ApiResources\Export\ExportRequest;
+
+/**
+ * Classe de base des services d'export
+ */
+interface ExporterInterface
+{
+    /**
+     * Le service supporte-t-il ce type d'ExportRequest
+     * @param ExportRequest $exportRequest
+     * @return boolean
+     */
+    public function support($exportRequest): bool;
+
+    /**
+     * Exécute l'opération d'export correspondant à la requête passée
+     * en paramètre
+     *
+     * @param ExportRequest $exportRequest
+     */
+    public function export($exportRequest);
+}

+ 10 - 4
src/Service/Export/LicenceCmfExporter.php

@@ -1,8 +1,10 @@
 <?php
+declare(strict_types=1);
 
 namespace App\Service\Export;
 
-
+use App\ApiResources\Export\ExportRequest;
+use App\ApiResources\Export\LicenceCmf\LicenceCmfOrganizationER;
 use App\Service\Export\Model\LicenceCmf;
 use App\Enum\Access\FunctionEnum;
 use App\Repository\Access\AccessRepository;
@@ -15,7 +17,7 @@ use Twig\Environment;
 /**
  * Exporte la licence CMF de la structure ou du ou des access, au format demandé
  */
-class LicenceCmfExporter extends BaseExporter
+class LicenceCmfExporter implements ExporterInterface
 {
     const CMF_ID = 12097;
 
@@ -33,13 +35,18 @@ class LicenceCmfExporter extends BaseExporter
     )
     {}
 
+    public function support($exportRequest): bool
+    {
+        return $exportRequest instanceof LicenceCmfOrganizationER;
+    }
+
     /**
      *
      */
     public function export($exportRequest)
     {
         $organization = $exportRequest->getRequester()->getOrganization();
-        $currentYear = date('Y');
+        $currentYear = (int)date('Y');
 
         $model = new LicenceCmf();
         $model->setId($organization->getId());
@@ -57,7 +64,6 @@ class LicenceCmfExporter extends BaseExporter
         $logoId = $organization->getLogo()?->getId();
         if ($logoId) {
             $model->setLogoUri(
-//                $this->router->generate('ot_legacy_file_download', array('id' => $logoId))
                 rtrim($_SERVER['INTERNAL_FILES_DOWNLOAD_URI'], '/') . '/' . $logoId
             );
         }