Skip to content

Commit c11015e

Browse files
committed
AttributeDefinitionManager
1 parent 68ac282 commit c11015e

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

resources/translations/messages.en.xlf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,15 @@
395395
<source>Subscription not found for this subscriber and list.</source>
396396
<target>Subscription not found for this subscriber and list.</target>
397397
</trans-unit>
398+
<trans-unit id="subscription.attribute_definition_exists">
399+
<source>Attribute definition already exists</source>
400+
<target>Attribute definition already exists</target>
401+
</trans-unit>
402+
<trans-unit id="subscription.attribute_definition_name_exists">
403+
<source>Another attribute with this name already exists.</source>
404+
<target>Another attribute with this name already exists.</target>
405+
</trans-unit>
406+
398407
</body>
399408
</file>
400409
</xliff>

src/Domain/Subscription/Service/Manager/AttributeDefinitionManager.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,32 @@
99
use PhpList\Core\Domain\Subscription\Model\SubscriberAttributeDefinition;
1010
use PhpList\Core\Domain\Subscription\Repository\SubscriberAttributeDefinitionRepository;
1111
use PhpList\Core\Domain\Subscription\Validator\AttributeTypeValidator;
12+
use Symfony\Contracts\Translation\TranslatorInterface;
1213

1314
class AttributeDefinitionManager
1415
{
1516
private SubscriberAttributeDefinitionRepository $definitionRepository;
1617
private AttributeTypeValidator $attributeTypeValidator;
18+
private TranslatorInterface $translator;
1719

1820
public function __construct(
1921
SubscriberAttributeDefinitionRepository $definitionRepository,
20-
AttributeTypeValidator $attributeTypeValidator
22+
AttributeTypeValidator $attributeTypeValidator,
23+
TranslatorInterface $translator,
2124
) {
2225
$this->definitionRepository = $definitionRepository;
2326
$this->attributeTypeValidator = $attributeTypeValidator;
27+
$this->translator = $translator;
2428
}
2529

2630
public function create(AttributeDefinitionDto $attributeDefinitionDto): SubscriberAttributeDefinition
2731
{
2832
$existingAttribute = $this->definitionRepository->findOneByName($attributeDefinitionDto->name);
2933
if ($existingAttribute) {
30-
throw new AttributeDefinitionCreationException('Attribute definition already exists', 409);
34+
throw new AttributeDefinitionCreationException(
35+
message: $this->translator->trans('Attribute definition already exists'),
36+
statusCode: 409
37+
);
3138
}
3239
$this->attributeTypeValidator->validate($attributeDefinitionDto->type);
3340

@@ -50,7 +57,10 @@ public function update(
5057
): SubscriberAttributeDefinition {
5158
$existingAttribute = $this->definitionRepository->findOneByName($attributeDefinitionDto->name);
5259
if ($existingAttribute && $existingAttribute->getId() !== $attributeDefinition->getId()) {
53-
throw new AttributeDefinitionCreationException('Another attribute with this name already exists.', 409);
60+
throw new AttributeDefinitionCreationException(
61+
message: $this->translator->trans('Another attribute with this name already exists.'),
62+
statusCode: 409
63+
);
5464
}
5565
$this->attributeTypeValidator->validate($attributeDefinitionDto->type);
5666

tests/Unit/Domain/Subscription/Service/Manager/AttributeDefinitionManagerTest.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@
1111
use PhpList\Core\Domain\Subscription\Service\Manager\AttributeDefinitionManager;
1212
use PhpList\Core\Domain\Subscription\Validator\AttributeTypeValidator;
1313
use PHPUnit\Framework\TestCase;
14+
use Symfony\Component\Translation\Translator;
1415

1516
class AttributeDefinitionManagerTest extends TestCase
1617
{
1718
public function testCreateAttributeDefinition(): void
1819
{
1920
$repository = $this->createMock(SubscriberAttributeDefinitionRepository::class);
2021
$validator = $this->createMock(AttributeTypeValidator::class);
21-
$manager = new AttributeDefinitionManager($repository, $validator);
22+
$manager = new AttributeDefinitionManager(
23+
definitionRepository: $repository,
24+
attributeTypeValidator: $validator,
25+
translator: new Translator('en')
26+
);
2227

2328
$dto = new AttributeDefinitionDto(
2429
name: 'Country',
@@ -51,7 +56,11 @@ public function testCreateThrowsWhenAttributeAlreadyExists(): void
5156
{
5257
$repository = $this->createMock(SubscriberAttributeDefinitionRepository::class);
5358
$validator = $this->createMock(AttributeTypeValidator::class);
54-
$manager = new AttributeDefinitionManager($repository, $validator);
59+
$manager = new AttributeDefinitionManager(
60+
definitionRepository: $repository,
61+
attributeTypeValidator: $validator,
62+
translator: new Translator('en'),
63+
);
5564

5665
$dto = new AttributeDefinitionDto(
5766
name: 'Country',
@@ -78,7 +87,11 @@ public function testUpdateAttributeDefinition(): void
7887
{
7988
$repository = $this->createMock(SubscriberAttributeDefinitionRepository::class);
8089
$validator = $this->createMock(AttributeTypeValidator::class);
81-
$manager = new AttributeDefinitionManager($repository, $validator);
90+
$manager = new AttributeDefinitionManager(
91+
definitionRepository: $repository,
92+
attributeTypeValidator: $validator,
93+
translator: new Translator('en'),
94+
);
8295

8396
$attribute = new SubscriberAttributeDefinition();
8497
$attribute->setName('Old');
@@ -113,7 +126,11 @@ public function testUpdateThrowsWhenAnotherAttributeWithSameNameExists(): void
113126
{
114127
$repository = $this->createMock(SubscriberAttributeDefinitionRepository::class);
115128
$validator = $this->createMock(AttributeTypeValidator::class);
116-
$manager = new AttributeDefinitionManager($repository, $validator);
129+
$manager = new AttributeDefinitionManager(
130+
definitionRepository: $repository,
131+
attributeTypeValidator: $validator,
132+
translator: new Translator('en'),
133+
);
117134

118135
$dto = new AttributeDefinitionDto(
119136
name: 'Existing',
@@ -144,7 +161,11 @@ public function testDeleteAttributeDefinition(): void
144161
{
145162
$repository = $this->createMock(SubscriberAttributeDefinitionRepository::class);
146163
$validator = $this->createMock(AttributeTypeValidator::class);
147-
$manager = new AttributeDefinitionManager($repository, $validator);
164+
$manager = new AttributeDefinitionManager(
165+
definitionRepository: $repository,
166+
attributeTypeValidator: $validator,
167+
translator: new Translator('en'),
168+
);
148169

149170
$attribute = new SubscriberAttributeDefinition();
150171

0 commit comments

Comments
 (0)