Skip to content

Use DI & SRP to make Amazon SQS Messenger more extensible #38640

@jacekwilczynski

Description

@jacekwilczynski

Description

This issue is here in case #38639 is rejected and I have to implement ListableReceiverInterface in my application code with decoration or inheritance. However, it might be useful in other scenarios as well.

The amazon-sqs-messenger component instantiates dependencies in dependants, which is a problem for extending the component.

  • AmazonSqsReceiver and AmazonSqsSender are instantiated in AmazonSqsTransport. -> Please allow passing them via the constructor. Can make the parameters optional and fall back to the default receiver&sender implementations not to force other users to change their calls.
  • The Connection::fromDsn static method contains configuration processing logic that could be used also for other purposes than instantiating a Connection. For example, someone may want to use an identical instance of SqsClient to the one hidden within the Connection, or even the same instance - so one would create the SqsClient on their own and pass it to the Connection's main constructor. -> Please extract a method (or a class) to handle just configuration processing without instantiating any services. See example below.

An extreme solution would be to make all private fields and methods in all classes protected and mark Connection as not @internal but I guess what is private and internal is private and internal for a reason so I'm not really asking for that (though it wouldn't hurt me :-)).

I can write the implementation but would like to get an approval of the ticket first.

Example

In a custom implementation of AmazonSqsTransportFactoryInterface::createTransport:

Before

$transport = new AmazonSqsTransport(Connection::fromDsn($dsn, $options), $serializer);

After (not necessarily exactly that):

[$connectionConfiguration, $clientConfiguration] = ConfigurationFactory::processConfiguration($dsn, $options);
$client = new SqsClient($clientConfiguration);
$connection = new Connection($connectionConfiguration, $client);

$transport = new AmazonSqsTransport(
    $connection,
    $serializer,
    new MyExtendedSqsReceiver($connection, $serializer, $client), // now I can use the client to send some `receiveMessage` requests
    new AmazonSqsSender($connection, $serializer)
);

The Connection::fromDsn method could still exist but be refactored to use ConfigurationFactory::processConfiguration. This way, the setup shown in the "before" code snippet would still work.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions