Skip to content

Commit

Permalink
feat: add an AttibutesStamp to handle serialized message attributes (#22
Browse files Browse the repository at this point in the history
)
  • Loading branch information
HeahDude authored Mar 23, 2023
1 parent 93d97f6 commit bbfbb08
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ petit_press_gps_messenger:
### Step 5: Use available stamps if needed

* `OrderingKeyStamp`: use for keeping messages of the same context in order.
For more information, read an [official documentation](https://cloud.google.com/pubsub/docs/publisher#using_ordering_keys).
For more information, read an [official documentation](https://cloud.google.com/pubsub/docs/publisher#using_ordering_keys).

* `AttributesStamp`: use to add contextual metadata to serialized messages.
For more information, read an [official documentation](https://cloud.google.com/pubsub/docs/publisher#using-attributes).
Can be very useful when used together with [subscription filters](https://cloud.google.com/pubsub/docs/subscription-message-filter).

### Step 6: Create topics from config
```bash
Expand Down
38 changes: 38 additions & 0 deletions Tests/Transport/GpsSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Google\Cloud\PubSub\Topic;
use PetitPress\GpsMessengerBundle\Transport\GpsConfigurationInterface;
use PetitPress\GpsMessengerBundle\Transport\GpsSender;
use PetitPress\GpsMessengerBundle\Transport\Stamp\AttributesStamp;
use PetitPress\GpsMessengerBundle\Transport\Stamp\OrderingKeyStamp;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -146,4 +147,41 @@ public function testItPublishesWithoutOrderingKey(): void

self::assertSame($envelope, $this->gpsSender->send($envelope));
}

public function testItPublishesWithAttributes(): void
{
$attributes = ['foo' => 'bar'];
$envelope = EnvelopeFactory::create(new AttributesStamp($attributes));
$envelopeArray = ['body' => []];

$this->serializerMock
->expects($this->once())
->method('encode')
->with($envelope)
->willReturn($envelopeArray)
;

$this->gpsConfigurationMock
->expects($this->once())
->method('getTopicName')
->willReturn(self::TOPIC_NAME)
;

$this->topicMock
->expects($this->once())
->method('publish')
->with(new Message([
'data' => json_encode($envelopeArray),
'attributes' => $attributes,
]))
;

$this->pubSubClientMock
->expects($this->once())
->method('topic')
->with(self::TOPIC_NAME)
->willReturn($this->topicMock);

self::assertSame($envelope, $this->gpsSender->send($envelope));
}
}
6 changes: 6 additions & 0 deletions Transport/GpsSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Google\Cloud\PubSub\MessageBuilder;
use Google\Cloud\PubSub\PubSubClient;
use PetitPress\GpsMessengerBundle\Transport\Stamp\AttributesStamp;
use PetitPress\GpsMessengerBundle\Transport\Stamp\OrderingKeyStamp;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\TransportException;
Expand Down Expand Up @@ -57,6 +58,11 @@ public function send(Envelope $envelope): Envelope
$messageBuilder = $messageBuilder->setOrderingKey($orderingKeyStamp->getOrderingKey());
}

$attributesStamp = $envelope->last(AttributesStamp::class);
if ($attributesStamp instanceof AttributesStamp) {
$messageBuilder = $messageBuilder->setAttributes($attributesStamp->getAttributes());
}

$this->pubSubClient
->topic($this->gpsConfiguration->getTopicName())
->publish($messageBuilder->build())
Expand Down
34 changes: 34 additions & 0 deletions Transport/Stamp/AttributesStamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace PetitPress\GpsMessengerBundle\Transport\Stamp;

use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;

/**
* @author Jules Pietri <[email protected]>
*/
final class AttributesStamp implements NonSendableStampInterface
{
/**
* @var array<string, string>
*/
private array $attributes;

/**
* @param array<string, string> $attributes
*/
public function __construct(array $attributes)
{
$this->attributes = $attributes;
}

/**
* @return array<string, string>
*/
public function getAttributes(): array
{
return $this->attributes;
}
}

0 comments on commit bbfbb08

Please sign in to comment.