Skip to content

Commit 8c38a05

Browse files
ismail1432chalasr
authored andcommitted
[Notifier] add SentMessageEvent and FailedMessageEvent
1 parent 09df57b commit 8c38a05

File tree

8 files changed

+238
-7
lines changed

8 files changed

+238
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
CHANGELOG
22
=========
33

4+
5.4
5+
---
6+
* Add `SentMessageEvent` and `FailedMessageEvent`
7+
48
5.3
59
---
610

Event/FailedMessageEvent.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Event;
13+
14+
use Symfony\Component\Notifier\Message\MessageInterface;
15+
use Symfony\Contracts\EventDispatcher\Event;
16+
17+
/**
18+
* @author Smaïne Milianni <[email protected]>
19+
*/
20+
final class FailedMessageEvent extends Event
21+
{
22+
private $message;
23+
private $error;
24+
25+
public function __construct(MessageInterface $message, \Throwable $error)
26+
{
27+
$this->message = $message;
28+
$this->error = $error;
29+
}
30+
31+
public function getMessage(): MessageInterface
32+
{
33+
return $this->message;
34+
}
35+
36+
public function getError(): \Throwable
37+
{
38+
return $this->error;
39+
}
40+
}

Event/SentMessageEvent.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Event;
13+
14+
use Symfony\Component\Notifier\Message\SentMessage;
15+
use Symfony\Contracts\EventDispatcher\Event;
16+
17+
/**
18+
* @author Smaïne Milianni <[email protected]>
19+
*/
20+
final class SentMessageEvent extends Event
21+
{
22+
private $message;
23+
24+
public function __construct(SentMessage $message)
25+
{
26+
$this->message = $message;
27+
}
28+
29+
public function getMessage(): SentMessage
30+
{
31+
return $this->message;
32+
}
33+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace Symfony\Component\Notifier\Tests\Event;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\Notifier\Event\FailedMessageEvent;
7+
use Symfony\Component\Notifier\Event\MessageEvent;
8+
use Symfony\Component\Notifier\Message\ChatMessage;
9+
use Symfony\Component\Notifier\Message\MessageInterface;
10+
use Symfony\Component\Notifier\Message\SentMessage;
11+
use Symfony\Component\Notifier\Message\SmsMessage;
12+
use Symfony\Component\Notifier\Tests\Transport\DummyMessage;
13+
use Symfony\Component\Notifier\Transport\AbstractTransport;
14+
use Symfony\Component\Notifier\Transport\NullTransport;
15+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
16+
use Symfony\Contracts\HttpClient\HttpClientInterface;
17+
18+
/**
19+
* @author Smaïne Milianni <[email protected]>
20+
*/
21+
final class FailedMessageEventTest extends TestCase
22+
{
23+
/**
24+
* @dataProvider messagesProvider
25+
*/
26+
public function testConstruct(MessageInterface $message, \Throwable $error, FailedMessageEvent $event)
27+
{
28+
$this->assertEquals($event, new FailedMessageEvent($message, $error));
29+
}
30+
31+
/**
32+
* @dataProvider messagesProvider
33+
*/
34+
public function testGetMessage(MessageInterface $message, \Throwable $error, FailedMessageEvent $event)
35+
{
36+
$this->assertSame($message, $event->getMessage());
37+
}
38+
39+
/**
40+
* @dataProvider messagesProvider
41+
*/
42+
public function testGetError(MessageInterface $message, \Throwable $error, FailedMessageEvent $event)
43+
{
44+
$this->assertSame($error, $event->getError());
45+
}
46+
47+
public function testFailedMessageEventIsDisptachIfError()
48+
{
49+
$eventDispatcherMock = $this->createMock(EventDispatcherInterface::class);
50+
$clientMock = $this->createMock(HttpClientInterface::class);
51+
52+
$transport = new class($clientMock, $eventDispatcherMock) extends AbstractTransport {
53+
public function __construct($client, EventDispatcherInterface $dispatcher = null)
54+
{
55+
$this->exception = new NullTransportException();
56+
parent::__construct($client, $dispatcher);
57+
}
58+
59+
public function doSend(MessageInterface $message): SentMessage
60+
{
61+
throw $this->exception;
62+
}
63+
64+
public function supports(MessageInterface $message): bool
65+
{
66+
return true;
67+
}
68+
69+
public function __toString(): string
70+
{
71+
}
72+
};
73+
74+
$message = new DummyMessage();
75+
76+
$eventDispatcherMock->expects($this->exactly(2))
77+
->method('dispatch')
78+
->withConsecutive(
79+
[new MessageEvent($message)],
80+
[new FailedMessageEvent($message, $transport->exception)]
81+
);
82+
try {
83+
$transport->send($message);
84+
} catch (NullTransportException $exception) {
85+
// catch Exception that is voluntary thrown in NullTransport::send
86+
}
87+
}
88+
89+
public function messagesProvider(): iterable
90+
{
91+
yield [$message = new ChatMessage('subject'), $error = new \RuntimeException(), new FailedMessageEvent($message, $error)];
92+
yield [$message = new SmsMessage('+3312345678', 'subject'), $error = new \Exception(), new FailedMessageEvent($message, $error)];
93+
}
94+
}
95+
96+
class NullTransportException extends \Exception
97+
{
98+
}

Tests/Event/SentMessageEventTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Symfony\Component\Notifier\Tests\Event;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\Notifier\Event\SentMessageEvent;
7+
use Symfony\Component\Notifier\Message\ChatMessage;
8+
use Symfony\Component\Notifier\Message\SentMessage;
9+
use Symfony\Component\Notifier\Message\SmsMessage;
10+
11+
/**
12+
* @author Smaïne Milianni <[email protected]>
13+
*/
14+
final class SentMessageEventTest extends TestCase
15+
{
16+
/**
17+
* @dataProvider messagesProvider
18+
*/
19+
public function testConstruct(SentMessage $message, SentMessageEvent $event)
20+
{
21+
$this->assertEquals($event, new SentMessageEvent($message));
22+
}
23+
24+
/**
25+
* @dataProvider messagesProvider
26+
*/
27+
public function testGetMessage(SentMessage $message, SentMessageEvent $event)
28+
{
29+
$this->assertSame($message, $event->getMessage());
30+
}
31+
32+
public function messagesProvider(): iterable
33+
{
34+
yield [$message = new SentMessage(new ChatMessage('subject'), 'null_transport'), new SentMessageEvent($message)];
35+
yield [$message = new SentMessage(new SmsMessage('+3312345678', 'subject'), 'null_transport'), new SentMessageEvent($message)];
36+
}
37+
}

Tests/Transport/NullTransportTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testSend()
3131
$eventDispatcherMock = $this->createMock(EventDispatcherInterface::class)
3232
);
3333

