1414namespace Sylius \ImportExport \Messenger \Handler ;
1515
1616use Doctrine \ORM \EntityManagerInterface ;
17- use Sylius \ImportExport \Denormalizer \DenormalizerRegistryInterface ;
1817use Sylius \ImportExport \Entity \ImportProcessInterface ;
1918use Sylius \ImportExport \Exception \ImportFailedException ;
19+ use Sylius \ImportExport \Exception \ValidationFailedException ;
2020use Sylius \ImportExport \Messenger \Command \ImportCommand ;
21+ use Sylius \ImportExport \Processor \BatchProcessor ;
2122use Sylius \Resource \Doctrine \Persistence \RepositoryInterface ;
22- use Sylius \Resource \Metadata \RegistryInterface ;
23- use Symfony \Component \Validator \Validator \ValidatorInterface ;
2423
2524class ImportCommandHandler
2625{
2726 /** @param RepositoryInterface<ImportProcessInterface> $processRepository */
2827 public function __construct (
2928 protected RepositoryInterface $ processRepository ,
30- protected DenormalizerRegistryInterface $ denormalizerRegistry ,
3129 protected EntityManagerInterface $ entityManager ,
32- protected RegistryInterface $ metadataRegistry ,
33- protected ValidatorInterface $ validator ,
30+ protected BatchProcessor $ batchProcessor ,
3431 ) {
3532 }
3633
@@ -42,42 +39,32 @@ public function __invoke(ImportCommand $command): void
4239 }
4340
4441 try {
45- $ importedCount = 0 ;
46- $ resourceMetadata = $ this ->metadataRegistry ->get ($ process ->getResource ());
47- $ resourceClass = $ resourceMetadata ->getClass ('model ' );
48- $ denormalizer = $ this ->denormalizerRegistry ->get ($ resourceClass );
49-
50- foreach ($ command ->batchData as $ recordData ) {
51- $ entity = $ denormalizer ->denormalize ($ recordData , $ resourceClass );
52-
53- $ validationGroups = $ process ->getParameters ()['validation_groups ' ] ?? ['Default ' ];
54- $ violations = $ this ->validator ->validate ($ entity , groups: $ validationGroups );
55-
56- if (count ($ violations ) > 0 ) {
57- $ errorMessages = [];
58- foreach ($ violations as $ violation ) {
59- $ errorMessages [] = sprintf ('%s: %s ' , $ violation ->getPropertyPath (), $ violation ->getMessage ());
60- }
61-
62- throw new ImportFailedException (sprintf ('Validation failed for record: %s ' , implode (', ' , $ errorMessages )));
63- }
64-
65- $ this ->entityManager ->persist ($ entity );
66-
67- ++$ importedCount ;
68- }
69-
70- $ this ->entityManager ->flush ();
42+ $ importedCount = $ this ->batchProcessor ->processBatch ($ process , $ command ->batchData );
7143
7244 $ process ->setBatchesCount ($ process ->getBatchesCount () - 1 );
7345 $ process ->setImportedCount ($ process ->getImportedCount () + $ importedCount );
7446
7547 if ($ process ->getBatchesCount () <= 0 ) {
7648 $ process ->setStatus ('success ' );
7749 }
50+
51+ $ this ->entityManager ->persist ($ process );
52+ $ this ->entityManager ->flush ();
53+ } catch (ValidationFailedException $ e ) {
54+ $ this ->entityManager ->clear ();
55+ $ process = $ this ->processRepository ->find ($ command ->processId );
56+ if (null === $ process ) {
57+ throw new ImportFailedException (sprintf ('Process with uuid "%s" not found after validation failure. ' , $ command ->processId ));
58+ }
59+ $ process ->setStatus ('failed ' );
60+ $ process ->setErrorMessage ($ e ->getMessage ());
61+ $ this ->entityManager ->persist ($ process );
62+ $ this ->entityManager ->flush ();
7863 } catch (\Throwable $ e ) {
7964 $ process ->setStatus ('failed ' );
8065 $ process ->setErrorMessage ($ e ->getMessage ());
66+
67+ $ this ->entityManager ->flush ();
8168 }
8269 }
8370}
0 commit comments