| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?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\Message\Command\Export;
- use App\Service\ServiceIterator\ExporterIterator;
- use Exception;
- use Symfony\Component\Messenger\MessageBusInterface;
- use Symfony\Component\Security\Core\Security;
- use Symfony\Component\HttpFoundation\Response;
- class ExportRequestDataPersister implements ContextAwareDataPersisterInterface
- {
- public function __construct(
- private Security $security,
- private MessageBusInterface $messageBus,
- private ExporterIterator $handler
- ) {}
- public function supports($data, array $context = []): bool
- {
- return $data instanceof ExportRequest;
- }
- /**
- * @param $exportRequest ExportRequest Une requête d'export
- * @param array $context
- * @return Response
- * @throws Exception
- */
- public function persist($exportRequest, array $context = []): Response
- {
- /** @var Access $access */
- $access = $this->security->getUser();
- $exportRequest->setRequesterId($access->getId());
- if ($exportRequest->isAsync()) {
- // Send the export request to Messenger (@see App\Message\Handler\ExportHandler)
- $this->messageBus->dispatch(
- new Export($exportRequest)
- );
- return new Response(null, 204);
- }
- $exportService = $this->handler->getExporterFor($exportRequest);
- $file = $exportService->export($exportRequest);
- return new Response('File generated: ' . $file->getId(), 200);
- }
- /**
- * @throws Exception
- */
- public function remove($data, array $context = [])
- {
- throw new Exception('not supported', 500);
- }
- }
|