Skip to content

Commit 1dbb679

Browse files
committed
Rename classes/functions
1 parent 033aa54 commit 1dbb679

File tree

5 files changed

+30
-25
lines changed

5 files changed

+30
-25
lines changed

config/services/mappers.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ services:
44
autoconfigure: true
55
public: false
66

7-
PhpList\Core\Domain\Subscription\Service\CsvRowToDtoMapper:
7+
PhpList\Core\Domain\Subscription\Service\ArrayToDtoMapper:
88
autowire: true
99
autoconfigure: true
1010

11-
PhpList\Core\Domain\Subscription\Service\CsvImporter:
11+
PhpList\Core\Domain\Subscription\Service\CsvToDtoImporter:
1212
autowire: true
1313
autoconfigure: true

src/Domain/Subscription/Service/CsvRowToDtoMapper.php renamed to src/Domain/Subscription/Service/ArrayToDtoMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use PhpList\Core\Domain\Subscription\Model\Dto\ImportSubscriberDto;
88

9-
class CsvRowToDtoMapper
9+
class ArrayToDtoMapper
1010
{
1111
private const FK_HEADER = 'foreignkey';
1212
private const KNOWN_HEADERS = [

src/Domain/Subscription/Service/CsvImporter.php renamed to src/Domain/Subscription/Service/CsvToDtoImporter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
use Symfony\Contracts\Translation\TranslatorInterface;
1212
use Throwable;
1313

14-
class CsvImporter
14+
class CsvToDtoImporter
1515
{
1616
public function __construct(
17-
private readonly CsvRowToDtoMapper $rowMapper,
17+
private readonly ArrayToDtoMapper $rowMapper,
1818
private readonly ValidatorInterface $validator,
1919
private readonly TranslatorInterface $translator,
2020
) {
@@ -25,7 +25,7 @@ public function __construct(
2525
* @return array{valid: ImportSubscriberDto[], errors: array<int, array<string>>}
2626
* @throws CsvException
2727
*/
28-
public function import(string $csvFilePath): array
28+
public function parseAndValidate(string $csvFilePath): array
2929
{
3030
$reader = Reader::createFromPath($csvFilePath, 'r');
3131
$reader->setHeaderOffset(0);

src/Domain/Subscription/Service/SubscriberCsvImporter.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
use Throwable;
2424

2525
/**
26-
* phpcs:ignore Generic.Commenting.Todo
27-
* @todo: check if dryRun will work (some function flush)
28-
* Service for importing subscribers from a CSV file.
26+
* Handles full subscriber import workflow, including DB transactions and message dispatching.
27+
*
28+
* Note: Although this lives in the Service namespace, it acts as an *application service* —
29+
* it orchestrates multiple domain services and manages transactions/flushes directly.
30+
* This is an intentional exception for this complex import use case.
31+
*
2932
* @SuppressWarnings("CouplingBetweenObjects")
3033
* @SuppressWarnings("ExcessiveParameterList")
3134
*/
@@ -35,7 +38,7 @@ class SubscriberCsvImporter
3538
private SubscriberAttributeManager $attributeManager;
3639
private SubscriptionManager $subscriptionManager;
3740
private SubscriberRepository $subscriberRepository;
38-
private CsvImporter $csvImporter;
41+
private CsvToDtoImporter $csvToDtoImporter;
3942
private EntityManagerInterface $entityManager;
4043
private TranslatorInterface $translator;
4144
private MessageBusInterface $messageBus;
@@ -46,7 +49,7 @@ public function __construct(
4649
SubscriberAttributeManager $attributeManager,
4750
SubscriptionManager $subscriptionManager,
4851
SubscriberRepository $subscriberRepository,
49-
CsvImporter $csvImporter,
52+
CsvToDtoImporter $csvToDtoImporter,
5053
EntityManagerInterface $entityManager,
5154
TranslatorInterface $translator,
5255
MessageBusInterface $messageBus,
@@ -56,7 +59,7 @@ public function __construct(
5659
$this->attributeManager = $attributeManager;
5760
$this->subscriptionManager = $subscriptionManager;
5861
$this->subscriberRepository = $subscriberRepository;
59-
$this->csvImporter = $csvImporter;
62+
$this->csvToDtoImporter = $csvToDtoImporter;
6063
$this->entityManager = $entityManager;
6164
$this->translator = $translator;
6265
$this->messageBus = $messageBus;
@@ -78,6 +81,7 @@ public function importFromCsv(
7881
'updated' => 0,
7982
'skipped' => 0,
8083
'blacklisted' => 0,
84+
'invalid_email' => 0,
8185
'errors' => [],
8286
];
8387

@@ -89,7 +93,7 @@ public function importFromCsv(
8993
);
9094
}
9195

92-
$result = $this->csvImporter->import($path);
96+
$result = $this->csvToDtoImporter->parseAndValidate($path);
9397

9498
foreach ($result['valid'] as $dto) {
9599
try {
@@ -244,11 +248,12 @@ private function handleInvalidEmail(
244248
if (!filter_var($dto->email, FILTER_VALIDATE_EMAIL)) {
245249
if ($options->skipInvalidEmail) {
246250
$stats['skipped']++;
251+
$stats['invalid_email']++;
252+
$stats['errors'][] = $this->translator->trans('Invalid email: %email%', ['%email%' => $dto->email]);
247253

248254
return true;
249255
}
250-
// phpcs:ignore Generic.Commenting.Todo
251-
// @todo: check
256+
252257
$dto->email = 'invalid_' . $dto->email;
253258
$dto->sendConfirmation = false;
254259
}

tests/Unit/Domain/Subscription/Service/SubscriberCsvImporterTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use PhpList\Core\Domain\Subscription\Model\SubscriberAttributeDefinition;
1313
use PhpList\Core\Domain\Subscription\Repository\SubscriberAttributeDefinitionRepository;
1414
use PhpList\Core\Domain\Subscription\Repository\SubscriberRepository;
15-
use PhpList\Core\Domain\Subscription\Service\CsvImporter;
15+
use PhpList\Core\Domain\Subscription\Service\CsvToDtoImporter;
1616
use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberAttributeManager;
1717
use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberHistoryManager;
1818
use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberManager;
@@ -29,7 +29,7 @@ class SubscriberCsvImporterTest extends TestCase
2929
private SubscriberManager&MockObject $subscriberManagerMock;
3030
private SubscriberAttributeManager&MockObject $attributeManagerMock;
3131
private SubscriberRepository&MockObject $subscriberRepositoryMock;
32-
private CsvImporter&MockObject $csvImporterMock;
32+
private CsvToDtoImporter&MockObject $csvImporterMock;
3333
private SubscriberAttributeDefinitionRepository&MockObject $attributeDefinitionRepositoryMock;
3434
private SubscriberCsvImporter $subject;
3535

@@ -39,7 +39,7 @@ protected function setUp(): void
3939
$this->attributeManagerMock = $this->createMock(SubscriberAttributeManager::class);
4040
$subscriptionManagerMock = $this->createMock(SubscriptionManager::class);
4141
$this->subscriberRepositoryMock = $this->createMock(SubscriberRepository::class);
42-
$this->csvImporterMock = $this->createMock(CsvImporter::class);
42+
$this->csvImporterMock = $this->createMock(CsvToDtoImporter::class);
4343
$this->attributeDefinitionRepositoryMock = $this->createMock(SubscriberAttributeDefinitionRepository::class);
4444
$entityManager = $this->createMock(EntityManagerInterface::class);
4545

@@ -48,7 +48,7 @@ protected function setUp(): void
4848
attributeManager: $this->attributeManagerMock,
4949
subscriptionManager: $subscriptionManagerMock,
5050
subscriberRepository: $this->subscriberRepositoryMock,
51-
csvImporter: $this->csvImporterMock,
51+
csvToDtoImporter: $this->csvImporterMock,
5252
entityManager: $entityManager,
5353
translator: new Translator('en'),
5454
messageBus: $this->createMock(MessageBusInterface::class),
@@ -108,7 +108,7 @@ public function testImportFromCsvCreatesNewSubscribers(): void
108108
$importDto2->extraAttributes = ['first_name' => 'Jane'];
109109

110110
$this->csvImporterMock
111-
->method('import')
111+
->method('parseAndValidate')
112112
->with($tempFile)
113113
->willReturn([
114114
'valid' => [$importDto1, $importDto2],
@@ -173,7 +173,7 @@ public function testImportFromCsvUpdatesExistingSubscribers(): void
173173
$importDto->extraAttributes = [];
174174

175175
$this->csvImporterMock
176-
->method('import')
176+
->method('parseAndValidate')
177177
->with($tempFile)
178178
->willReturn([
179179
'valid' => [$importDto],
@@ -244,7 +244,7 @@ public function testImportResolvesByForeignKeyWhenProvidedAndMatches(): void
244244
);
245245

246246
$this->csvImporterMock
247-
->method('import')
247+
->method('parseAndValidate')
248248
->with($tempFile)
249249
->willReturn([
250250
'valid' => [$dto],
@@ -309,7 +309,7 @@ public function testImportConflictWhenEmailAndForeignKeyReferToDifferentSubscrib
309309
);
310310

311311
$this->csvImporterMock
312-
->method('import')
312+
->method('parseAndValidate')
313313
->with($tempFile)
314314
->willReturn([
315315
'valid' => [$dto],
@@ -369,7 +369,7 @@ public function testImportResolvesByEmailWhenForeignKeyNotFound(): void
369369
);
370370

371371
$this->csvImporterMock
372-
->method('import')
372+
->method('parseAndValidate')
373373
->with($tempFile)
374374
->willReturn([
375375
'valid' => [$dto],
@@ -429,7 +429,7 @@ public function testImportCreatesNewWhenNeitherEmailNorForeignKeyFound(): void
429429
);
430430

431431
$this->csvImporterMock
432-
->method('import')
432+
->method('parseAndValidate')
433433
->with($tempFile)
434434
->willReturn([
435435
'valid' => [$dto],

0 commit comments

Comments
 (0)