55namespace PhpList \Core \Domain \Subscription \Service ;
66
77use Doctrine \ORM \EntityManagerInterface ;
8+ use PhpList \Core \Domain \Subscription \Exception \CouldNotReadUploadedFileException ;
89use PhpList \Core \Domain \Subscription \Model \Dto \ImportSubscriberDto ;
910use PhpList \Core \Domain \Subscription \Model \Dto \SubscriberImportOptions ;
1011use PhpList \Core \Domain \Subscription \Model \Subscriber ;
1314use PhpList \Core \Domain \Subscription \Service \Manager \SubscriberAttributeManager ;
1415use PhpList \Core \Domain \Subscription \Service \Manager \SubscriberManager ;
1516use PhpList \Core \Domain \Subscription \Service \Manager \SubscriptionManager ;
16- use RuntimeException ;
1717use Symfony \Component \HttpFoundation \File \UploadedFile ;
18+ use Symfony \Contracts \Translation \TranslatorInterface ;
1819use Throwable ;
1920
2021/**
@@ -30,6 +31,7 @@ class SubscriberCsvImporter
3031 private CsvImporter $ csvImporter ;
3132 private SubscriberAttributeDefinitionRepository $ attrDefinitionRepository ;
3233 private EntityManagerInterface $ entityManager ;
34+ private TranslatorInterface $ translator ;
3335
3436 public function __construct (
3537 SubscriberManager $ subscriberManager ,
@@ -38,7 +40,8 @@ public function __construct(
3840 SubscriberRepository $ subscriberRepository ,
3941 CsvImporter $ csvImporter ,
4042 SubscriberAttributeDefinitionRepository $ attrDefinitionRepository ,
41- EntityManagerInterface $ entityManager
43+ EntityManagerInterface $ entityManager ,
44+ TranslatorInterface $ translator ,
4245 ) {
4346 $ this ->subscriberManager = $ subscriberManager ;
4447 $ this ->attributeManager = $ attributeManager ;
@@ -47,6 +50,7 @@ public function __construct(
4750 $ this ->csvImporter = $ csvImporter ;
4851 $ this ->attrDefinitionRepository = $ attrDefinitionRepository ;
4952 $ this ->entityManager = $ entityManager ;
53+ $ this ->translator = $ translator ;
5054 }
5155
5256 /**
@@ -55,7 +59,7 @@ public function __construct(
5559 * @param UploadedFile $file The uploaded CSV file
5660 * @param SubscriberImportOptions $options
5761 * @return array Import statistics
58- * @throws RuntimeException When the uploaded file cannot be read or for any other errors during import
62+ * @throws CouldNotReadUploadedFileException When the uploaded file cannot be read during import
5963 */
6064 public function importFromCsv (UploadedFile $ file , SubscriberImportOptions $ options ): array
6165 {
@@ -69,7 +73,9 @@ public function importFromCsv(UploadedFile $file, SubscriberImportOptions $optio
6973 try {
7074 $ path = $ file ->getRealPath ();
7175 if ($ path === false ) {
72- throw new RuntimeException ('Could not read the uploaded file. ' );
76+ throw new CouldNotReadUploadedFileException (
77+ $ this ->translator ->trans ('Could not read the uploaded file. ' )
78+ );
7379 }
7480
7581 $ result = $ this ->csvImporter ->import ($ path );
@@ -81,7 +87,9 @@ public function importFromCsv(UploadedFile $file, SubscriberImportOptions $optio
8187 $ this ->entityManager ->flush ();
8288 }
8389 } catch (Throwable $ e ) {
84- $ stats ['errors ' ][] = 'Error processing ' . $ dto ->email . ': ' . $ e ->getMessage ();
90+ $ stats ['errors ' ][] = $ this ->translator ->trans ('Error processing %email%: %error% ' ,
91+ ['%email% ' => $ dto ->email , '%error% ' => $ e ->getMessage ()]
92+ );
8593 $ stats ['skipped ' ]++;
8694 }
8795 }
@@ -91,7 +99,10 @@ public function importFromCsv(UploadedFile $file, SubscriberImportOptions $optio
9199 $ stats ['skipped ' ]++;
92100 }
93101 } catch (Throwable $ e ) {
94- $ stats ['errors ' ][] = 'General import error: ' . $ e ->getMessage ();
102+ $ stats ['errors ' ][] = $ this ->translator ->trans (
103+ 'General import error: %error% ' ,
104+ ['%error% ' => $ e ->getMessage ()]
105+ );
95106 }
96107
97108 return $ stats ;
0 commit comments