mirror of
https://github.com/symfony/amazon-sqs-messenger.git
synced 2026-03-23 23:12:09 +01:00
[Messenger] [Sqs] Add AddFifoStamp middleware
This commit is contained in:
committed by
Nicolas Grekas
parent
a51a70e3e9
commit
4cb59cb984
@@ -1,6 +1,11 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
6.4
|
||||
---
|
||||
|
||||
* Add `AddFifoStampMiddleware` to help adding `AmazonSqsFifoStamp`
|
||||
|
||||
6.1
|
||||
---
|
||||
|
||||
|
||||
24
MessageDeduplicationAwareInterface.php
Normal file
24
MessageDeduplicationAwareInterface.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Messenger\Bridge\AmazonSqs;
|
||||
|
||||
/**
|
||||
* @see https://docs.aws.amazon.com/en_gb/AWSSimpleQueueService/latest/SQSDeveloperGuide/using-messagededuplicationid-property.html
|
||||
*/
|
||||
interface MessageDeduplicationAwareInterface
|
||||
{
|
||||
/**
|
||||
* The max length of MessageDeduplicationId is 128 characters.
|
||||
* Valid values: alphanumeric characters and punctuation (!"#$%&'()*+,-./:;<=>?@[]^_`{|}~).
|
||||
*/
|
||||
public function getMessageDeduplicationId(): ?string;
|
||||
}
|
||||
24
MessageGroupAwareInterface.php
Normal file
24
MessageGroupAwareInterface.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Messenger\Bridge\AmazonSqs;
|
||||
|
||||
/**
|
||||
* @see https://docs.aws.amazon.com/en_gb/AWSSimpleQueueService/latest/SQSDeveloperGuide/using-messagegroupid-property.html
|
||||
*/
|
||||
interface MessageGroupAwareInterface
|
||||
{
|
||||
/**
|
||||
* The max length of MessageGroupId is 128 characters.
|
||||
* Valid values: alphanumeric characters and punctuation (!"#$%&'()*+,-./:;<=>?@[]^_`{|}~).
|
||||
*/
|
||||
public function getMessageGroupId(): ?string;
|
||||
}
|
||||
42
Middleware/AddFifoStampMiddleware.php
Normal file
42
Middleware/AddFifoStampMiddleware.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Messenger\Bridge\AmazonSqs\Middleware;
|
||||
|
||||
use Symfony\Component\Messenger\Bridge\AmazonSqs\MessageDeduplicationAwareInterface;
|
||||
use Symfony\Component\Messenger\Bridge\AmazonSqs\MessageGroupAwareInterface;
|
||||
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsFifoStamp;
|
||||
use Symfony\Component\Messenger\Envelope;
|
||||
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
|
||||
use Symfony\Component\Messenger\Middleware\StackInterface;
|
||||
|
||||
final class AddFifoStampMiddleware implements MiddlewareInterface
|
||||
{
|
||||
public function handle(Envelope $envelope, StackInterface $stack): Envelope
|
||||
{
|
||||
$message = $envelope->getMessage();
|
||||
$messageGroupId = null;
|
||||
$messageDeduplicationId = null;
|
||||
|
||||
if ($message instanceof MessageGroupAwareInterface) {
|
||||
$messageGroupId = $message->getMessageGroupId();
|
||||
}
|
||||
if ($message instanceof MessageDeduplicationAwareInterface) {
|
||||
$messageDeduplicationId = $message->getMessageDeduplicationId();
|
||||
}
|
||||
|
||||
if (null !== $messageGroupId || null !== $messageDeduplicationId) {
|
||||
$envelope = $envelope->with(new AmazonSqsFifoStamp($messageGroupId, $messageDeduplicationId));
|
||||
}
|
||||
|
||||
return $stack->next()->handle($envelope, $stack);
|
||||
}
|
||||
}
|
||||
112
Tests/Middleware/AddFifoStampTest.php
Normal file
112
Tests/Middleware/AddFifoStampTest.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Middleware;
|
||||
|
||||
use Symfony\Component\Messenger\Bridge\AmazonSqs\MessageDeduplicationAwareInterface;
|
||||
use Symfony\Component\Messenger\Bridge\AmazonSqs\MessageGroupAwareInterface;
|
||||
use Symfony\Component\Messenger\Bridge\AmazonSqs\Middleware\AddFifoStampMiddleware;
|
||||
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsFifoStamp;
|
||||
use Symfony\Component\Messenger\Envelope;
|
||||
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
|
||||
|
||||
class AddFifoStampTest extends MiddlewareTestCase
|
||||
{
|
||||
public function testAddStampWithGroupIdOnly()
|
||||
{
|
||||
$middleware = new AddFifoStampMiddleware();
|
||||
$envelope = new Envelope(new WithMessageGroupIdMessage('groupId'));
|
||||
$finalEnvelope = $middleware->handle($envelope, $this->getStackMock());
|
||||
$stamp = $finalEnvelope->last(AmazonSqsFifoStamp::class);
|
||||
$this->assertInstanceOf(AmazonSqsFifoStamp::class, $stamp);
|
||||
$this->assertSame('groupId', $stamp->getMessageGroupId());
|
||||
$this->assertNull($stamp->getMessageDeduplicationId());
|
||||
}
|
||||
|
||||
public function testHandleWithDeduplicationIdOnly()
|
||||
{
|
||||
$middleware = new AddFifoStampMiddleware();
|
||||
$envelope = new Envelope(new WithMessageDeduplicationIdMessage('deduplicationId'));
|
||||
$finalEnvelope = $middleware->handle($envelope, $this->getStackMock());
|
||||
$stamp = $finalEnvelope->last(AmazonSqsFifoStamp::class);
|
||||
$this->assertInstanceOf(AmazonSqsFifoStamp::class, $stamp);
|
||||
$this->assertSame('deduplicationId', $stamp->getMessageDeduplicationId());
|
||||
$this->assertNull($stamp->getMessageGroupId());
|
||||
}
|
||||
|
||||
public function testHandleWithGroupIdAndDeduplicationId()
|
||||
{
|
||||
$middleware = new AddFifoStampMiddleware();
|
||||
$envelope = new Envelope(new WithMessageDeduplicationIdAndMessageGroupIdMessage('my_group', 'my_random_id'));
|
||||
$finalEnvelope = $middleware->handle($envelope, $this->getStackMock());
|
||||
$stamp = $finalEnvelope->last(AmazonSqsFifoStamp::class);
|
||||
$this->assertInstanceOf(AmazonSqsFifoStamp::class, $stamp);
|
||||
$this->assertSame('my_random_id', $stamp->getMessageDeduplicationId());
|
||||
$this->assertSame('my_group', $stamp->getMessageGroupId());
|
||||
}
|
||||
|
||||
public function testHandleWithoutId()
|
||||
{
|
||||
$middleware = new AddFifoStampMiddleware();
|
||||
$envelope = new Envelope(new WithoutIdMessage());
|
||||
$finalEnvelope = $middleware->handle($envelope, $this->getStackMock());
|
||||
$stamp = $finalEnvelope->last(AmazonSqsFifoStamp::class);
|
||||
$this->assertNull($stamp);
|
||||
}
|
||||
}
|
||||
|
||||
class WithMessageDeduplicationIdAndMessageGroupIdMessage implements MessageDeduplicationAwareInterface, MessageGroupAwareInterface
|
||||
{
|
||||
public function __construct(
|
||||
private string $messageGroupId,
|
||||
private string $messageDeduplicationId,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getMessageDeduplicationId(): ?string
|
||||
{
|
||||
return $this->messageDeduplicationId;
|
||||
}
|
||||
|
||||
public function getMessageGroupId(): ?string
|
||||
{
|
||||
return $this->messageGroupId;
|
||||
}
|
||||
}
|
||||
|
||||
class WithMessageDeduplicationIdMessage implements MessageDeduplicationAwareInterface
|
||||
{
|
||||
public function __construct(
|
||||
private string $messageDeduplicationId,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getMessageDeduplicationId(): ?string
|
||||
{
|
||||
return $this->messageDeduplicationId;
|
||||
}
|
||||
}
|
||||
|
||||
class WithMessageGroupIdMessage implements MessageGroupAwareInterface
|
||||
{
|
||||
public function __construct(
|
||||
private string $messageGroupId,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getMessageGroupId(): ?string
|
||||
{
|
||||
return $this->messageGroupId;
|
||||
}
|
||||
}
|
||||
class WithoutIdMessage
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user