ExportRequestProcessor.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\State\Processor\Export\LicenceCmf;
  4. use ApiPlatform\Metadata\Delete;
  5. use ApiPlatform\Metadata\Operation;
  6. use ApiPlatform\State\ProcessorInterface;
  7. use App\ApiResources\Export\ExportRequest;
  8. use App\Entity\Access\Access;
  9. use App\Entity\Core\File;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use App\Message\Command\Export;
  12. use App\Service\ServiceIterator\ExporterIterator;
  13. use Exception;
  14. use RuntimeException;
  15. use Symfony\Component\Messenger\MessageBusInterface;
  16. use Symfony\Bundle\SecurityBundle\Security;
  17. class ExportRequestProcessor implements ProcessorInterface
  18. {
  19. public function __construct(
  20. private Security $security,
  21. private MessageBusInterface $messageBus,
  22. private ExporterIterator $handler
  23. ) {}
  24. /**
  25. * @param ExportRequest $exportRequest
  26. * @param Operation $operation
  27. * @param mixed[] $uriVariables
  28. * @param mixed[] $context
  29. * @return ExportRequest
  30. * @throws Exception
  31. */
  32. public function process(mixed $exportRequest, Operation $operation, array $uriVariables = [], array $context = []): ExportRequest
  33. {
  34. if($operation instanceof Delete){
  35. throw new RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
  36. }
  37. /** @var Access $access */
  38. $access = $this->security->getUser();
  39. $exportRequest->setRequesterId($access->getId());
  40. // Prepare the file record and attach its id to the export request
  41. $exporter = $this->handler->getExporterFor($exportRequest);
  42. $file = $exporter->prepareFile($exportRequest, true);
  43. $exportRequest->setFileId($file->getId());
  44. if (!$exportRequest->isAsync()) {
  45. $exporter->export($exportRequest);
  46. } else {
  47. // Send the export request to Messenger (@see App\Message\Handler\ExportHandler)
  48. $this->messageBus->dispatch(
  49. new Export($exportRequest)
  50. );
  51. }
  52. return $exportRequest;
  53. }
  54. }