|
14 | 14 | namespace Sylius\GridImportExport\Messenger\Handler; |
15 | 15 |
|
16 | 16 | use Sylius\GridImportExport\Entity\ProcessInterface; |
| 17 | +use Sylius\GridImportExport\Exporter\CsvExporter; |
| 18 | +use Sylius\GridImportExport\Exporter\JsonExporter; |
17 | 19 | use Sylius\GridImportExport\Factory\ProcessFactoryInterface; |
18 | 20 | use Sylius\GridImportExport\Messenger\Command\CreateExportProcess; |
19 | 21 | use Sylius\GridImportExport\Messenger\Command\ExportCommand; |
| 22 | +use Sylius\GridImportExport\Messenger\Stamp\ExportBatchCounterStamp; |
20 | 23 | use Sylius\Resource\Doctrine\Persistence\RepositoryInterface; |
21 | 24 | use Symfony\Component\Messenger\MessageBusInterface; |
22 | 25 |
|
23 | 26 | class CreateExportProcessHandler |
24 | 27 | { |
| 28 | + private const FILE_BASED_FORMATS = [ |
| 29 | + CsvExporter::FORMAT, |
| 30 | + JsonExporter::FORMAT, |
| 31 | + ]; |
| 32 | + |
| 33 | + protected string $temporaryExportDirectory; |
| 34 | + |
25 | 35 | /** |
26 | 36 | * @param RepositoryInterface<ProcessInterface> $processRepository |
27 | 37 | * @param int<1, max> $batchSize |
28 | 38 | */ |
29 | 39 | public function __construct( |
30 | | - public ProcessFactoryInterface $processFactory, |
31 | | - public RepositoryInterface $processRepository, |
32 | | - public MessageBusInterface $messageBus, |
33 | | - public int $batchSize = 100, |
| 40 | + protected ProcessFactoryInterface $processFactory, |
| 41 | + protected RepositoryInterface $processRepository, |
| 42 | + protected MessageBusInterface $messageBus, |
| 43 | + string $exportDirectory, |
| 44 | + protected int $batchSize = 100, |
34 | 45 | ) { |
| 46 | + $this->temporaryExportDirectory = sprintf('%s/ongoing/', $exportDirectory); |
35 | 47 | } |
36 | 48 |
|
37 | 49 | public function __invoke(CreateExportProcess $command): void |
38 | 50 | { |
39 | 51 | $process = $this->processFactory->createExportProcess($command); |
40 | 52 |
|
| 53 | + if (in_array($command->format, self::FILE_BASED_FORMATS)) { |
| 54 | + $ongoingProcessDirectory = $this->temporaryExportDirectory . '/' . $process->getUuid(); |
| 55 | + $process->setTemporaryDirectory($ongoingProcessDirectory); |
| 56 | + |
| 57 | + if (!is_dir($ongoingProcessDirectory)) { |
| 58 | + mkdir($ongoingProcessDirectory, recursive: true); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + $batchesCount = (int) ceil(count($process->getResourceIds()) / $this->batchSize); |
| 63 | + $process->setBatchesCount($batchesCount); |
| 64 | + |
41 | 65 | $this->processRepository->add($process); |
42 | 66 |
|
43 | 67 | foreach (array_chunk($process->getResourceIds(), $this->batchSize) as $batch) { |
44 | 68 | $this->messageBus->dispatch(new ExportCommand( |
45 | 69 | processId: $process->getUuid(), |
46 | 70 | resourceIds: $batch, |
47 | | - )); |
| 71 | + ), [new ExportBatchCounterStamp()]); |
48 | 72 | } |
49 | 73 | } |
50 | 74 | } |
0 commit comments