Skip to content

Commit

Permalink
Use a property attributes instead of a key in headers
Browse files Browse the repository at this point in the history
  • Loading branch information
p-pichet committed Aug 12, 2024
1 parent d748405 commit 10dea2a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 27 deletions.
15 changes: 0 additions & 15 deletions docs/transport/gps.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,6 @@ $context->declareTopic($fooTopic);
$context->createProducer()->send($fooTopic, $message);
```

You can send attributes using headers :

```php
<?php
/** @var \Enqueue\Gps\GpsContext $context */

$fooTopic = $context->createTopic('foo');
$attributes = ['key1' => 'value1'];
$message = $context->createMessage('Hello world!', [], ['attributes' => $attributes]);

$context->declareTopic($fooTopic);

$context->createProducer()->send($fooTopic, $message);
```

## Consume message:

Before you can consume message you have to subscribe a queue to the topic.
Expand Down
20 changes: 12 additions & 8 deletions pkg/gps/GpsMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class GpsMessage implements Message, \JsonSerializable
*/
private $headers;

/**
* @var array
*/
private $attributes;

/**
* @var bool
*/
Expand All @@ -34,18 +39,12 @@ class GpsMessage implements Message, \JsonSerializable
*/
private $nativeMessage;

/**
* @var array
*/
private $attributes;

public function __construct(string $body = '', array $properties = [], array $headers = [])
public function __construct(string $body = '', array $properties = [], array $headers = [], array $attributes = [])
{
$this->body = $body;
$this->properties = $properties;
$this->attributes = $headers['attributes'] ?? [];
unset($headers['attributes']);
$this->headers = $headers;
$this->attributes = $attributes;

$this->redelivered = false;
}
Expand Down Expand Up @@ -181,6 +180,11 @@ public function setNativeMessage(?GoogleMessage $message = null): void
$this->nativeMessage = $message;
}

public function setAttributes(array $attributes): void
{
$this->attributes = $attributes;
}

public function getAttributes(): array
{
return $this->attributes;
Expand Down
2 changes: 1 addition & 1 deletion pkg/gps/Tests/GpsMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function testThrowIfMalformedJsonGivenOnUnsterilizedFromJson()

public function testGetAttributes()
{
$message = new GpsMessage('the body', [], ['attributes' => ['key1' => 'value1']]);
$message = new GpsMessage('the body', [], [], ['key1' => 'value1']);

$attributes = $message->getAttributes();

Expand Down
5 changes: 2 additions & 3 deletions pkg/gps/Tests/GpsProducerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public function testShouldSendMessage()
->method('publish')
->with($this->identicalTo([
'data' => '{"body":"","properties":[],"headers":[]}',
])
);
]));

$client = $this->createPubSubClientMock();
$client
Expand All @@ -60,7 +59,7 @@ public function testShouldSendMessage()
public function testShouldSendMessageWithAttributes()
{
$topic = new GpsTopic('topic-name');
$message = new GpsMessage('', [], ['attributes' => ['key1' => 'value1']]);
$message = new GpsMessage('', [], [], ['key1' => 'value1']);

$gtopic = $this->createGTopicMock();
$gtopic
Expand Down

0 comments on commit 10dea2a

Please sign in to comment.