Olivier Massot 10 months ago
parent
commit
1a366ea7d2

+ 6 - 5
src/ApiResources/Dolibarr/DolibarrDocDownload.php

@@ -12,8 +12,7 @@ use App\State\Provider\Dolibarr\DolibarrDocDownloadProvider;
 use Symfony\Component\Validator\Constraints as Assert;
 
 /**
- * Demande de téléchargement d'un fichier depuis Dolibarr (facture, bon de commande, etc)
- *
+ * Demande de téléchargement d'un fichier depuis Dolibarr (facture, bon de commande, etc).
  */
 #[ApiResource(
     operations: [
@@ -21,7 +20,7 @@ use Symfony\Component\Validator\Constraints as Assert;
             uriTemplate: '/dolibarr/download/{dolibarrDocType}/{ref}',
             requirements: [
                 'dolibarrDocType' => DolibarrDocTypeEnum::VALIDATION_REGEX,
-                'ref' => '[\w-]+'
+                'ref' => '[\w-]+',
             ],
             security: '(is_granted("ROLE_ADMIN_CORE") or 
                             is_granted("ROLE_ADMINISTRATIF_MANAGER_CORE") or 
@@ -48,8 +47,7 @@ class DolibarrDocDownload
 
     /**
      * The dolibarr reference of the document (ex: CO2502-0380, FA2101-02988, ...)
-     * Must be URL Encoded
-     * @var string
+     * Must be URL Encoded.
      */
     protected string $ref;
 
@@ -61,6 +59,7 @@ class DolibarrDocDownload
     public function setId(int $id): self
     {
         $this->id = $id;
+
         return $this;
     }
 
@@ -72,6 +71,7 @@ class DolibarrDocDownload
     public function setDolibarrDocType(DolibarrDocTypeEnum $dolibarrDocType): self
     {
         $this->dolibarrDocType = $dolibarrDocType;
+
         return $this;
     }
 
@@ -83,6 +83,7 @@ class DolibarrDocDownload
     public function setRef(string $ref): self
     {
         $this->ref = $ref;
+
         return $this;
     }
 }

+ 1 - 1
src/Enum/Dolibarr/DolibarrDocTypeEnum.php

@@ -17,5 +17,5 @@ enum DolibarrDocTypeEnum: string
     case INVOICE = 'invoice'; // Facture
     case CONTRACT = 'contract'; // Contrat
 
-    public const VALIDATION_REGEX = '^' . self::ORDER->value . '|' . self::INVOICE->value . '|' . self::CONTRACT->value . '$';
+    public const VALIDATION_REGEX = '^'.self::ORDER->value.'|'.self::INVOICE->value.'|'.self::CONTRACT->value.'$';
 }

+ 12 - 16
src/Service/Dolibarr/DolibarrApiService.php

@@ -223,28 +223,24 @@ class DolibarrApiService extends ApiRequestService
     }
 
     /**
-     * @param $type
-     * @param $docRef
-     * @return array<mixed>|null  The resulting document. Ex :
-     *                      {
-     *                          "filename": "CO2502-0380.pdf",
-     *                          "content-type": "application/pdf",
-     *                          "filesize": 10660,
-     *                          "content": "JVBERi0xLjcKJeLjz9MKNyAwIG9iago8PCAvV...",
-     *                          "encoding": "base64"
-     *                      }
+     * @return array<string, string|number> The resulting document. Ex :
+     *                                      {
+     *                                      "filename": "CO2502-0380.pdf",
+     *                                      "content-type": "application/pdf",
+     *                                      "filesize": 10660,
+     *                                      "content": "JVBERi0xLjcKJeLjz9MKNyAwIG9iago8PCAvV...",
+     *                                      "encoding": "base64"
+     *                                      }
+     *
+     * @throws \JsonException
      */
     public function downloadBillingDocPdf(string $type, string $docRef): array
     {
         if (!DolibarrDocTypeEnum::tryFrom($type)) {
-            throw new \InvalidArgumentException(sprintf(
-                'Invalid type "%s" provided. Allowed values are: %s',
-                $type,
-                implode(', ', array_map(fn($t) => $t->value, DolibarrDocTypeEnum::cases()))
-            ));
+            throw new \InvalidArgumentException(sprintf('Invalid type "%s" provided. Allowed values are: %s', $type, implode(', ', array_map(fn ($t) => $t->value, DolibarrDocTypeEnum::cases()))));
         }
 
-        $route = "documents/download?modulepart=".$type."&original_file=".urlencode("$docRef/$docRef.pdf");
+        $route = 'documents/download?modulepart='.$type.'&original_file='.urlencode("$docRef/$docRef.pdf");
 
         return $this->getJsonContent($route);
     }

+ 3 - 2
src/State/Provider/Dolibarr/DolibarrDocDownloadProvider.php

@@ -18,7 +18,8 @@ final readonly class DolibarrDocDownloadProvider implements ProviderInterface
 {
     public function __construct(
         private DolibarrApiService $dolibarrApiService,
-    ) {}
+    ) {
+    }
 
     /**
      * @param mixed[] $uriVariables
@@ -41,7 +42,7 @@ final readonly class DolibarrDocDownloadProvider implements ProviderInterface
 
         $response->headers->set('Charset', 'UTF-8');
         $response->headers->set('Access-Control-Expose-Headers', 'Content-Disposition');
-        $response->headers->set('Content-Type',$data['content-type']);
+        $response->headers->set('Content-Type', $data['content-type']);
 
         $response->headers->set(
             'Content-Disposition',