Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 805e3e6

Browse files
committed
feat(GPS): allow send attributes in Google PubSub message.
1 parent bb6e759 commit 805e3e6

File tree

5 files changed

+77
-9
lines changed

5 files changed

+77
-9
lines changed

‎docs/transport/gps.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ $context->declareTopic($fooTopic);
6262
$context->createProducer()->send($fooTopic, $message);
6363
```
6464

65+
You can send attributes using headers :
66+
67+
```php
68+
<?php
69+
/** @var \Enqueue\Gps\GpsContext $context */
70+
71+
$fooTopic = $context->createTopic('foo');
72+
$attributes = ['key1' => 'value1'];
73+
$message = $context->createMessage('Hello world!', [], ['attributes' => $attributes]);
74+
75+
$context->declareTopic($fooTopic);
76+
77+
$context->createProducer()->send($fooTopic, $message);
78+
```
79+
6580
## Consume message:
6681

6782
Before you can consume message you have to subscribe a queue to the topic.

‎pkg/gps/GpsMessage.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,17 @@ class GpsMessage implements Message, \JsonSerializable
3434
*/
3535
private $nativeMessage;
3636

37+
/**
38+
* @var array
39+
*/
40+
private $attributes;
41+
3742
public function __construct(string $body = '', array $properties = [], array $headers = [])
3843
{
3944
$this->body = $body;
4045
$this->properties = $properties;
46+
$this->attributes = $headers['attributes'] ?? [];
47+
unset($headers['attributes']);
4148
$this->headers = $headers;
4249

4350
$this->redelivered = false;
@@ -103,7 +110,7 @@ public function isRedelivered(): bool
103110
return $this->redelivered;
104111
}
105112

106-
public function setCorrelationId(string $correlationId = null): void
113+
public function setCorrelationId(?string $correlationId = null): void
107114
{
108115
$this->setHeader('correlation_id', $correlationId);
109116
}
@@ -113,7 +120,7 @@ public function getCorrelationId(): ?string
113120
return $this->getHeader('correlation_id');
114121
}
115122

116-
public function setMessageId(string $messageId = null): void
123+
public function setMessageId(?string $messageId = null): void
117124
{
118125
$this->setHeader('message_id', $messageId);
119126
}
@@ -130,12 +137,12 @@ public function getTimestamp(): ?int
130137
return null === $value ? null : (int) $value;
131138
}
132139

133-
public function setTimestamp(int $timestamp = null): void
140+
public function setTimestamp(?int $timestamp = null): void
134141
{
135142
$this->setHeader('timestamp', $timestamp);
136143
}
137144

138-
public function setReplyTo(string $replyTo = null): void
145+
public function setReplyTo(?string $replyTo = null): void
139146
{
140147
$this->setHeader('reply_to', $replyTo);
141148
}
@@ -169,8 +176,13 @@ public function getNativeMessage(): ?GoogleMessage
169176
return $this->nativeMessage;
170177
}
171178

172-
public function setNativeMessage(GoogleMessage $message = null): void
179+
public function setNativeMessage(?GoogleMessage $message = null): void
173180
{
174181
$this->nativeMessage = $message;
175182
}
183+
184+
public function getAttributes(): array
185+
{
186+
return $this->attributes;
187+
}
176188
}

‎pkg/gps/GpsProducer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ public function send(Destination $destination, Message $message): void
3939
$topic = $this->context->getClient()->topic($destination->getTopicName());
4040
$topic->publish([
4141
'data' => json_encode($message),
42+
'attributes' => $message->getAttributes(),
4243
]);
4344
}
4445

45-
public function setDeliveryDelay(int $deliveryDelay = null): Producer
46+
public function setDeliveryDelay(?int $deliveryDelay = null): Producer
4647
{
4748
if (null === $deliveryDelay) {
4849
return $this;
@@ -56,7 +57,7 @@ public function getDeliveryDelay(): ?int
5657
return null;
5758
}
5859

59-
public function setPriority(int $priority = null): Producer
60+
public function setPriority(?int $priority = null): Producer
6061
{
6162
if (null === $priority) {
6263
return $this;
@@ -70,7 +71,7 @@ public function getPriority(): ?int
7071
return null;
7172
}
7273

73-
public function setTimeToLive(int $timeToLive = null): Producer
74+
public function setTimeToLive(?int $timeToLive = null): Producer
7475
{
7576
if (null === $timeToLive) {
7677
return $this;

‎pkg/gps/Tests/GpsMessageTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testCouldBeUnserializedFromJson()
2929

3030
$json = json_encode($message);
3131

32-
//guard
32+
//guard
3333
$this->assertNotEmpty($json);
3434

3535
$unserializedMessage = GpsMessage::jsonUnserialize($json);
@@ -70,4 +70,13 @@ public function testThrowIfMalformedJsonGivenOnUnsterilizedFromJson()
7070

7171
GpsMessage::jsonUnserialize('{]');
7272
}
73+
74+
public function testGetAttributes()
75+
{
76+
$message = new GpsMessage('the body', [], ['attributes' => ['key1' => 'value1']]);
77+
78+
$attributes = $message->getAttributes();
79+
80+
$this->assertSame(['key1' => 'value1'], $attributes);
81+
}
7382
}

‎pkg/gps/Tests/GpsProducerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,37 @@ public function testShouldSendMessage()
5555
$producer->send($topic, $message);
5656
}
5757

58+
public function testShouldSendMessageWithAttributes()
59+
{
60+
$topic = new GpsTopic('topic-name');
61+
$message = new GpsMessage('', [], ['attributes' => ['key1' => 'value1']]);
62+
63+
$gtopic = $this->createGTopicMock();
64+
$gtopic
65+
->expects($this->once())
66+
->method('publish')
67+
->with($this->identicalTo(['data' => '{"body":"","properties":[],"headers":[]}', 'attributes' => ['key1' => 'value1']]))
68+
;
69+
70+
$client = $this->createPubSubClientMock();
71+
$client
72+
->expects($this->once())
73+
->method('topic')
74+
->with('topic-name')
75+
->willReturn($gtopic)
76+
;
77+
78+
$context = $this->createContextMock();
79+
$context
80+
->expects($this->once())
81+
->method('getClient')
82+
->willReturn($client)
83+
;
84+
85+
$producer = new GpsProducer($context);
86+
$producer->send($topic, $message);
87+
}
88+
5889
/**
5990
* @return GpsContext|\PHPUnit\Framework\MockObject\MockObject|GpsContext
6091
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /