|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Sylius package. |
| 5 | + * |
| 6 | + * (c) Sylius Sp. z o.o. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Sylius\GridImportExport\Messenger\Handler; |
| 15 | + |
| 16 | +use Sylius\GridImportExport\Entity\ProcessInterface; |
| 17 | +use Sylius\GridImportExport\Factory\ProcessFactoryInterface; |
| 18 | +use Sylius\GridImportExport\Messenger\Command\CreateExportProcess; |
| 19 | +use Sylius\GridImportExport\Messenger\Command\ExportCommand; |
| 20 | +use Sylius\Resource\Doctrine\Persistence\RepositoryInterface; |
| 21 | +use Symfony\Component\Messenger\MessageBusInterface; |
| 22 | + |
| 23 | +class CreateExportProcessHandler |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @param RepositoryInterface<ProcessInterface> $processRepository |
| 27 | + * @param int<1, max> $batchSize |
| 28 | + */ |
| 29 | + public function __construct( |
| 30 | + public ProcessFactoryInterface $processFactory, |
| 31 | + public RepositoryInterface $processRepository, |
| 32 | + public MessageBusInterface $messageBus, |
| 33 | + public int $batchSize = 100, |
| 34 | + ) { |
| 35 | + } |
| 36 | + |
| 37 | + public function __invoke(CreateExportProcess $command): void |
| 38 | + { |
| 39 | + $process = $this->processFactory->createForExport($command); |
| 40 | + |
| 41 | + $this->processRepository->add($process); |
| 42 | + |
| 43 | + foreach (array_chunk($process->getResourceIds(), $this->batchSize) as $batch) { |
| 44 | + $this->messageBus->dispatch(new ExportCommand( |
| 45 | + processId: $process->getUuid(), |
| 46 | + resourceIds: $batch, |
| 47 | + )); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments