forked from symfony/symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
src/Symfony/Component/Messenger/Bridge/AmazonSqs/CHANGELOG.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/Symfony/Component/Messenger/Bridge/AmazonSqs/Middleware/AddFifoStamp.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?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\Transport\AmazonSqsFifoStamp; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Middleware\MiddlewareInterface; | ||
use Symfony\Component\Messenger\Middleware\StackInterface; | ||
|
||
final class AddFifoStamp implements MiddlewareInterface | ||
{ | ||
public function handle(Envelope $envelope, StackInterface $stack): Envelope | ||
{ | ||
$message = $envelope->getMessage(); | ||
$messageGroupId = null; | ||
$messageDeduplicationId = null; | ||
|
||
if ($message instanceof WithMessageGroupId) { | ||
$messageGroupId = $message->messageGroupId(); | ||
} | ||
if ($message instanceof WithMessageDeduplicationId) { | ||
$messageDeduplicationId = $message->messageDeduplicationId(); | ||
} | ||
|
||
if (null !== $messageGroupId || null !== $messageDeduplicationId) { | ||
$envelope = $envelope->with( | ||
new AmazonSqsFifoStamp( | ||
$messageGroupId, | ||
$messageDeduplicationId, | ||
) | ||
); | ||
} | ||
|
||
return $stack->next()->handle($envelope, $stack); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Symfony/Component/Messenger/Bridge/AmazonSqs/Middleware/WithMessageDeduplicationId.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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\Middleware; | ||
|
||
/** | ||
* @see https://docs.aws.amazon.com/en_gb/AWSSimpleQueueService/latest/SQSDeveloperGuide/using-messagededuplicationid-property.html | ||
*/ | ||
interface WithMessageDeduplicationId | ||
{ | ||
/** | ||
* The max length of MessageDeduplicationId is 128 characters. | ||
* Valid values: alphanumeric characters and punctuation (!"#$%&'()*+,-./:;<=>?@[]^_`{|}~). | ||
*/ | ||
public function messageDeduplicationId(): string; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Symfony/Component/Messenger/Bridge/AmazonSqs/Middleware/WithMessageGroupId.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?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; | ||
|
||
interface WithMessageGroupId | ||
{ | ||
/** | ||
* The max length of MessageGroupId is 128 characters. | ||
* Valid values: alphanumeric characters and punctuation (!"#$%&'()*+,-./:;<=>?@[]^_`{|}~). | ||
*/ | ||
public function messageGroupId(): string; | ||
} |
116 changes: 116 additions & 0 deletions
116
src/Symfony/Component/Messenger/Bridge/AmazonSqs/Tests/Middleware/AddFifoStampTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?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\Middleware\AddFifoStamp; | ||
use Symfony\Component\Messenger\Bridge\AmazonSqs\Middleware\WithMessageDeduplicationId; | ||
use Symfony\Component\Messenger\Bridge\AmazonSqs\Middleware\WithMessageGroupId; | ||
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 AddFifoStamp(); | ||
$envelope = new Envelope(new WithMessageGroupIdMessage('groupId')); | ||
$finalEnvelope = $middleware->handle($envelope, $this->getStackMock()); | ||
$stamp = $finalEnvelope->last(AmazonSqsFifoStamp::class); | ||
$this->assertNotNull($stamp); | ||
/* @var AmazonSqsFifoStamp $stamp */ | ||
$this->assertEquals('groupId', $stamp->getMessageGroupId()); | ||
$this->assertNull($stamp->getMessageDeduplicationId()); | ||
} | ||
|
||
public function testHandleWithDeduplicationIdOnly() | ||
{ | ||
$middleware = new AddFifoStamp(); | ||
$envelope = new Envelope(new WithMessageDeduplicationIdMessage('deduplicationId')); | ||
$finalEnvelope = $middleware->handle($envelope, $this->getStackMock()); | ||
$stamp = $finalEnvelope->last(AmazonSqsFifoStamp::class); | ||
$this->assertNotNull($stamp); | ||
/* @var AmazonSqsFifoStamp $stamp */ | ||
$this->assertEquals('deduplicationId', $stamp->getMessageDeduplicationId()); | ||
$this->assertNull($stamp->getMessageGroupId()); | ||
} | ||
|
||
public function testHandleWithGroupIdAndDeduplicationId() | ||
{ | ||
$middleware = new AddFifoStamp(); | ||
$envelope = new Envelope(new WithMessageDeduplicationIdAndMessageGroupIdMessage('my_group', 'my_random_id')); | ||
$finalEnvelope = $middleware->handle($envelope, $this->getStackMock()); | ||
$stamp = $finalEnvelope->last(AmazonSqsFifoStamp::class); | ||
$this->assertNotNull($stamp); | ||
/* @var AmazonSqsFifoStamp $stamp */ | ||
$this->assertEquals('my_random_id', $stamp->getMessageDeduplicationId()); | ||
$this->assertEquals('my_group', $stamp->getMessageGroupId()); | ||
} | ||
|
||
public function testHandleWithoutId() | ||
{ | ||
$middleware = new AddFifoStamp(); | ||
$envelope = new Envelope(new WithoutIdMessage()); | ||
$finalEnvelope = $middleware->handle($envelope, $this->getStackMock()); | ||
$stamp = $finalEnvelope->last(AmazonSqsFifoStamp::class); | ||
/* @var AmazonSqsFifoStamp $stamp */ | ||
$this->assertNull($stamp); | ||
} | ||
} | ||
|
||
class WithMessageDeduplicationIdAndMessageGroupIdMessage implements WithMessageDeduplicationId, WithMessageGroupId | ||
{ | ||
public function __construct( | ||
private string $messageGroupId, | ||
private string $messageDeduplicationId | ||
) { | ||
} | ||
|
||
public function messageDeduplicationId(): string | ||
{ | ||
return $this->messageDeduplicationId; | ||
} | ||
|
||
public function messageGroupId(): string | ||
{ | ||
return $this->messageGroupId; | ||
} | ||
} | ||
|
||
class WithMessageDeduplicationIdMessage implements WithMessageDeduplicationId | ||
{ | ||
public function __construct( | ||
private string $messageDeduplicationId | ||
) { | ||
} | ||
|
||
public function messageDeduplicationId(): string | ||
{ | ||
return $this->messageDeduplicationId; | ||
} | ||
} | ||
|
||
class WithMessageGroupIdMessage implements WithMessageGroupId | ||
{ | ||
public function __construct( | ||
private string $messageGroupId | ||
) { | ||
} | ||
|
||
public function messageGroupId(): string | ||
{ | ||
return $this->messageGroupId; | ||
} | ||
} | ||
class WithoutIdMessage | ||
{ | ||
} |