Skip to content

[Notifier] Remove default transport property in Transports class #35834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2020

Conversation

jschaedl
Copy link
Contributor

Q A
Branch? master
Bug fix? no
New feature? yes
Deprecations? no
Tickets -
License MIT
Doc PR tbd.

At the moment the Transports class uses the first element of the injected transports array as the default transport: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Notifier/Transport/Transports.php#L35

If you try to send a message that doesn't define a transport (!$message->getTransport()) the default transport is used. I see two main drawbacks with this solution that I try to fix with this PR:

  1. There is no check if the given message is supported by the default transport. What means that the transport is going to fail with an Exception, if it's not supporting the given message. E.g. the SlackTransport only supports ChatMessages with nullable options or options from type SlackOptions. So as a default transport the SlackTransport can't handle all types of ChatMessages.

  2. Why should we only send the message using the default transport if there are more possible transports which are probably supported?

I did the following to fix the mentioned drawbacks:

  • removed the default transport property
  • added a check to make sure the transport defined by the message supports it
  • send the message to all supported transports, in case the given message does not define a transport
  • added a test

@@ -26,10 +25,6 @@ class ChatChannel extends AbstractChannel
{
public function notify(Notification $notification, Recipient $recipient, string $transportName = null): void
{
if (null === $transportName) {
throw new LogicException('A Chat notification must have a transport defined.');
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a reason for this restriction. All the other channels don't have it either.

@nicolas-grekas nicolas-grekas added this to the next milestone Feb 24, 2020
@jschaedl jschaedl mentioned this pull request Feb 24, 2020
6 tasks
@jschaedl jschaedl requested a review from fabpot February 24, 2020 16:47
@jschaedl jschaedl force-pushed the notifier-transports-improvement branch from b6a2d24 to a5c207b Compare February 28, 2020 14:05
fabpot added a commit that referenced this pull request Mar 16, 2020
This PR was merged into the 5.0 branch.

Discussion
----------

[Notifier] Add unit tests

| Q             | A
| ------------- | ---
| Branch?       | 5.0
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | - <!-- prefix each issue number with "Fix #", if any -->
| License       | MIT
| Doc PR        | - <!-- required for new features -->

- [x] `AbstractChannel`
- [x] `ChannelPolicy`
- [x] `RecipientTest` (done in PR #35773)
- [x] `EmailRecipientTest` (done in PR #35773)
- [x] `SmsRecipientTest` (done in PR #35773)
- [x] `Transports` (see PR #35834)

Commits
-------

022c170 [Notifier] Add tests for AbstractChannel and ChannelPolicy
@jschaedl
Copy link
Contributor Author

jschaedl commented Mar 24, 2020

Hi @fabpot,

what do you think about removing the default transport in the Transports class?

foreach ($this->transports as $transport) {
if ($transport->supports($message)) {
$transport->send($message);
++$supportedTransportsCount;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of sending it through all available transports, I would only send it to the first one. That would be more consistent with my idea of a default transport and probably what I would expect. I would probably not expect to receive 2 SMSes for instance. When you want to dispatch to more than one transport, you need to be explicit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, ok. If I only define a channel name in the channel policy configuration, I actually expected the notification to be broadcasted to all supported transports defined in this channel. For me personally I wouldn't need a default transport. But I also see that this broadcasting approach might be confusing in some situations and a default channel is a good idea. Will change this.

@jschaedl jschaedl force-pushed the notifier-transports-improvement branch from a5c207b to 066990b Compare April 15, 2020 06:43
@jschaedl jschaedl force-pushed the notifier-transports-improvement branch from 51e8352 to 96218d3 Compare June 12, 2020 08:09
@jschaedl
Copy link
Contributor Author

@fabpot friendly ping :-)

I changed the code according to your comment. WDYT?

@fabpot fabpot force-pushed the notifier-transports-improvement branch from 96218d3 to 5c167b0 Compare June 12, 2020 12:23
@fabpot
Copy link
Member

fabpot commented Jun 12, 2020

Thank you @jschaedl.

@fabpot fabpot mentioned this pull request Oct 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants