Skip to content

Commit 7e1337b

Browse files
committed
Fixes after CR
1 parent aec218d commit 7e1337b

File tree

6 files changed

+75
-34
lines changed

6 files changed

+75
-34
lines changed

config/config/messenger.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ framework:
1717
'Sylius\ImportExport\Messenger\Command\ExportCommand': export_command
1818
'Sylius\ImportExport\Messenger\Event\ExportProcessCompleted': export_event
1919
buses:
20+
sylius_import_export.import.command_bus:
21+
middleware:
22+
- doctrine_transaction
23+
- sylius_import_export.messenger.middleware.doctrine_flush
2024
sylius_import_export.export.command_bus:
2125
middleware:
2226
- doctrine_transaction
2327
- sylius_import_export.messenger.middleware.export_batch_tracking
28+
- sylius_import_export.messenger.middleware.doctrine_flush
2429
sylius_import_export.export.event_bus: ~

config/services.xml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,19 @@
4444
<tag name="messenger.middleware" />
4545
</service>
4646

47+
<service id="sylius_import_export.messenger.middleware.doctrine_flush" class="Sylius\ImportExport\Messenger\Middleware\DoctrineFlushMiddleware">
48+
<argument type="service" id="doctrine.orm.entity_manager" />
49+
50+
<tag name="messenger.middleware" />
51+
</service>
52+
4753
<service id="sylius_import_export.messenger.command_handler.create_import_process" class="Sylius\ImportExport\Messenger\Handler\CreateImportProcessHandler">
4854
<argument type="service" id="sylius_import_export.factory.process" />
4955
<argument type="service" id="sylius_import_export.repository.process_import" />
50-
<argument type="service" id="sylius.command_bus" />
56+
<argument type="service" id="sylius_import_export.import.command_bus" />
5157
<argument type="service" id="sylius_import_export.importer_resolver" />
5258

53-
<tag name="messenger.message_handler" bus="sylius.command_bus" />
59+
<tag name="messenger.message_handler" bus="sylius_import_export.import.command_bus" />
5460
</service>
5561

5662
<service id="sylius_import_export.validator.import" class="Sylius\ImportExport\Validator\ImportValidator">
@@ -69,7 +75,7 @@
6975
<argument type="service" id="doctrine.orm.entity_manager" />
7076
<argument type="service" id="sylius_import_export.processor.batch" />
7177

72-
<tag name="messenger.message_handler" bus="sylius.command_bus" />
78+
<tag name="messenger.message_handler" bus="sylius_import_export.import.command_bus" />
7379
</service>
7480

7581
<service id="sylius_import_export.twig.component.export_resource" class="Sylius\ImportExport\Twig\Component\ExportResourceFormComponent">
@@ -117,7 +123,7 @@
117123

118124
<service id="sylius_import_export.controller.import_action" class="Sylius\ImportExport\Controller\ImportAction" public="true">
119125
<argument type="service" id="form.factory" />
120-
<argument type="service" id="sylius.command_bus" />
126+
<argument type="service" id="sylius_import_export.import.command_bus" />
121127
<argument>%sylius_import_export.import.form_class%</argument>
122128
<argument type="service" id="sylius_import_export.uploader.import_file" />
123129
<argument type="service" id="sylius.resource_registry" />

src/Controller/ImportAction.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,14 @@ public function __invoke(Request $request, string $grid): Response
4545
$form->handleRequest($request);
4646

