Skip to content

Commit f6bbf6c

Browse files
committed
Replaced DbalMessage::fromArrrayDbResult() with DbalContext::convertMessage()
1 parent 5c469c6 commit f6bbf6c

File tree

6 files changed

+41
-37
lines changed

6 files changed

+41
-37
lines changed

pkg/dbal/DbalConsumer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ protected function receiveMessage(): ?DbalMessage
142142
$this->dbal->commit();
143143

144144
if (empty($dbalMessage['time_to_live']) || ($dbalMessage['time_to_live'] / 1000) > microtime(true)) {
145-
return DbalMessage::fromArrayDbResult($dbalMessage);
145+
return $this->context->convertMessage($dbalMessage);
146146
}
147147

148148
return null;

pkg/dbal/DbalContext.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,30 @@ public function createSubscriptionConsumer(): SubscriptionConsumer
128128
return new DbalSubscriptionConsumer($this);
129129
}
130130

131+
/**
132+
* @internal It must be used here and in the consumer only
133+
*/
134+
public function convertMessage(array $dbalMessage): DbalMessage
135+
{
136+
$dbalMessageObj = new DbalMessage(
137+
$dbalMessage['body'],
138+
$dbalMessage['properties'] ? JSON::decode($dbalMessage['properties']) : [],
139+
$dbalMessage['headers'] ? JSON::decode($dbalMessage['headers']) : []
140+
);
141+
142+
if (isset($dbalMessage['redelivered'])) {
143+
$dbalMessageObj->setRedelivered((bool) $dbalMessage['redelivered']);
144+
}
145+
if (isset($dbalMessage['priority'])) {
146+
$dbalMessageObj->setPriority((int) $dbalMessage['priority']);
147+
}
148+
if (isset($dbalMessage['published_at'])) {
149+
$dbalMessageObj->setPublishedAt((int) $dbalMessage['published_at']);
150+
}
151+
152+
return $dbalMessageObj;
153+
}
154+
131155
/**
132156
* @param DbalDestination $queue
133157
*/

pkg/dbal/DbalMessage.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,6 @@ public function __construct(string $body = '', array $properties = [], array $he
6767
$this->deliveryDelay = null;
6868
}
6969

70-
public static function fromArrayDbResult(array $dbalMessage): self
71-
{
72-
$dbalMessageObj = new self(
73-
$dbalMessage['body'],
74-
$dbalMessage['properties'] ? JSON::decode($dbalMessage['properties']) : [],
75-
$dbalMessage['headers'] ? JSON::decode($dbalMessage['headers']) : []
76-
);
77-
78-
if (isset($dbalMessage['redelivered'])) {
79-
$dbalMessageObj->setRedelivered((bool) $dbalMessage['redelivered']);
80-
}
81-
if (isset($dbalMessage['priority'])) {
82-
$dbalMessageObj->setPriority((int) $dbalMessage['priority']);
83-
}
84-
if (isset($dbalMessage['published_at'])) {
85-
$dbalMessageObj->setPublishedAt((int) $dbalMessage['published_at']);
86-
}
87-
88-
return $dbalMessageObj;
89-
}
90-
9170
public function setBody(string $body): void
9271
{
9372
$this->body = $body;

pkg/dbal/DbalSubscriptionConsumer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function consume(int $timeout = 0): void
6262
if ($message) {
6363
$this->dbal->delete($this->context->getTableName(), ['id' => $message['id']], ['id' => Type::GUID]);
6464

65-
$dbalMessage = DbalMessage::fromArrayDbResult($message);
65+
$dbalMessage = $this->context->convertMessage($message);
6666

6767
/**
6868
* @var DbalConsumer

pkg/dbal/Tests/DbalContextTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ public function testShouldCreateMessage()
6464
$this->assertFalse($message->isRedelivered());
6565
}
6666

67+
public function testShouldConvertArrayToDbalMessage()
68+
{
69+
$arrayData = [
70+
'body' => 'theBody',
71+
'properties' => json_encode(['barProp' => 'barPropVal']),
72+
'headers' => json_encode(['fooHeader' => 'fooHeaderVal']),
73+
];
74+
$context = new DbalContext($this->createConnectionMock());
75+
$message = $context->convertMessage($arrayData);
76+
77+
$this->assertSame('theBody', $message->getBody());
78+
$this->assertSame(['barProp' => 'barPropVal'], $message->getProperties());
79+
$this->assertSame(['fooHeader' => 'fooHeaderVal'], $message->getHeaders());
80+
}
81+
6782
public function testShouldCreateTopic()
6883
{
6984
$context = new DbalContext($this->createConnectionMock());

pkg/dbal/Tests/DbalMessageTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,6 @@ public function testCouldBeConstructedWithOptionalArguments()
2929
$this->assertSame(['fooHeader' => 'fooHeaderVal'], $message->getHeaders());
3030
}
3131

32-
public function testCouldBeCreatedFromArray()
33-
{
34-
$arrayData = [
35-
'body' => 'theBody',
36-
'properties' => json_encode(['barProp' => 'barPropVal']),
37-
'headers' => json_encode(['fooHeader' => 'fooHeaderVal']),
38-
];
39-
$message = DbalMessage::fromArrayDbResult($arrayData);
40-
41-
$this->assertSame('theBody', $message->getBody());
42-
$this->assertSame(['barProp' => 'barPropVal'], $message->getProperties());
43-
$this->assertSame(['fooHeader' => 'fooHeaderVal'], $message->getHeaders());
44-
}
45-
4632
public function testShouldSetPriorityToNullInConstructor()
4733
{
4834
$message = new DbalMessage();

0 commit comments

Comments
 (0)