34-
$eventDispatcherMock->expects($this->once())->method('dispatch');
34+
$eventDispatcherMock->expects($this->exactly(2))->method('dispatch');
3535
$nullTransport->send(new DummyMessage());
3636
}
3737
}

Transport/AbstractTransport.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use Symfony\Component\EventDispatcher\Event;
1515
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
1616
use Symfony\Component\HttpClient\HttpClient;
17+
use Symfony\Component\Notifier\Event\FailedMessageEvent;
1718
use Symfony\Component\Notifier\Event\MessageEvent;
19+
use Symfony\Component\Notifier\Event\SentMessageEvent;
1820
use Symfony\Component\Notifier\Exception\LogicException;
1921
use Symfony\Component\Notifier\Message\MessageInterface;
2022
use Symfony\Component\Notifier\Message\SentMessage;
@@ -70,11 +72,23 @@ public function setPort(?int $port): self
7072

7173
public function send(MessageInterface $message): SentMessage
7274
{
73-
if (null !== $this->dispatcher) {
74-
$this->dispatcher->dispatch(new MessageEvent($message));
75+
if (null === $this->dispatcher) {
76+
return $this->doSend($message);
7577
}
7678

77-
return $this->doSend($message);
79+
$this->dispatcher->dispatch(new MessageEvent($message));
80+
81+
try {
82+
$sentMessage = $this->doSend($message);
83+
} catch (\Throwable $error) {
84+
$this->dispatcher->dispatch(new FailedMessageEvent($message, $error));
85+
86+
throw $error;
87+
}
88+
89+
$this->dispatcher->dispatch(new SentMessageEvent($sentMessage));
90+
91+
return $sentMessage;
7892
}
7993

8094
abstract protected function doSend(MessageInterface $message): SentMessage;

Transport/NullTransport.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\EventDispatcher\Event;
1515
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
1616
use Symfony\Component\Notifier\Event\MessageEvent;
17+
use Symfony\Component\Notifier\Event\SentMessageEvent;
1718
use Symfony\Component\Notifier\Message\MessageInterface;
1819
use Symfony\Component\Notifier\Message\NullMessage;
1920
use Symfony\Component\Notifier\Message\SentMessage;
@@ -34,12 +35,16 @@ public function __construct(EventDispatcherInterface $dispatcher = null)
3435
public function send(MessageInterface $message): SentMessage
3536
{
3637
$message = new NullMessage($message);
38+
$sentMessage = new SentMessage($message, (string) $this);
3739

38-
if (null !== $this->dispatcher) {
39-
$this->dispatcher->dispatch(new MessageEvent($message));
40+
if (null === $this->dispatcher) {
41+
return $sentMessage;
4042
}
4143

42-
return new SentMessage($message, (string) $this);
44+
$this->dispatcher->dispatch(new MessageEvent($message));
45+
$this->dispatcher->dispatch(new SentMessageEvent($sentMessage));
46+
47+
return $sentMessage;
4348
}
4449

4550
public function __toString(): string

0 commit comments

Comments
 (0)