E56C feat: Add AddFifoStamp middleware · symfony/symfony@8c0a310 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8c0a310

Browse files
committed
feat: Add AddFifoStamp middleware
1 parent ff56fe8 commit 8c0a310

File tree

5 files changed

+221
-0
lines changed

5 files changed

+221
-0
lines changed

src/Symfony/Component/Messenger/Bridge/AmazonSqs/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.2
5+
---
6+
7+
* Added `AddFifoStamp` middleware to easily create `AmazonSqsFifoStamp` stamp
8+
49
6.1
510
---
611

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Bridge\AmazonSqs\Middleware;
13+
14+
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsFifoStamp;
15+
use Symfony\Component\Messenger\Envelope;
16+
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
17+
use Symfony\Component\Messenger\Middleware\StackInterface;
18+
19+
final class AddFifoStamp implements MiddlewareInterface
20+
{
21+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
22+
{
23+
$message = $envelope->getMessage();
24+
$messageGroupId = null;
25+
$messageDeduplicationId = null;
26+
27+
if ($message instanceof WithMessageGroupId) {
28+
$messageGroupId = $message->messageGroupId();
29+
}
30+
if ($message instanceof WithMessageDeduplicationId) {
31+
$messageDeduplicationId = $message->messageDeduplicationId();
32+
}
33+
34+
if (null !== $messageGroupId || null !== $messageDeduplicationId) {
35+
$envelope = $envelope->with(
36+
new AmazonSqsFifoStamp(
37+
$messageGroupId,
38+
$messageDeduplicationId,
39+
)
40+
);
41+
}
42+
43+
return $stack->next()->handle($envelope, $stack);
44+
}
45+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Bridge\AmazonSqs\Middleware;
13+
14+
interface WithMessageDeduplicationId
15+
{
16+
/**
17+
* Max length of the messageDeduplicationId is 128 characters.
18+
* Can contain only alphanumeric characters and punctuation.
19+
* @See https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SendMessage.html
20+
*/
21+
public function messageDeduplicationId(): string;
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Bridge\AmazonSqs\Middleware;
13+
14+
interface WithMessageGroupId
15+
{
16+
public function messageGroupId(): string;
17+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Middleware;
13+
14+
use Symfony\Component\Messenger\Bridge\AmazonSqs\Middleware\AddFifoStamp;
15+
use Symfony\Component\Messenger\Bridge\AmazonSqs\Middleware\WithMessageDeduplicationId;
16+
use Symfony\Component\Messenger\Bridge\AmazonSqs\Middleware\WithMessageGroupId;
17+
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsFifoStamp;
18+
use Symfony\Component\Messenger\Envelope;
19+
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
20+
21+
class AddFifoStampTest extends MiddlewareTestCase
22+
{
23+
public function testAddStampWithGroupIdOnly(): void
24+
{
25+
$middleware = new AddFifoStamp();
26+
$envelope = new Envelope(new WithMessageGroupIdMessage('groupId'));
27+
$finalEnvelope = $middleware->handle($envelope, $this->getStackMock());
28+
$stamp = $finalEnvelope->last(AmazonSqsFifoStamp::class);
29+
$this->assertNotNull($stamp);
30+
/** @var AmazonSqsFifoStamp $stamp */
31+
$this->assertEquals('groupId', $stamp->getMessageGroupId());
32+
$this->assertNull($stamp->getMessageDeduplicationId());
33+
}
34+
35+
public function testHandleWithDeduplicationIdOnly(): void
36+
{
37+
$middleware = new AddFifoStamp();
38+
$envelope = new Envelope(new WithMessageDeduplicationIdMessage('deduplicationId'));
39+
$finalEnvelope = $middleware->handle($envelope, $this->getStackMock());
40+
$stamp = $finalEnvelope->last(AmazonSqsFifoStamp::class);
41+
$this->assertNotNull($stamp);
42+
/** @var AmazonSqsFifoStamp $stamp */
43+
$this->assertEquals('deduplicationId', $stamp->getMessageDeduplicationId());
44+
$this->assertNull($stamp->getMessageGroupId());
45+
}
46+
47+
public function testHandleWithGroupIdAndDeduplicationId(): void
48+
{
49+
$middleware = new AddFifoStamp();
50+
$envelope = new Envelope(new WithMessageDeduplicationIdAndMessageGroupIdMessage('my_group', 'my_random_id'));
51+
$finalEnvelope = $middleware->handle($envelope, $this->getStackMock());
52+
$stamp = $finalEnvelope->last(AmazonSqsFifoStamp::class);
53+
$this->assertNotNull($stamp);
54+
/** @var AmazonSqsFifoStamp $stamp */
55+
$this->assertEquals('my_random_id', $stamp->getMessageDeduplicationId());
56+
$this->assertEquals('my_group', $stamp->getMessageGroupId());
57+
}
58+
59+
public function testHandleWithoutId(): void
60+
{
61+
$middleware = new AddFifoStamp();
62+
$envelope = new Envelope(new WithoutIdMessage());
63+
$finalEnvelope = $middleware->handle($envelope, $this->getStackMock());
64+
$stamp = $finalEnvelope->last(AmazonSqsFifoStamp::class);
65+
/** @var AmazonSqsFifoStamp $stamp */
66+
$this->assertNull($stamp);
67+
}
68+
}
69+
70+
class WithMessageDeduplicationIdAndMessageGroupIdMessage implements WithMessageDeduplicationId, WithMessageGroupId
71+
{
72+
private string $messageGroupId;
73+
private string $messageDeduplicationId;
74+
75+
public function __construct(
76+
string $messageGroupId,
77+
string $messageDeduplicationId
78+
)
79+
{
80+
$this->messageGroupId = $messageGroupId;
81+
$this->messageDeduplicationId = $messageDeduplicationId;
82+
}
83+
84+
public function messageDeduplicationId(): string
85+
{
86+
return $this->messageDeduplicationId;
87+
}
88+
89+
public function messageGroupId(): string
90+
{
91+
return $this->messageGroupId;
92+
}
93+
}
94+
95+
96+
class WithMessageDeduplicationIdMessage implements WithMessageDeduplicationId
97+
{
98+
private string $messageDeduplicationId;
99+
100+
public function __construct(
101+
string $messageDeduplicationId
102+
)
103+
{
104+
$this->messageDeduplicationId = $messageDeduplicationId;
105+
}
106+
107+
public function messageDeduplicationId(): string
108+
{
109+
return $this->messageDeduplicationId;
110+
}
111+
}
112+
113+
114+
class WithMessageGroupIdMessage implements WithMessageGroupId
115+
{
116+
private string $messageGroupId;
117+
118+
public function __construct(
119+
string $messageGroupId
120+
)
121+
{
122+
$this->messageGroupId = $messageGroupId;
123+
}
124+
125+
public function messageGroupId(): string
126+
{
127+
return $this->messageGroupId;
128+
}
129+
}
130+
class WithoutIdMessage
131+
{
132+
}

0 commit comments

Comments
 (0)
0