4747
if (!$form->isSubmitted() || !$form->isValid()) {
48-
if ($form->isSubmitted()) {
49-
$errors = [];
50-
foreach ($form->getErrors(true) as $error) {
51-
if ($error instanceof FormError) {
52-
$errors[] = $error->getMessage();
53-
}
48+
$errors = [];
49+
foreach ($form->getErrors(true) as $error) {
50+
if ($error instanceof FormError) {
51+
$errors[] = $error->getMessage();
5452
}
55-
$errorMessage = !empty($errors) ? implode(', ', $errors) : 'sylius_import_export.import_form_invalid';
56-
$session->getFlashBag()->add('error', $errorMessage);
57-
} else {
58-
$session->getFlashBag()->add('error', 'sylius_import_export.import_form_invalid');
5953
}
54+
$errorMessage = !empty($errors) ? implode(', ', $errors) : 'sylius_import_export.import_form_invalid';
55+
$session->getFlashBag()->add('error', $errorMessage);
6056

6157
return new RedirectResponse($request->headers->get('referer') ?? '/');
6258
}

src/Messenger/Handler/ImportCommandHandler.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ public function __invoke(ImportCommand $command): void
4747
if ($process->getBatchesCount() <= 0) {
4848
$process->setStatus('success');
4949
}
50-
51-
$this->entityManager->persist($process);
52-
$this->entityManager->flush();
5350
} catch (ValidationFailedException $e) {
5451
$this->entityManager->clear();
5552
$process = $this->processRepository->find($command->processId);
@@ -58,13 +55,9 @@ public function __invoke(ImportCommand $command): void
5855
}
5956
$process->setStatus('failed');
6057
$process->setErrorMessage($e->getMessage());
61-
$this->entityManager->persist($process);
62-
$this->entityManager->flush();
6358
} catch (\Throwable $e) {
6459
$process->setStatus('failed');
6560
$process->setErrorMessage($e->getMessage());
66-
67-
$this->entityManager->flush();
6861
}
6962
}
7063
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\ImportExport\Messenger\Middleware;
15+
16+
use Doctrine\ORM\EntityManagerInterface;
17+
use Symfony\Component\Messenger\Envelope;
18+
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
19+
use Symfony\Component\Messenger\Middleware\StackInterface;
20+
21+
final class DoctrineFlushMiddleware implements MiddlewareInterface
22+
{
23+
public function __construct(
24+
private EntityManagerInterface $entityManager,
25+
) {
26+
}
27+
28+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
29+
{
30+
$envelope = $stack->next()->handle($envelope, $stack);
31+
32+
if ($this->entityManager->getUnitOfWork()->size() > 0) {
33+
$this->entityManager->flush();
34+
}
35+
36+
return $envelope;
37+
}
38+
}

tests/Functional/Importing/ImportHandlerTest.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
use Sylius\ImportExport\Messenger\Command\ImportCommand;
2121
use Sylius\ImportExport\Messenger\Handler\ImportCommandHandler;
2222
use Sylius\Resource\Doctrine\Persistence\RepositoryInterface;
23+
use Symfony\Component\Messenger\Exception\HandlerFailedException;
24+
use Symfony\Component\Messenger\MessageBusInterface;
2325
use Symfony\Component\Uid\Uuid;
2426
use Tests\Sylius\ImportExport\Entity\Dummy;
2527
use Tests\Sylius\ImportExport\Functional\FunctionalTestCase;
2628

