Skip to content

Commit 161d613

Browse files
committed
feat: Add AddFifoStamp middleware
1 parent ff56fe8 commit 161d613

File tree

5 files changed

+217
-0
lines changed

5 files changed

+217
-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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
*
20+
* @See https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SendMessage.html
21+
*/
22+
public function messageDeduplicationId(): string;
23+
}
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: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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()
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()
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()
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()
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+
$this->messageGroupId = $messageGroupId;
80+
$this->messageDeduplicationId = $messageDeduplicationId;
81+
}
82+
83+
public function messageDeduplicationId(): string
84+
{
85+
return $this->messageDeduplicationId;
86+
}
87+
88+
public function messageGroupId(): string
89+
{
90+
return $this->messageGroupId;
91+
}
92+
}
93+
94+
class WithMessageDeduplicationIdMessage implements WithMessageDeduplicationId
95+
{
96+
private string $messageDeduplicationId;
97+
98+
public function __construct(
99+
string $messageDeduplicationId
100+
) {
101+
$this->messageDeduplicationId = $messageDeduplicationId;
102+
}
103+
104+
public function messageDeduplicationId(): string
105+
{
106+
return $this->messageDeduplicationId;
107+
}
108+
}
109+
110+
class WithMessageGroupIdMessage implements WithMessageGroupId
111+
{
112+
private string $messageGroupId;
113+
114+
public function __construct(
115+
string $messageGroupId
116+
) {
117+
$this->messageGroupId = $messageGroupId;
118+
}
119+
120+
public function messageGroupId(): string
121+
{
122+
return $this->messageGroupId;
123+
}
124+
}
125+
class WithoutIdMessage
126+
{
127+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy