Skip to content

Commit 0213e05

Browse files
committed
refactor(service): accept datetime interface in field value service
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 9547c67 commit 0213e05

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

lib/Service/FieldValueService.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace OCA\ProfileFields\Service;
1111

1212
use DateTime;
13+
use DateTimeInterface;
1314
use InvalidArgumentException;
1415
use JsonException;
1516
use OCA\ProfileFields\Db\FieldDefinition;
@@ -27,7 +28,7 @@ public function __construct(
2728
/**
2829
* @param array<string, mixed>|scalar|null $rawValue
2930
*/
30-
public function upsert(FieldDefinition $definition, string $userUid, array|string|int|float|bool|null $rawValue, string $updatedByUid, ?string $currentVisibility = null, ?DateTime $updatedAt = null): FieldValue {
31+
public function upsert(FieldDefinition $definition, string $userUid, array|string|int|float|bool|null $rawValue, string $updatedByUid, ?string $currentVisibility = null, ?DateTimeInterface $updatedAt = null): FieldValue {
3132
$normalizedValue = $this->normalizeValue($definition, $rawValue);
3233
$visibility = $currentVisibility ?? $definition->getInitialVisibility();
3334
if (!FieldVisibility::isValid($visibility)) {
@@ -40,7 +41,7 @@ public function upsert(FieldDefinition $definition, string $userUid, array|strin
4041
$entity->setValueJson($this->encodeValue($normalizedValue));
4142
$entity->setCurrentVisibility($visibility);
4243
$entity->setUpdatedByUid($updatedByUid);
43-
$entity->setUpdatedAt($updatedAt ?? new DateTime());
44+
$entity->setUpdatedAt($this->asMutableDateTime($updatedAt));
4445

4546
if ($entity->getId() === null) {
4647
return $this->fieldValueMapper->insert($entity);
@@ -101,7 +102,7 @@ public function updateVisibility(FieldDefinition $definition, string $userUid, s
101102

102103
$entity->setCurrentVisibility($currentVisibility);
103104
$entity->setUpdatedByUid($updatedByUid);
104-
$entity->setUpdatedAt(new DateTime());
105+
$entity->setUpdatedAt($this->asMutableDateTime());
105106

106107
return $this->fieldValueMapper->update($entity);
107108
}
@@ -180,4 +181,16 @@ private function decodeValue(string $valueJson): array {
180181

181182
return $decoded;
182183
}
184+
185+
private function asMutableDateTime(?DateTimeInterface $value = null): DateTime {
186+
if ($value instanceof DateTime) {
187+
return clone $value;
188+
}
189+
190+
if ($value !== null) {
191+
return DateTime::createFromInterface($value);
192+
}
193+
194+
return new DateTime();
195+
}
183196
}

0 commit comments

Comments
 (0)