Skip to content

Commit

Permalink
bug #35841 [Notifier] Dispatch message event in null transport (jscha…
Browse files Browse the repository at this point in the history
…edl)

This PR was squashed before being merged into the 5.0 branch.

Discussion
----------

[Notifier] Dispatch message event in null transport

| Q             | A
| ------------- | ---
| Branch?       | 5.0
| Bug fix?      | yes
| 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 -->

I think we should be able to log notifications via the `NotificationLoggerListener` even if they were sent to a null transport. The mailer component does it the same way.

Commits
-------

a0d99ce [Notifier] Dispatch message event in null transport
  • Loading branch information
fabpot committed Feb 24, 2020
2 parents 08a233b + a0d99ce commit 8b819ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/Symfony/Component/Notifier/Transport/NullTransport.php
Expand Up @@ -11,7 +11,11 @@

namespace Symfony\Component\Notifier\Transport;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
use Symfony\Component\Notifier\Event\MessageEvent;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* @author Fabien Potencier <fabien@symfony.com>
Expand All @@ -20,8 +24,18 @@
*/
class NullTransport implements TransportInterface
{
private $dispatcher;

public function __construct(EventDispatcherInterface $dispatcher = null)
{
$this->dispatcher = class_exists(Event::class) ? LegacyEventDispatcherProxy::decorate($dispatcher) : $dispatcher;
}

public function send(MessageInterface $message): void
{
if (null !== $this->dispatcher) {
$this->dispatcher->dispatch(new MessageEvent($message));
}
}

public function __toString(): string
Expand Down
Expand Up @@ -26,7 +26,7 @@ final class NullTransportFactory extends AbstractTransportFactory
public function create(Dsn $dsn): TransportInterface
{
if ('null' === $dsn->getScheme()) {
return new NullTransport();
return new NullTransport($this->dispatcher);
}

throw new UnsupportedSchemeException($dsn, 'null', $this->getSupportedSchemes());
Expand Down

0 comments on commit 8b819ba

Please sign in to comment.