Skip to content

Commit 3ee2fdf

Browse files
committed
Import by foreign key
1 parent 6136ed7 commit 3ee2fdf

File tree

4 files changed

+318
-27
lines changed

4 files changed

+318
-27
lines changed

resources/translations/messages.en.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,10 @@ Thank you.</target>
730730
<source>Campaign not found or not in submitted status</source>
731731
<target>__Campaign not found or not in submitted status</target>
732732
</trans-unit>
733+
<trans-unit id="TBYUW2m" resname="Conflict: email and foreign key refer to different subscribers.">
734+
<source>Conflict: email and foreign key refer to different subscribers.</source>
735+
<target>__Conflict: email and foreign key refer to different subscribers.</target>
736+
</trans-unit>
733737
</body>
734738
</file>
735739
</xliff>

src/Domain/Subscription/Service/CsvRowToDtoMapper.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ class CsvRowToDtoMapper
1616
public function map(array $row): ImportSubscriberDto
1717
{
1818
// Normalize keys to lower-case for header matching safety (CSV library keeps original headers)
19-
$normalizedRow = [];
20-
foreach ($row as $key => $value) {
21-
$normalizedRow[strtolower((string)$key)] = is_string($value) ? trim($value) : $value;
22-
}
19+
$normalizedRow = $this->normalizeData($row);
2320

2421
$email = strtolower(trim((string)($normalizedRow['email'] ?? '')));
2522

@@ -42,4 +39,14 @@ public function map(array $row): ImportSubscriberDto
4239
foreignKey: $foreignKey ?? null,
4340
);
4441
}
42+
43+
private function normalizeData(array $row): array
44+
{
45+
$normalizedRow = [];
46+
foreach ($row as $key => $value) {
47+
$normalizedRow[strtolower((string)$key)] = is_string($value) ? trim($value) : $value;
48+
}
49+
50+
return $normalizedRow;
51+
}
4552
}

src/Domain/Subscription/Service/SubscriberCsvImporter.php

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)