55namespace PhpList \Core \Domain \Subscription \Service ;
66
77use Doctrine \ORM \EntityManagerInterface ;
8+ use PhpList \Core \Domain \Identity \Model \Administrator ;
89use PhpList \Core \Domain \Messaging \Message \SubscriptionConfirmationMessage ;
910use PhpList \Core \Domain \Subscription \Exception \CouldNotReadUploadedFileException ;
1011use PhpList \Core \Domain \Subscription \Model \Dto \ImportSubscriberDto ;
1314use PhpList \Core \Domain \Subscription \Repository \SubscriberAttributeDefinitionRepository ;
1415use PhpList \Core \Domain \Subscription \Repository \SubscriberRepository ;
1516use PhpList \Core \Domain \Subscription \Service \Manager \SubscriberAttributeManager ;
17+ use PhpList \Core \Domain \Subscription \Service \Manager \SubscriberHistoryManager ;
1618use PhpList \Core \Domain \Subscription \Service \Manager \SubscriberManager ;
1719use PhpList \Core \Domain \Subscription \Service \Manager \SubscriptionManager ;
1820use Symfony \Component \HttpFoundation \File \UploadedFile ;
@@ -35,6 +37,7 @@ class SubscriberCsvImporter
3537 private EntityManagerInterface $ entityManager ;
3638 private TranslatorInterface $ translator ;
3739 private MessageBusInterface $ messageBus ;
40+ private SubscriberHistoryManager $ subscriberHistoryManager ;
3841
3942 public function __construct (
4043 SubscriberManager $ subscriberManager ,
@@ -46,6 +49,7 @@ public function __construct(
4649 EntityManagerInterface $ entityManager ,
4750 TranslatorInterface $ translator ,
4851 MessageBusInterface $ messageBus ,
52+ SubscriberHistoryManager $ subscriberHistoryManager ,
4953 ) {
5054 $ this ->subscriberManager = $ subscriberManager ;
5155 $ this ->attributeManager = $ attributeManager ;
@@ -56,17 +60,18 @@ public function __construct(
5660 $ this ->entityManager = $ entityManager ;
5761 $ this ->translator = $ translator ;
5862 $ this ->messageBus = $ messageBus ;
63+ $ this ->subscriberHistoryManager = $ subscriberHistoryManager ;
5964 }
6065
6166 /**
6267 * Import subscribers from a CSV file.
6368 *
6469 * @param UploadedFile $file The uploaded CSV file
6570 * @param SubscriberImportOptions $options
71+ * @param ?Administrator $admin
6672 * @return array Import statistics
67- * @throws CouldNotReadUploadedFileException When the uploaded file cannot be read during import
6873 */
69- public function importFromCsv (UploadedFile $ file , SubscriberImportOptions $ options ): array
74+ public function importFromCsv (UploadedFile $ file , SubscriberImportOptions $ options, ? Administrator $ admin = null ): array
7075 {
7176 $ stats = [
7277 'created ' => 0 ,
@@ -88,7 +93,7 @@ public function importFromCsv(UploadedFile $file, SubscriberImportOptions $optio
8893
8994 foreach ($ result ['valid ' ] as $ dto ) {
9095 try {
91- $ this ->processRow ($ dto , $ options , $ stats );
96+ $ this ->processRow ($ dto , $ options , $ stats, $ admin );
9297 } catch (Throwable $ e ) {
9398 $ stats ['errors ' ][] = $ this ->translator ->trans (
9499 'Error processing %email%: %error% ' ,
@@ -118,11 +123,12 @@ public function importFromCsv(UploadedFile $file, SubscriberImportOptions $optio
118123 * @param UploadedFile $file The uploaded CSV file
119124 * @return array Import statistics
120125 */
121- public function importAndUpdateFromCsv (UploadedFile $ file , ?array $ listIds = [], bool $ dryRun = false ): array
126+ public function importAndUpdateFromCsv (UploadedFile $ file , Administrator $ admin , ?array $ listIds = [], bool $ dryRun = false ): array
122127 {
123128 return $ this ->importFromCsv (
124129 file: $ file ,
125- options: new SubscriberImportOptions (updateExisting: true , listIds: $ listIds , dryRun: $ dryRun )
130+ options: new SubscriberImportOptions (updateExisting: true , listIds: $ listIds , dryRun: $ dryRun ),
131+ admin: $ admin ,
126132 );
127133 }
128134
@@ -132,25 +138,23 @@ public function importAndUpdateFromCsv(UploadedFile $file, ?array $listIds = [],
132138 * @param UploadedFile $file The uploaded CSV file
133139 * @return array Import statistics
134140 */
135- public function importNewFromCsv (UploadedFile $ file , ?array $ listIds = [], bool $ dryRun = false ): array
141+ public function importNewFromCsv (UploadedFile $ file , Administrator $ admin , ?array $ listIds = [], bool $ dryRun = false ): array
136142 {
137143 return $ this ->importFromCsv (
138144 file: $ file ,
139- options: new SubscriberImportOptions (listIds: $ listIds , dryRun: $ dryRun )
145+ options: new SubscriberImportOptions (listIds: $ listIds , dryRun: $ dryRun ),
146+ admin: $ admin ,
140147 );
141148 }
142149
143150 /**
144151 * Process a single row from the CSV file.
145- *
146- * @param ImportSubscriberDto $dto
147- * @param SubscriberImportOptions $options
148- * @param array $stats Statistics to update
149152 */
150153 private function processRow (
151154 ImportSubscriberDto $ dto ,
152155 SubscriberImportOptions $ options ,
153156 array &$ stats ,
157+ ?Administrator $ admin = null
154158 ): void {
155159 if ($ this ->handleInvalidEmail ($ dto , $ options , $ stats )) {
156160 return ;
@@ -164,7 +168,7 @@ private function processRow(
164168 }
165169
166170 if ($ subscriber ) {
167- $ this ->subscriberManager ->updateFromImport ($ subscriber , $ dto );
171+ $ updatedData = $ this ->subscriberManager ->updateFromImport ($ subscriber , $ dto );
168172 $ stats ['updated ' ]++;
169173 } else {
170174 $ subscriber = $ this ->subscriberManager ->createFromImport ($ dto );
@@ -174,11 +178,13 @@ private function processRow(
174178 $ this ->processAttributes ($ subscriber , $ dto );
175179
176180 $ addedNewSubscriberToList = false ;
181+ $ listLines = [];
177182 if (!$ subscriber ->isBlacklisted () && count ($ options ->listIds ) > 0 ) {
178183 foreach ($ options ->listIds as $ listId ) {
179184 $ created = $ this ->subscriptionManager ->addSubscriberToAList ($ subscriber , $ listId );
180185 if ($ created ) {
181186 $ addedNewSubscriberToList = true ;
187+ $ listLines [] = sprintf ('Subscribed to %s ' , $ created ->getSubscriberList ()->getName ());
182188 }
183189 }
184190 }
@@ -187,6 +193,7 @@ private function processRow(
187193 $ stats ['blacklisted ' ]++;
188194 }
189195
196+ $ this ->subscriberHistoryManager ->addHistoryFromImport ($ subscriber , $ listLines , $ updatedData ?? [], $ admin );
190197 $ this ->handleFlushAndEmail ($ subscriber , $ options , $ dto , $ addedNewSubscriberToList );
191198 }
192199
@@ -219,23 +226,18 @@ private function handleFlushAndEmail(
219226 if (!$ options ->dryRun ) {
220227 $ this ->entityManager ->flush ();
221228 if ($ dto ->sendConfirmation && $ addedNewSubscriberToList ) {
222- $ this ->sendSubscribeEmail ($ subscriber , $ options ->listIds );
229+ $ message = new SubscriptionConfirmationMessage (
230+ email: $ subscriber ->getEmail (),
231+ uniqueId: $ subscriber ->getUniqueId (),
232+ listIds: $ options ->listIds ,
233+ htmlEmail: $ subscriber ->hasHtmlEmail (),
234+ );
235+
236+ $ this ->messageBus ->dispatch ($ message );
223237 }
224238 }
225239 }
226240
227- private function sendSubscribeEmail (Subscriber $ subscriber , array $ listIds ): void
228- {
229- $ message = new SubscriptionConfirmationMessage (
230- email: $ subscriber ->getEmail (),
231- uniqueId: $ subscriber ->getUniqueId (),
232- listIds: $ listIds ,
233- htmlEmail: $ subscriber ->hasHtmlEmail (),
234- );
235-
236- $ this ->messageBus ->dispatch ($ message );
237- }
238-
239241 /**
240242 * Process subscriber attributes.
241243 *
0 commit comments