Skip to content

Commit 3c5b5b0

Browse files
committed
[domain] add createdAt field to message state
1 parent 468b539 commit 3c5b5b0

2 files changed

Lines changed: 84 additions & 2 deletions

File tree

‎src/Domain/MessageState.php‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ class MessageState {
4949
*/
5050
private ?DataMessage $dataMessage;
5151

52+
/**
53+
* Created at timestamp
54+
* @var ?string
55+
*/
56+
private ?string $createdAt;
57+
5258
/**
5359
* @param array<RecipientState> $recipients
5460
*/
@@ -59,7 +65,8 @@ public function __construct(
5965
bool $isHashed = false,
6066
bool $isEncrypted = false,
6167
?TextMessage $textMessage = null,
62-
?DataMessage $dataMessage = null
68+
?DataMessage $dataMessage = null,
69+
?string $createdAt = null
6370
) {
6471
$this->id = $id;
6572
$this->state = $state;
@@ -68,6 +75,7 @@ public function __construct(
6875
$this->isEncrypted = $isEncrypted;
6976
$this->textMessage = $textMessage;
7077
$this->dataMessage = $dataMessage;
78+
$this->createdAt = $createdAt;
7179
}
7280

7381
/**
@@ -118,6 +126,14 @@ public function DataMessage(): ?DataMessage {
118126
return $this->dataMessage;
119127
}
120128

129+
/**
130+
* Get created at timestamp
131+
* @return ?string
132+
*/
133+
public function CreatedAt(): ?string {
134+
return $this->createdAt;
135+
}
136+
121137
public function Decrypt(Encryptor $encryptor): self {
122138
if ($this->isHashed) {
123139
return $this;
@@ -161,7 +177,8 @@ public static function FromObject(object $obj): self {
161177
$obj->isHashed ?? false,
162178
$obj->isEncrypted ?? false,
163179
isset($obj->textMessage) ? TextMessage::FromObject($obj->textMessage) : null,
164-
isset($obj->dataMessage) ? DataMessage::FromObject($obj->dataMessage) : null
180+
isset($obj->dataMessage) ? DataMessage::FromObject($obj->dataMessage) : null,
181+
$obj->createdAt ?? null
165182
);
166183
}
167184
}

‎tests/Domain/MessageStateTest.php‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,71 @@ public function testCanGetRecipients(): void {
4141
$this->assertSame($recipients, $messageState->Recipients());
4242
}
4343

44+
public function testCanGetCreatedAt(): void {
45+
$createdAt = '2024-01-02T03:04:05+02:00';
46+
$messageState = new MessageState('msg_123', $this->processStateMock, [$this->recipientStateMock], false, false, null, null, $createdAt);
47+
$this->assertEquals($createdAt, $messageState->CreatedAt());
48+
}
49+
50+
public function testCreatedAtDefaultsToNull(): void {
51+
$messageState = new MessageState('msg_123', $this->processStateMock, [$this->recipientStateMock]);
52+
$this->assertNull($messageState->CreatedAt());
53+
}
54+
55+
public function testCanCreateFromObjectWithCreatedAt(): void {
56+
$createdAt = '2024-01-02T03:04:05+02:00';
57+
$obj = (object) [
58+
'id' => 'msg_123',
59+
'state' => ProcessState::PENDING,
60+
'createdAt' => $createdAt,
61+
'recipients' => [
62+
(object) [
63+
'phoneNumber' => '+1234567890',
64+
'state' => ProcessState::PENDING,
65+
],
66+
]
67+
];
68+
69+
$messageState = MessageState::FromObject($obj);
70+
71+
$this->assertEquals($createdAt, $messageState->CreatedAt());
72+
}
73+
74+
public function testCanCreateFromObjectWithoutCreatedAt(): void {
75+
$obj = (object) [
76+
'id' => 'msg_123',
77+
'state' => ProcessState::PENDING,
78+
'recipients' => [
79+
(object) [
80+
'phoneNumber' => '+1234567890',
81+
'state' => ProcessState::PENDING,
82+
],
83+
]
84+
];
85+
86+
$messageState = MessageState::FromObject($obj);
87+
88+
$this->assertNull($messageState->CreatedAt());
89+
}
90+
91+
public function testCanCreateFromObjectWithMalformedCreatedAt(): void {
92+
$obj = (object) [
93+
'id' => 'msg_123',
94+
'state' => ProcessState::PENDING,
95+
'createdAt' => 'not-a-valid-timestamp',
96+
'recipients' => [
97+
(object) [
98+
'phoneNumber' => '+1234567890',
99+
'state' => ProcessState::PENDING,
100+
],
101+
]
102+
];
103+
104+
$messageState = MessageState::FromObject($obj);
105+
106+
$this->assertEquals('not-a-valid-timestamp', $messageState->CreatedAt());
107+
}
108+
44109
public function testCanCreateFromObject(): void {
45110
$id = 'msg_123';
46111
$obj = (object) [

0 commit comments

Comments
 (0)