Skip to content

Commit

Permalink
feature #49270 [Messenger] Allow passing a string instead of an array…
Browse files Browse the repository at this point in the history
… in `TransportNamesStamp` (alexandre-daubois)

This PR was merged into the 6.3 branch.

Discussion
----------

[Messenger] Allow passing a string instead of an array in `TransportNamesStamp`

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | _NA_
| License       | MIT
| Doc PR        | Todo

This PR aims to improve a bit the DX when using `TransportNamesStamp` by allowing passing a string as the only argument of the stamp when only one transport is provided by the developer.

Also, I fixed the property name to match getter's name. I did not changed constructor argument's name as I'm afraid it would be a BC if it is used with named arguments 🤔

Commits
-------

ff86922 [Messenger] Allow passing a string instead of an array in `TransportNamesStamp`
  • Loading branch information
nicolas-grekas committed Feb 10, 2023
2 parents 7d9a82f + ff86922 commit 49b6ab8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
`Symfony\Component\Messenger\Transport\InMemoryTransportFactory` in favor of
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport` and
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransportFactory`
* Allow passing a string instead of an array in `TransportNamesStamp`

6.2
---
Expand Down
7 changes: 5 additions & 2 deletions src/Symfony/Component/Messenger/Stamp/TransportNamesStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
*/
final class TransportNamesStamp implements StampInterface
{
private array $transports;

/**
* @param string[] $transports Transport names to be used for the message
* @param string[]|string $transports Transport names to be used for the message
*/
public function __construct(private array $transports)
public function __construct(array|string $transports)
{
$this->transports = (array) $transports;
}

public function getTransportNames(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,13 @@ public function testGetSenders()
$this->assertSame($sender, $stampSenders[$key]);
}
}

public function testGetIndividualSender()
{
$stamp = new TransportNamesStamp('first_transport');
$stampSenders = $stamp->getTransportNames();

$this->assertCount(1, $stampSenders);
$this->assertSame('first_transport', $stampSenders[0]);
}
}

0 comments on commit 49b6ab8

Please sign in to comment.