2729
final class ImportHandlerTest extends FunctionalTestCase
2830
{
29-
private ImportCommandHandler $handler;
31+
private MessageBusInterface $commandBus;
3032

3133
private string $importsDir;
3234

@@ -36,7 +38,7 @@ protected function setUp(): void
3638
{
3739
parent::setUp();
3840

39-
$this->handler = $this->getContainer()->get('sylius_import_export.messenger.command_handler.import');
41+
$this->commandBus = $this->getContainer()->get('sylius_import_export.import.command_bus');
4042
$this->importsDir = $this->getContainer()->getParameter('sylius_import_export.import_files_directory');
4143
$this->processRepository = $this->getContainer()->get('sylius_import_export.repository.process_import');
4244

@@ -63,7 +65,7 @@ public function it_imports_data_from_array(array $importData, int $expectedCount
6365
[],
6466
);
6567

66-
$this->handler->__invoke(new ImportCommand($processUuid, $importData));
68+
$this->commandBus->dispatch(new ImportCommand($processUuid, $importData));
6769

6870
$dummyRepository = $this->entityManager->getRepository(Dummy::class);
6971
$importedDummies = $dummyRepository->findAll();
@@ -90,7 +92,7 @@ public function it_imports_single_dummy_with_basic_data(): void
9092
$processUuid = (string) Uuid::v7();
9193
$process = $this->createImportProcess($processUuid, 'json', '/tmp/test.json', []);
9294

93-
$this->handler->__invoke(new ImportCommand($processUuid, $importData));
95+
$this->commandBus->dispatch(new ImportCommand($processUuid, $importData));
9496

9597
$dummyRepository = $this->entityManager->getRepository(Dummy::class);
9698
$dummy = $dummyRepository->findOneBy(['uuid' => 'test-uuid-1']);
@@ -122,7 +124,7 @@ public function it_imports_dummy_with_nested_config(): void
122124
$processUuid = (string) Uuid::v7();
123125
$process = $this->createImportProcess($processUuid, 'json', '/tmp/test.json', []);
124126

125-
$this->handler->__invoke(new ImportCommand($processUuid, $importData));
127+
$this->commandBus->dispatch(new ImportCommand($processUuid, $importData));
126128

127129
$dummyRepository = $this->entityManager->getRepository(Dummy::class);
128130
$dummy = $dummyRepository->findOneBy(['uuid' => 'test-uuid-2']);
@@ -152,7 +154,7 @@ public function it_handles_multiple_batches_correctly(): void
152154
['uuid' => 'batch1-1', 'text' => 'Batch 1 Item 1', 'counter' => 1, 'config' => []],
153155
['uuid' => 'batch1-2', 'text' => 'Batch 1 Item 2', 'counter' => 2, 'config' => []],
154156
];
155-
$this->handler->__invoke(new ImportCommand($processUuid, $batchData1));
157+
$this->commandBus->dispatch(new ImportCommand($processUuid, $batchData1));
156158

157159
$this->entityManager->refresh($process);
158160
$this->assertSame(2, $process->getImportedCount());
@@ -162,7 +164,7 @@ public function it_handles_multiple_batches_correctly(): void
162164
$batchData2 = [
163165
['uuid' => 'batch2-1', 'text' => 'Batch 2 Item 1', 'counter' => 3, 'config' => []],
164166
];
165-
$this->handler->__invoke(new ImportCommand($processUuid, $batchData2));
167+
$this->commandBus->dispatch(new ImportCommand($processUuid, $batchData2));
166168

167169
$this->entityManager->refresh($process);
168170
$this->assertSame(3, $process->getImportedCount());
@@ -172,7 +174,7 @@ public function it_handles_multiple_batches_correctly(): void
172174
$batchData3 = [
173175
['uuid' => 'batch3-1', 'text' => 'Batch 3 Item 1', 'counter' => 4, 'config' => []],
174176
];
175-
$this->handler->__invoke(new ImportCommand($processUuid, $batchData3));
177+
$this->commandBus->dispatch(new ImportCommand($processUuid, $batchData3));
176178

177179
$this->entityManager->refresh($process);
178180
$this->assertSame(4, $process->getImportedCount());
@@ -204,9 +206,10 @@ public function it_handles_validation_errors(): void
204206
],
205207
];
206208

207-
$this->handler->__invoke(new ImportCommand($processUuid, $invalidData));
209+
$this->commandBus->dispatch(new ImportCommand($processUuid, $invalidData));
208210

209-
$process = $this->processRepository->find($processUuid);
211+
$processRepository = $this->getContainer()->get('sylius_import_export.repository.process_import');
212+
$process = $processRepository->find($processUuid);
210213
$this->assertSame('failed', $process->getStatus());
211214
$this->assertNotNull($process->getErrorMessage());
212215
$this->assertStringContainsString('Validation failed', $process->getErrorMessage());
@@ -232,7 +235,7 @@ public function it_uses_custom_validation_groups(): void
232235
],
233236
];
234237

235-
$this->handler->__invoke(new ImportCommand($processUuid, $importData));
238+
$this->commandBus->dispatch(new ImportCommand($processUuid, $importData));
236239

237240
$this->entityManager->refresh($process);
238241
$this->assertSame(1, $process->getImportedCount());
@@ -242,10 +245,10 @@ public function it_uses_custom_validation_groups(): void
242245
#[Test]
243246
public function it_handles_process_not_found_error(): void
244247
{
245-
$this->expectException(\Sylius\ImportExport\Exception\ImportFailedException::class);
248+
$this->expectException(HandlerFailedException::class);
246249
$this->expectExceptionMessage('Process with uuid "non-existent-uuid" not found.');
247250

248-
$this->handler->__invoke(new ImportCommand('non-existent-uuid', []));
251+
$this->commandBus->dispatch(new ImportCommand('non-existent-uuid', []));
249252
}
250253

251254
public static function getImportData(): array

0 commit comments

Comments
 (0)