Skip to content

Commit a6f2c46

Browse files
authored
Merge pull request #31 from clue-labs/message-properties
Improve initializing `MessageEvent` properties
2 parents ba6b1c9 + 64dae4b commit a6f2c46

File tree

3 files changed

+74
-26
lines changed

3 files changed

+74
-26
lines changed

src/EventSource.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,8 @@ private function request()
211211
$buffer = array_pop($messageEvents);
212212

213213
foreach ($messageEvents as $data) {
214-
$message = MessageEvent::parse($data);
215-
if ($message->lastEventId === null) {
216-
$message->lastEventId = $this->lastEventId;
217-
} else {
218-
$this->lastEventId = $message->lastEventId;
219-
}
214+
$message = MessageEvent::parse($data, $this->lastEventId);
215+
$this->lastEventId = $message->lastEventId;
220216

221217
if ($message->retry !== null) {
222218
$this->reconnectTime = $message->retry / 1000;

src/MessageEvent.php

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,59 @@ class MessageEvent
66
{
77
/**
88
* @param string $data
9+
* @param string $lastEventId
910
* @return self
1011
* @internal
1112
*/
12-
public static function parse($data)
13+
public static function parse($data, $lastEventId)
1314
{
14-
$message = new self();
15-
1615
$lines = preg_split(
1716
'/\r\n|\r(?!\n)|\n/S',
1817
$data
1918
);
19+
20+
$data = '';
21+
$id = $lastEventId;
22+
$type = 'message';
23+
$retry = null;
24+
2025
foreach ($lines as $line) {
2126
$name = strstr($line, ':', true);
2227
$value = substr(strstr($line, ':'), 1);
2328
if (isset($value[0]) && $value[0] === ' ') {
2429
$value = substr($value, 1);
2530
}
2631
if ($name === 'data') {
27-
$message->data .= $value . "\n";
32+
$data .= $value . "\n";
2833
} elseif ($name === 'id') {
29-
$message->lastEventId .= $value;
34+
$id = $value;
3035
} elseif ($name === 'event') {
31-
$message->type = $value;
36+
$type = $value;
3237
} elseif ($name === 'retry' && $value === (string)(int)$value && $value >= 0) {
33-
$message->retry = (int)$value;
38+
$retry = (int)$value;
3439
}
3540
}
3641

37-
if (substr($message->data, -1) === "\n") {
38-
$message->data = substr($message->data, 0, -1);
42+
if (substr($data, -1) === "\n") {
43+
$data = substr($data, 0, -1);
3944
}
4045

41-
return $message;
46+
return new self($data, $id, $type, $retry);
47+
}
48+
49+
/**
50+
* @internal
51+
* @param string $data
52+
* @param string $lastEventId
53+
* @param string $type
54+
* @param ?int $retry
55+
*/
56+
private function __construct($data, $lastEventId, $type, $retry)
57+
{
58+
$this->data = $data;
59+
$this->lastEventId = $lastEventId;
60+
$this->type = $type;
61+
$this->retry = $retry;
4262
}
4363

4464
/**
@@ -48,10 +68,10 @@ public static function parse($data)
4868
public $data = '';
4969

5070
/**
51-
* @var ?string
71+
* @var string
5272
* @readonly
5373
*/
54-
public $lastEventId = null;
74+
public $lastEventId = '';
5575

5676
/**
5777
* @var string

tests/MessageEventTest.php

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,85 @@ class MessageEventTest extends TestCase
99
{
1010
public function testParseSimpleData()
1111
{
12-
$message = MessageEvent::parse("data: hello");
12+
$message = MessageEvent::parse("data: hello", '');
1313

1414
$this->assertEquals('hello', $message->data);
1515
}
1616

1717
public function testParseDataOverTwoLinesWillBeCombined()
1818
{
19-
$message = MessageEvent::parse("data: hello\ndata: world");
19+
$message = MessageEvent::parse("data: hello\ndata: world", '');
2020

2121
$this->assertEquals("hello\nworld", $message->data);
2222
}
2323

2424
public function testParseDataOverTwoLinesWithCarrigeReturnsWillBeCombinedWithNewline()
2525
{
26-
$message = MessageEvent::parse("data: hello\rdata: world");
26+
$message = MessageEvent::parse("data: hello\rdata: world", '');
2727

2828
$this->assertEquals("hello\nworld", $message->data);
2929
}
3030

3131
public function testParseDataOverTwoLinesWithCarrigeReturnsAndNewlinesWillBeCombinedWithNewline()
3232
{
33-
$message = MessageEvent::parse("data: hello\r\ndata: world");
33+
$message = MessageEvent::parse("data: hello\r\ndata: world", '');
3434

3535
$this->assertEquals("hello\nworld", $message->data);
3636
}
3737

3838
public function testParseDataWithTrailingNewlineOverTwoLines()
3939
{
40-
$message = MessageEvent::parse("data: hello\ndata:");
40+
$message = MessageEvent::parse("data: hello\ndata:", '');
4141

4242
$this->assertEquals("hello\n", $message->data);
4343
}
4444

4545
public function testParseDataWithCarrigeReturnOverTwoLines()
4646
{
47-
$message = MessageEvent::parse("data: hello\rdata:");
47+
$message = MessageEvent::parse("data: hello\rdata:", '');
4848

4949
$this->assertEquals("hello\n", $message->data);
5050
}
5151

5252
public function testParseDataWithCarrigeReturnAndNewlineOverTwoLines()
5353
{
54-
$message = MessageEvent::parse("data: hello\r\ndata:");
54+
$message = MessageEvent::parse("data: hello\r\ndata:", '');
5555

5656
$this->assertEquals("hello\n", $message->data);
5757
}
5858

59+
public function testParseReturnsMessageWithIdFromStream()
60+
{
61+
$message = MessageEvent::parse("data: hello\r\nid: 1", '');
62+
63+
$this->assertEquals("hello", $message->data);
64+
$this->assertEquals('1', $message->lastEventId);
65+
}
66+
67+
public function testParseWithoutIdReturnsMessageWithIdFromLastEventId()
68+
{
69+
$message = MessageEvent::parse("data: hello", '1');
70+
71+
$this->assertEquals("hello", $message->data);
72+
$this->assertEquals('1', $message->lastEventId);
73+
}
74+
75+
public function testParseWithoutIdReturnsMessageWithEmptyIdIfLastEventIdIsEmpty()
76+
{
77+
$message = MessageEvent::parse("data: hello", '');
78+
79+
$this->assertEquals("hello", $message->data);
80+
$this->assertEquals('', $message->lastEventId);
81+
}
82+
83+
public function testParseWithMultipleIdsReturnsMessageWithLastEventIdFromStream()
84+
{
85+
$message = MessageEvent::parse("data: hello\nid: 1\nid: 2", '0');
86+
87+
$this->assertEquals("hello", $message->data);
88+
$this->assertEquals('2', $message->lastEventId);
89+
}
90+
5991
public function retryTimeDataProvider()
6092
{
6193
return [
@@ -75,7 +107,7 @@ public function retryTimeDataProvider()
75107
*/
76108
public function testParseRetryTime($input, $expected)
77109
{
78-
$message = MessageEvent::parse($input);
110+
$message = MessageEvent::parse($input, '');
79111

80112
$this->assertSame($expected, $message->retry);
81113
}

0 commit comments

Comments
 (0)