Skip to content

Commit 0430c35

Browse files
committed
TemplateImageValidator
1 parent c23211d commit 0430c35

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

resources/translations/messages.en.xlf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,23 @@
357357
<target>Batch limit reached, sleeping %sleep%s to respect MAILQUEUE_BATCH_PERIOD</target>
358358
</trans-unit>
359359

360+
<trans-unit id="messaging.validation.image_url">
361+
<source>Value must be an array of image URLs.</source>
362+
<target>Value must be an array of image URLs.</target>
363+
</trans-unit>
364+
<trans-unit id="messaging.validation.image_url_not_full">
365+
<source>Image "%url%" is not a full URL.</source>
366+
<target>Image "%url%" is not a full URL.</target>
367+
</trans-unit>
368+
<trans-unit id="messaging.validation.image_url_not_exists">
369+
<source>Image "%url%" does not exist (HTTP %code%)</source>
370+
<target>Image "%url%" does not exist (HTTP %code%)</target>
371+
</trans-unit>
372+
<trans-unit id="messaging.validation.image_url_cant_validate">
373+
<source>Image "%url%" could not be validated: %message%</source>
374+
<target>Image "%url%" could not be validated: %message%</target>
375+
</trans-unit>
376+
360377
<!-- Subscription -->
361378
<trans-unit id="subscription.list_not_found">
362379
<source>Subscriber list not found.</source>

src/Domain/Messaging/Validator/TemplateImageValidator.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@
99
use PhpList\Core\Domain\Common\Model\ValidationContext;
1010
use PhpList\Core\Domain\Common\Validator\ValidatorInterface;
1111
use Symfony\Component\Validator\Exception\ValidatorException;
12+
use Symfony\Contracts\Translation\TranslatorInterface;
1213
use Throwable;
1314

1415
class TemplateImageValidator implements ValidatorInterface
1516
{
16-
public function __construct(private readonly ClientInterface $httpClient)
17-
{
17+
public function __construct(
18+
private readonly ClientInterface $httpClient,
19+
private readonly TranslatorInterface $translator,
20+
) {
1821
}
1922

2023
public function validate(mixed $value, ValidationContext $context = null): void
2124
{
2225
if (!is_array($value)) {
23-
throw new InvalidArgumentException('Value must be an array of image URLs.');
26+
throw new InvalidArgumentException($this->translator->trans('Value must be an array of image URLs.'));
2427
}
2528

2629
$checkFull = $context?->get('checkImages', false);
@@ -42,7 +45,7 @@ private function validateFullUrls(array $urls): array
4245

4346
foreach ($urls as $url) {
4447
if (!preg_match('#^https?://#i', $url)) {
45-
$errors[] = sprintf('Image "%s" is not a full URL.', $url);
48+
$errors[] = $this->translator->trans('Image "%url%" is not a full URL.', ['%url%' => $url]);
4649
}
4750
}
4851

@@ -61,10 +64,16 @@ private function validateExistence(array $urls): array
6164
try {
6265
$response = $this->httpClient->request('HEAD', $url);
6366
if ($response->getStatusCode() !== 200) {
64-
$errors[] = sprintf('Image "%s" does not exist (HTTP %s)', $url, $response->getStatusCode());
67+
$errors[] = $this->translator->trans('Image "%url%" does not exist (HTTP %code%)', [
68+
'%url%' => $url,
69+
'%code%' => $response->getStatusCode()
70+
]);
6571
}
6672
} catch (Throwable $e) {
67-
$errors[] = sprintf('Image "%s" could not be validated: %s', $url, $e->getMessage());
73+
$errors[] = $this->translator->trans('Image "%url%" could not be validated: %message%', [
74+
'%url%' => $url,
75+
'%message%' => $e->getMessage()
76+
]);
6877
}
6978
}
7079

tests/Unit/Domain/Messaging/Validator/TemplateImageValidatorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpList\Core\Domain\Messaging\Validator\TemplateImageValidator;
1313
use PHPUnit\Framework\MockObject\MockObject;
1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Translation\Translator;
1516
use Symfony\Component\Validator\Exception\ValidatorException;
1617

1718
class TemplateImageValidatorTest extends TestCase
@@ -22,7 +23,7 @@ class TemplateImageValidatorTest extends TestCase
2223
protected function setUp(): void
2324
{
2425
$this->httpClient = $this->createMock(ClientInterface::class);
25-
$this->validator = new TemplateImageValidator($this->httpClient);
26+
$this->validator = new TemplateImageValidator($this->httpClient, new Translator('en'));
2627
}
2728

2829
public function testThrowsExceptionIfValueIsNotArray(): void

0 commit comments

Comments
 (0)