Skip to content

Commit 4ef29c1

Browse files
committed
refactor(php): inject IL10N into FieldDefinitionService and translate throws with literal strings
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 5c26937 commit 4ef29c1

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

lib/Service/FieldDefinitionService.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
use OCA\ProfileFields\Db\FieldDefinition;
1616
use OCA\ProfileFields\Db\FieldDefinitionMapper;
1717
use OCA\ProfileFields\Db\FieldValueMapper;
18+
use OCP\IL10N;
1819

1920
class FieldDefinitionService {
2021
public function __construct(
2122
private FieldDefinitionMapper $fieldDefinitionMapper,
2223
private FieldValueMapper $fieldValueMapper,
2324
private FieldDefinitionValidator $validator,
25+
private IL10N $l10n,
2426
) {
2527
}
2628

@@ -30,7 +32,7 @@ public function __construct(
3032
public function create(array $definition): FieldDefinition {
3133
$validated = $this->validator->validate($definition);
3234
if ($this->fieldDefinitionMapper->findByFieldKey($validated['field_key']) !== null) {
33-
throw new InvalidArgumentException('field_key already exists');
35+
throw new InvalidArgumentException($this->l10n->t('field_key already exists'));
3436
}
3537

3638
$createdAt = $this->parseImportedDate($definition['created_at'] ?? null) ?? new DateTime();
@@ -46,7 +48,7 @@ public function create(array $definition): FieldDefinition {
4648
try {
4749
$entity->setOptions(isset($validated['options']) ? json_encode($validated['options'], JSON_THROW_ON_ERROR) : null);
4850
} catch (JsonException $e) {
49-
throw new InvalidArgumentException('options could not be encoded: ' . $e->getMessage(), 0, $e);
51+
throw new InvalidArgumentException($this->l10n->t('options could not be encoded: %s', [$e->getMessage()]), 0, $e);
5052
}
5153
$entity->setCreatedAt($createdAt);
5254
$entity->setUpdatedAt($updatedAt);
@@ -60,11 +62,11 @@ public function create(array $definition): FieldDefinition {
6062
public function update(FieldDefinition $existing, array $definition): FieldDefinition {
6163
$validated = $this->validator->validate($definition + ['field_key' => $existing->getFieldKey()]);
6264
if (($definition['field_key'] ?? $existing->getFieldKey()) !== $existing->getFieldKey()) {
63-
throw new InvalidArgumentException('field_key cannot be changed');
65+
throw new InvalidArgumentException($this->l10n->t('field_key cannot be changed'));
6466
}
6567

6668
if ($validated['type'] !== $existing->getType() && $this->fieldValueMapper->hasValuesForFieldDefinitionId($existing->getId())) {
67-
throw new InvalidArgumentException('type cannot be changed after values exist');
69+
throw new InvalidArgumentException($this->l10n->t('type cannot be changed after values exist'));
6870
}
6971

7072
$existing->setLabel($validated['label']);
@@ -76,7 +78,7 @@ public function update(FieldDefinition $existing, array $definition): FieldDefin
7678
try {
7779
$existing->setOptions(isset($validated['options']) ? json_encode($validated['options'], JSON_THROW_ON_ERROR) : null);
7880
} catch (JsonException $e) {
79-
throw new InvalidArgumentException('options could not be encoded: ' . $e->getMessage(), 0, $e);
81+
throw new InvalidArgumentException($this->l10n->t('options could not be encoded: %s', [$e->getMessage()]), 0, $e);
8082
}
8183
$existing->setUpdatedAt($this->parseImportedDate($definition['updated_at'] ?? null) ?? new DateTime());
8284

0 commit comments

Comments
 (0)