DolibarrDocDownloadProvider.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\State\Provider\Dolibarr;
  4. use ApiPlatform\Metadata\GetCollection;
  5. use ApiPlatform\Metadata\Operation;
  6. use ApiPlatform\State\ProviderInterface;
  7. use App\Service\Dolibarr\DolibarrApiService;
  8. use Symfony\Component\HttpFoundation\HeaderUtils;
  9. use Symfony\Component\HttpFoundation\Response;
  10. /**
  11. * Custom provider pour les DolibarrAccounts récupérés via l'api dolibarr.
  12. */
  13. final readonly class DolibarrDocDownloadProvider implements ProviderInterface
  14. {
  15. public function __construct(
  16. private DolibarrApiService $dolibarrApiService,
  17. ) {}
  18. /**
  19. * @param mixed[] $uriVariables
  20. * @param mixed[] $context
  21. */
  22. public function provide(Operation $operation, array $uriVariables = [], array $context = []): Response
  23. {
  24. if ($operation instanceof GetCollection) {
  25. throw new \RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
  26. }
  27. $data = $this->dolibarrApiService->downloadBillingDocPdf(
  28. $uriVariables['dolibarrDocType'],
  29. $uriVariables['ref']
  30. );
  31. // Build the response and attach the file to it
  32. // @see https://symfony.com/doc/current/components/http_foundation.html#serving-files
  33. $response = new Response(base64_decode($data['content']));
  34. $response->headers->set('Charset', 'UTF-8');
  35. $response->headers->set('Access-Control-Expose-Headers', 'Content-Disposition');
  36. $response->headers->set('Content-Type',$data['content-type']);
  37. $response->headers->set(
  38. 'Content-Disposition',
  39. HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, $data['filename'])
  40. );
  41. return $response;
  42. }
  43. }