Skip to content

Commit 7de21e0

Browse files
committed
Replaced MongodbMessage::fromArrrayDbResult() with MongodbContext::convertMessage()
1 parent 7d4c27f commit 7de21e0

File tree

5 files changed

+47
-18
lines changed

5 files changed

+47
-18
lines changed

MongodbConsumer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private function receiveMessage(): ?MongodbMessage
137137
return null;
138138
}
139139
if (empty($message['time_to_live']) || $message['time_to_live'] > time()) {
140-
return MongodbMessage::fromArrayDbResult($message);
140+
return $this->context->convertMessage($message);
141141
}
142142

143143
return null;

MongodbContext.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,25 @@ public function createSubscriptionConsumer(): SubscriptionConsumer
109109
return new MongodbSubscriptionConsumer($this);
110110
}
111111

112+
/**
113+
* @internal It must be used here and in the consumer only
114+
*/
115+
public function convertMessage(array $mongodbMessage): MongodbMessage
116+
{
117+
$mongodbMessageObj = new MongodbMessage(
118+
$mongodbMessage['body'],
119+
JSON::decode($mongodbMessage['properties']),
120+
JSON::decode($mongodbMessage['headers'])
121+
);
122+
123+
$mongodbMessageObj->setId((string) $mongodbMessage['_id']);
124+
$mongodbMessageObj->setPriority((int) $mongodbMessage['priority']);
125+
$mongodbMessageObj->setRedelivered((bool) $mongodbMessage['redelivered']);
126+
$mongodbMessageObj->setPublishedAt((int) $mongodbMessage['published_at']);
127+
128+
return $mongodbMessageObj;
129+
}
130+
112131
/**
113132
* @param MongodbDestination $queue
114133
*/

MongodbMessage.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,6 @@ public function __construct(string $body = '', array $properties = [], array $he
6565
$this->redelivered = false;
6666
}
6767

68-
public static function fromArrayDbResult(array $arrayResult): self
69-
{
70-
$message = new self(
71-
$arrayResult['body'],
72-
JSON::decode($arrayResult['properties']),
73-
JSON::decode($arrayResult['headers'])
74-
);
75-
76-
$message->setId((string) $arrayResult['_id']);
77-
$message->setPriority((int) $arrayResult['priority']);
78-
$message->setRedelivered((bool) $arrayResult['redelivered']);
79-
$message->setPublishedAt((int) $arrayResult['published_at']);
80-
81-
return $message;
82-
}
83-
8468
public function setId(string $id = null): void
8569
{
8670
$this->id = $id;

MongodbSubscriptionConsumer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function consume(int $timeout = 0): void
6767
if ($result) {
6868
list($consumer, $callback) = $this->subscribers[$result['queue']];
6969

70-
$message = MongodbMessage::fromArrayDbResult($result);
70+
$message = $this->context->convertMessage($result);
7171

7272
if (false === call_user_func($callback, $message, $consumer)) {
7373
return;

Tests/MongodbContextTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,32 @@ public function testShouldCreateMessage()
7070
$this->assertFalse($message->isRedelivered());
7171
}
7272

73+
public function testShouldConvertFromArrayToMongodbMessage()
74+
{
75+
$arrayData = [
76+
'_id' => 'stringId',
77+
'body' => 'theBody',
78+
'properties' => json_encode(['barProp' => 'barPropVal']),
79+
'headers' => json_encode(['fooHeader' => 'fooHeaderVal']),
80+
'priority' => '12',
81+
'published_at' => 1525935820,
82+
'redelivered' => false,
83+
];
84+
85+
$context = new MongodbContext($this->createClientMock());
86+
$message = $context->convertMessage($arrayData);
87+
88+
$this->assertInstanceOf(MongodbMessage::class, $message);
89+
90+
$this->assertEquals('stringId', $message->getId());
91+
$this->assertEquals('theBody', $message->getBody());
92+
$this->assertEquals(['barProp' => 'barPropVal'], $message->getProperties());
93+
$this->assertEquals(['fooHeader' => 'fooHeaderVal'], $message->getHeaders());
94+
$this->assertEquals(12, $message->getPriority());
95+
$this->assertEquals(1525935820, $message->getPublishedAt());
96+
$this->assertFalse($message->isRedelivered());
97+
}
98+
7399
public function testShouldCreateTopic()
74100
{
75101
$context = new MongodbContext($this->createClientMock());

0 commit comments

Comments
 (0)