@@ -182,20 +182,14 @@ private function processRow(
182182 return null ;
183183 }
184184
185- $ subscriberByEmail = $ this ->subscriberRepository ->findOneByEmail ($ dto ->email );
186- $ subscriberByFk = null ;
187- if ($ dto ->foreignKey !== null ) {
188- $ subscriberByFk = $ this ->subscriberRepository ->findOneByForeignKey ($ dto ->foreignKey );
189- }
185+ [$ subscriber , $ conflictError ] = $ this ->resolveSubscriber ($ dto );
190186
191- if ($ subscriberByEmail && $ subscriberByFk && $ subscriberByEmail -> getId () !== $ subscriberByFk -> getId () ) {
187+ if ($ conflictError !== null ) {
192188 $ stats ['skipped ' ]++;
193- $ stats ['errors ' ][] = $ this -> translator -> trans ( ' Conflict: email and foreign key refer to different subscribers. ' ) ;
189+ $ stats ['errors ' ][] = $ conflictError ;
194190 return null ;
195191 }
196192
197- $ subscriber = $ subscriberByFk ?? $ subscriberByEmail ;
198-
199193 if ($ this ->handleSkipCase ($ subscriber , $ options , $ stats )) {
200194 return null ;
201195 }
@@ -210,20 +204,7 @@ private function processRow(
210204
211205 $ this ->attributeManager ->processAttributes ($ subscriber , $ dto ->extraAttributes );
212206
213- $ addedNewSubscriberToList = false ;
214- $ listLines = [];
215- if (!$ subscriber ->isBlacklisted () && count ($ options ->listIds ) > 0 ) {
216- foreach ($ options ->listIds as $ listId ) {
217- $ created = $ this ->subscriptionManager ->addSubscriberToAList ($ subscriber , $ listId );
218- if ($ created ) {
219- $ addedNewSubscriberToList = true ;
220- $ listLines [] = $ this ->translator ->trans (
221- 'Subscribed to %list% ' ,
222- ['%list% ' => $ created ->getSubscriberList ()->getName ()]
223- );
224- }
225- }
226- }
207+ [$ listLines , $ addedNewSubscriberToList ] = $ this ->getHistoryListLines ($ subscriber , $ options );
227208
228209 if ($ subscriber ->isBlacklisted ()) {
229210 $ stats ['blacklisted ' ]++;
@@ -239,6 +220,22 @@ private function processRow(
239220 return $ this ->prepareConfirmationMessage ($ subscriber , $ options , $ dto , $ addedNewSubscriberToList );
240221 }
241222
223+ private function resolveSubscriber (ImportSubscriberDto $ dto ): array
224+ {
225+ $ byEmail = $ this ->subscriberRepository ->findOneByEmail ($ dto ->email );
226+ $ byFk = null ;
227+
228+ if ($ dto ->foreignKey !== null ) {
229+ $ byFk = $ this ->subscriberRepository ->findOneByForeignKey ($ dto ->foreignKey );
230+ }
231+
232+ if ($ byEmail && $ byFk && $ byEmail ->getId () !== $ byFk ->getId ()) {
233+ return [null , $ this ->translator ->trans ('Conflict: email and foreign key refer to different subscribers. ' )];
234+ }
235+
236+ return [$ byFk ?? $ byEmail , null ];
237+ }
238+
242239 private function handleInvalidEmail (
243240 ImportSubscriberDto $ dto ,
244241 SubscriberImportOptions $ options ,
@@ -290,4 +287,27 @@ private function prepareConfirmationMessage(
290287
291288 return null ;
292289 }
290+
291+ private function getHistoryListLines (Subscriber $ subscriber , SubscriberImportOptions $ options ): array
292+ {
293+ $ addedNewSubscriberToList = false ;
294+ $ listLines = [];
295+ if (!$ subscriber ->isBlacklisted () && count ($ options ->listIds ) > 0 ) {
296+ foreach ($ options ->listIds as $ listId ) {
297+ $ created = $ this ->subscriptionManager ->addSubscriberToAList ($ subscriber , $ listId );
298+ if ($ created ) {
299+ $ addedNewSubscriberToList = true ;
300+ $ listLines [] = $ this ->translator ->trans (
301+ 'Subscribed to %list% ' ,
302+ ['%list% ' => $ created ->getSubscriberList ()->getName ()]
303+ );
304+ }
305+ }
306+ }
307+
308+ return [
309+ $ listLines ,
310+ $ addedNewSubscriberToList ,
311+ ];
312+ }
293313}
0 commit comments