Skip to content

[Notifier] add SentMessageEvent and FailedMessageEvent #39601

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
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Symfony/Component/Notifier/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG
=========

5.4
---
* Add `SentMessageEvent` and `FailedMessageEvent`

5.3
---

Expand Down
40 changes: 40 additions & 0 deletions src/Symfony/Component/Notifier/Event/FailedMessageEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Event;

use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Contracts\EventDispatcher\Event;

/**
* @author Smaïne Milianni <smaine.milianni@gmail.com>
*/
final class FailedMessageEvent extends Event
{
private $message;
private $error;

public function __construct(MessageInterface $message, \Throwable $error)
{
$this->message = $message;
$this->error = $error;
}

public function getMessage(): MessageInterface
{
return $this->message;
}

public function getError(): \Throwable
{
return $this->error;
}
}
33 changes: 33 additions & 0 deletions src/Symfony/Component/Notifier/Event/SentMessageEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Event;

use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Contracts\EventDispatcher\Event;

/**
* @author Smaïne Milianni <smaine.milianni@gmail.com>
*/
final class SentMessageEvent extends Event
{
private $message;

public function __construct(SentMessage $message)
{
$this->message = $message;
}

public function getMessage(): SentMessage
{
return $this->message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Symfony\Component\Notifier\Tests\Event;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Event\FailedMessageEvent;
use Symfony\Component\Notifier\Event\MessageEvent;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Tests\Transport\DummyMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Component\Notifier\Transport\NullTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Smaïne Milianni <smaine.milianni@gmail.com>
*/
final class FailedMessageEventTest extends TestCase
{
/**
* @dataProvider messagesProvider
*/
public function testConstruct(MessageInterface $message, \Throwable $error, FailedMessageEvent $event)
{
$this->assertEquals($event, new FailedMessageEvent($message, $error));
}

/**
* @dataProvider messagesProvider
*/
public function testGetMessage(MessageInterface $message, \Throwable $error, FailedMessageEvent $event)
{
$this->assertSame($message, $event->getMessage());
}

/**
* @dataProvider messagesProvider
*/
public function testGetError(MessageInterface $message, \Throwable $error, FailedMessageEvent $event)
{
$this->assertSame($error, $event->getError());
}

public function testFailedMessageEventIsDisptachIfError()
{
$eventDispatcherMock = $this->createMock(EventDispatcherInterface::class);
$clientMock = $this->createMock(HttpClientInterface::class);

$transport = new class($clientMock, $eventDispatcherMock) extends AbstractTransport {
public function __construct($client, EventDispatcherInterface $dispatcher = null)
{
$this->exception = new NullTransportException();
parent::__construct($client, $dispatcher);
}

public function doSend(MessageInterface $message): SentMessage
{
throw $this->exception;
}

public function supports(MessageInterface $message): bool
{
return true;
}

public function __toString(): string
{
}
};

$message = new DummyMessage();

$eventDispatcherMock->expects($this->exactly(2))
->method('dispatch')
->withConsecutive(
[new MessageEvent($message)],
[new FailedMessageEvent($message, $transport->exception)]
);
try {
$transport->send($message);
} catch (NullTransportException $exception) {
// catch Exception that is voluntary thrown in NullTransport::send
}
}

public function messagesProvider(): iterable
{
yield [$message = new ChatMessage('subject'), $error = new \RuntimeException(), new FailedMessageEvent($message, $error)];
yield [$message = new SmsMessage('+3312345678', 'subject'), $error = new \Exception(), new FailedMessageEvent($message, $error)];
}
}

class NullTransportException extends \Exception
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Symfony\Component\Notifier\Tests\Event;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Event\SentMessageEvent;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Message\SmsMessage;

/**
* @author Smaïne Milianni <smaine.milianni@gmail.com>
*/
final class SentMessageEventTest extends TestCase
{
/**
* @dataProvider messagesProvider
*/
public function testConstruct(SentMessage $message, SentMessageEvent $event)
{
$this->assertEquals($event, new SentMessageEvent($message));
}

/**
* @dataProvider messagesProvider
*/
public function testGetMessage(SentMessage $message, SentMessageEvent $event)
{
$this->assertSame($message, $event->getMessage());
}

public function messagesProvider(): iterable
{
yield [$message = new SentMessage(new ChatMessage('subject'), 'null_transport'), new SentMessageEvent($message)];
yield [$message = new SentMessage(new SmsMessage('+3312345678', 'subject'), 'null_transport'), new SentMessageEvent($message)];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testSend()
$eventDispatcherMock = $this->createMock(EventDispatcherInterface::class)
);

$eventDispatcherMock->expects($this->once())->method('dispatch');
$eventDispatcherMock->expects($this->exactly(2))->method('dispatch');
$nullTransport->send(new DummyMessage());
}
}
20 changes: 17 additions & 3 deletions src/Symfony/Component/Notifier/Transport/AbstractTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\Notifier\Event\FailedMessageEvent;
use Symfony\Component\Notifier\Event\MessageEvent;
use Symfony\Component\Notifier\Event\SentMessageEvent;
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
Expand Down Expand Up @@ -70,11 +72,23 @@ public function setPort(?int $port): self

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

return $this->doSend($message);
$this->dispatcher->dispatch(new MessageEvent($message));

try {
$sentMessage = $this->doSend($message);
} catch (\Throwable $error) {
$this->dispatcher->dispatch(new FailedMessageEvent($message, $error));

throw $error;
}

$this->dispatcher->dispatch(new SentMessageEvent($sentMessage));

return $sentMessage;
}

abstract protected function doSend(MessageInterface $message): SentMessage;
Expand Down
11 changes: 8 additions & 3 deletions src/Symfony/Component/Notifier/Transport/NullTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
use Symfony\Component\Notifier\Event\MessageEvent;
use Symfony\Component\Notifier\Event\SentMessageEvent;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\NullMessage;
use Symfony\Component\Notifier\Message\SentMessage;
Expand All @@ -34,12 +35,16 @@ public function __construct(EventDispatcherInterface $dispatcher = null)
public function send(MessageInterface $message): SentMessage
{
$message = new NullMessage($message);
$sentMessage = new SentMessage($message, (string) $this);

if (null !== $this->dispatcher) {
$this->dispatcher->dispatch(new MessageEvent($message));
if (null === $this->dispatcher) {
return $sentMessage;
}

return new SentMessage($message, (string) $this);
$this->dispatcher->dispatch(new MessageEvent($message));
$this->dispatcher->dispatch(new SentMessageEvent($sentMessage));

return $sentMessage;
}

public function __toString(): string
Expand Down