Skip to content

Commit c07be31

Browse files
committed
CouldNotReadUploadedFileException
1 parent 404eb03 commit c07be31

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

resources/translations/messages.en.xlf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,18 @@
424424
<source>Added to blacklist for reason %reason%</source>
425425
<target>Added to blacklist for reason %reason%</target>
426426
</trans-unit>
427+
<trans-unit id="subscription.could_not_read_file">
428+
<source>Could not read the uploaded file.</source>
429+
<target>Could not read the uploaded file.</target>
430+
</trans-unit>
431+
<trans-unit id="subscription.csv_import_error">
432+
<source>Error processing %email%: %error%</source>
433+
<target>Error processing %email%: %error%</target>
434+
</trans-unit>
435+
<trans-unit id="subscription.csv_import_general_error">
436+
<source>General import error: %error%</source>
437+
<target>General import error: %error%</target>
438+
</trans-unit>
427439

428440

429441
</body>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\Core\Domain\Subscription\Exception;
6+
7+
use RuntimeException;
8+
9+
class CouldNotReadUploadedFileException extends RuntimeException
10+
{
11+
12+
}

src/Domain/Subscription/Service/SubscriberCsvImporter.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpList\Core\Domain\Subscription\Service;
66

77
use Doctrine\ORM\EntityManagerInterface;
8+
use PhpList\Core\Domain\Subscription\Exception\CouldNotReadUploadedFileException;
89
use PhpList\Core\Domain\Subscription\Model\Dto\ImportSubscriberDto;
910
use PhpList\Core\Domain\Subscription\Model\Dto\SubscriberImportOptions;
1011
use PhpList\Core\Domain\Subscription\Model\Subscriber;
@@ -13,8 +14,8 @@
1314
use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberAttributeManager;
1415
use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberManager;
1516
use PhpList\Core\Domain\Subscription\Service\Manager\SubscriptionManager;
16-
use RuntimeException;
1717
use Symfony\Component\HttpFoundation\File\UploadedFile;
18+
use Symfony\Contracts\Translation\TranslatorInterface;
1819
use 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;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PHPUnit\Framework\MockObject\MockObject;
2020
use PHPUnit\Framework\TestCase;
2121
use Symfony\Component\HttpFoundation\File\UploadedFile;
22+
use Symfony\Component\Translation\Translator;
2223

2324
class SubscriberCsvImporterTest extends TestCase
2425
{
@@ -47,6 +48,7 @@ protected function setUp(): void
4748
csvImporter: $this->csvImporterMock,
4849
attrDefinitionRepository: $this->attributeDefinitionRepositoryMock,
4950
entityManager: $entityManager,
51+
translator: new Translator('en'),
5052
);
5153
}
5254

0 commit comments

Comments
 (0)