-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from robinlehrmann/transport-name-resolver
Added TransportNameResolver for sns and sqs for automatic transport r…
- Loading branch information
Showing
12 changed files
with
507 additions
and
35 deletions.
There are no files selected for viewing
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
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
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
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,37 @@ | ||
<?php | ||
|
||
namespace Bref\Symfony\Messenger\Service; | ||
|
||
use InvalidArgumentException; | ||
|
||
/** @final */ | ||
class MessengerTransportConfiguration | ||
{ | ||
public function __construct( | ||
private array $messengerTransportsConfiguration | ||
) { | ||
} | ||
|
||
/** @throws InvalidArgumentException */ | ||
public function provideTransportFromEventSource(string $eventSourceWithProtocol): string | ||
{ | ||
foreach ($this->messengerTransportsConfiguration as $messengerTransport => $messengerOptions) { | ||
$dsn = $this->extractDsnFromTransport($messengerOptions); | ||
|
||
if ($dsn === $eventSourceWithProtocol) { | ||
return $messengerTransport; | ||
} | ||
} | ||
|
||
throw new InvalidArgumentException(sprintf('No transport found for eventSource "%s".', $eventSourceWithProtocol)); | ||
} | ||
|
||
private function extractDsnFromTransport(string|array $messengerTransport): string | ||
{ | ||
if (is_array($messengerTransport) && array_key_exists('dsn', $messengerTransport)) { | ||
return $messengerTransport['dsn']; | ||
} | ||
|
||
return $messengerTransport; | ||
} | ||
} |
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
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,30 @@ | ||
<?php | ||
|
||
namespace Bref\Symfony\Messenger\Service\Sns; | ||
|
||
use Bref\Event\Sns\SnsRecord; | ||
use Bref\Symfony\Messenger\Service\MessengerTransportConfiguration; | ||
use InvalidArgumentException; | ||
|
||
/** @final */ | ||
class SnsTransportNameResolver | ||
{ | ||
private const TRANSPORT_PROTOCOL = 'sns://'; | ||
|
||
public function __construct( | ||
private MessengerTransportConfiguration $configurationProvider | ||
) { | ||
} | ||
|
||
/** @throws InvalidArgumentException */ | ||
public function __invoke(SnsRecord $snsRecord): string | ||
{ | ||
if (!array_key_exists('EventSubscriptionArn', $snsRecord->toArray())) { | ||
throw new InvalidArgumentException('EventSubscriptionArn is missing in sns record.'); | ||
} | ||
|
||
$eventSourceArn = $snsRecord->getEventSubscriptionArn(); | ||
|
||
return $this->configurationProvider->provideTransportFromEventSource(self::TRANSPORT_PROTOCOL . $eventSourceArn); | ||
} | ||
} |
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
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,29 @@ | ||
<?php | ||
|
||
namespace Bref\Symfony\Messenger\Service\Sqs; | ||
|
||
use Bref\Event\Sqs\SqsRecord; | ||
use Bref\Symfony\Messenger\Service\MessengerTransportConfiguration; | ||
use InvalidArgumentException; | ||
|
||
/** @final */ | ||
class SqsTransportNameResolver | ||
{ | ||
private const TRANSPORT_PROTOCOL = 'sqs://'; | ||
|
||
public function __construct( | ||
private MessengerTransportConfiguration $configurationProvider | ||
) { | ||
} | ||
|
||
public function __invoke(SqsRecord $sqsRecord): string | ||
{ | ||
if (!array_key_exists('eventSourceARN', $sqsRecord->toArray())) { | ||
throw new InvalidArgumentException('EventSourceArn is missing in sqs record.'); | ||
} | ||
|
||
$eventSourceArn = $sqsRecord->toArray()['eventSourceARN']; | ||
|
||
return $this->configurationProvider->provideTransportFromEventSource(self::TRANSPORT_PROTOCOL . $eventSourceArn); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
tests/Unit/Service/MessengerTransportConfigurationTest.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,48 @@ | ||
<?php | ||
|
||
namespace Bref\Symfony\Messenger\Test\Unit\Service; | ||
|
||
use Bref\Symfony\Messenger\Service\MessengerTransportConfiguration; | ||
use InvalidArgumentException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class MessengerTransportConfigurationTest extends TestCase | ||
{ | ||
public function test_existing_transport_will_be_found_with_existing_event_source_arn(): void | ||
{ | ||
$messengerTransportConfiguration = new MessengerTransportConfiguration([ | ||
'async_example_one' => 'sqs://arn:aws:sqs:us-east-1:123456789012:example_one', | ||
'async_example_two' => [ | ||
'dsn' => 'sqs://arn:aws:sqs:us-east-1:123456789012:example_two', | ||
], | ||
]); | ||
|
||
self::assertSame( | ||
'async_example_one', | ||
$messengerTransportConfiguration->provideTransportFromEventSource( | ||
'sqs://arn:aws:sqs:us-east-1:123456789012:example_one' | ||
) | ||
); | ||
|
||
self::assertSame( | ||
'async_example_two', | ||
$messengerTransportConfiguration->provideTransportFromEventSource( | ||
'sqs://arn:aws:sqs:us-east-1:123456789012:example_two' | ||
) | ||
); | ||
} | ||
|
||
public function test_non_existing_transport_for_event_source_arn_will_be_not_found(): void | ||
{ | ||
$messengerTransportConfiguration = new MessengerTransportConfiguration([]); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage( | ||
'No transport found for eventSource "sqs://arn:aws:sqs:us-east-1:123456789012:missing".' | ||
); | ||
|
||
$messengerTransportConfiguration->provideTransportFromEventSource( | ||
'sqs://arn:aws:sqs:us-east-1:123456789012:missing' | ||
); | ||
} | ||
} |
Oops, something went wrong.