ExportHandler.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Message\Handler;
  4. use App\Message\Command\Export;
  5. use App\Service\ServiceIterator\ExporterIterator;
  6. use Symfony\Component\Mercure\HubInterface;
  7. use Symfony\Component\Mercure\Update;
  8. use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
  9. use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
  10. class ExportHandler implements MessageHandlerInterface
  11. {
  12. public function __construct(
  13. private ExporterIterator $handler,
  14. private HubInterface $mercureHub
  15. ) {}
  16. public function __invoke(Export $export)
  17. {
  18. $exportRequest = $export->getExportRequest();
  19. try {
  20. $exportService = $this->handler->getExporterFor($exportRequest);
  21. $file = $exportService->export($exportRequest);
  22. $update = new Update('files', json_encode(['url' => 'https://my.download.url/' . $file->getId()]));
  23. $this->mercureHub->publish($update);
  24. } catch (\Exception $e) {
  25. // To prevent Messenger from retrying
  26. throw new UnrecoverableMessageHandlingException($e->getMessage(), $e->getCode(), $e);
  27. }
  28. }
  29. }