Skip to content

Commit 6c0b119

Browse files
committed
feature #36611 Add Notifier SentMessage (jeremyFreeAgent)
This PR was squashed before being merged into the 5.2-dev branch. Discussion ---------- Add Notifier SentMessage | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | symfony/symfony-docs#13624 Like Mailer, Notifier returns now a SentMessage that contains the messageId (returned by the provider in the response). It contains also the body of the response as array to have more info about price, number of sms sent, status and so on. - [x] apply to bridges Commits ------- 5a6f0537ec Add Notifier SentMessage
2 parents 3799d90 + 629c342 commit 6c0b119

File tree

10 files changed

+92
-27
lines changed

10 files changed

+92
-27
lines changed

CHANGELOG.md

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

4+
5.2.0
5+
-----
6+
7+
* [BC BREAK] The `TransportInterface::send()` and `AbstractTransport::doSend()` methods changed to return a `SentMessage` instance instead of `void`.
8+
49
5.1.0
510
-----
611

Chatter.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Messenger\MessageBusInterface;
1717
use Symfony\Component\Notifier\Event\MessageEvent;
1818
use Symfony\Component\Notifier\Message\MessageInterface;
19+
use Symfony\Component\Notifier\Message\SentMessage;
1920
use Symfony\Component\Notifier\Transport\TransportInterface;
2021
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2122

@@ -47,12 +48,10 @@ public function supports(MessageInterface $message): bool
4748
return $this->transport->supports($message);
4849
}
4950

50-
public function send(MessageInterface $message): void
51+
public function send(MessageInterface $message): SentMessage
5152
{
5253
if (null === $this->bus) {
53-
$this->transport->send($message);
54-
55-
return;
54+
return $this->transport->send($message);
5655
}
5756

5857
if (null !== $this->dispatcher) {

Message/SentMessage.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Message;
13+
14+
/**
15+
* @author Jérémy Romey <[email protected]>
16+
*
17+
* @experimental in 5.2
18+
*/
19+
final class SentMessage
20+
{
21+
private $original;
22+
private $transport;
23+
private $messageId;
24+
25+
public function __construct(MessageInterface $original, string $transport)
26+
{
27+
$this->original = $original;
28+
$this->transport = $transport;
29+
}
30+
31+
public function getOriginalMessage(): MessageInterface
32+
{
33+
return $this->original;
34+
}
35+
36+
public function getTransport(): string
37+
{
38+
return $this->transport;
39+
}
40+
41+
public function setMessageId(string $id): void
42+
{
43+
$this->messageId = $id;
44+
}
45+
46+
public function getMessageId(): ?string
47+
{
48+
return $this->messageId;
49+
}
50+
}

Tests/Transport/TransportsTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
1616
use Symfony\Component\Notifier\Exception\LogicException;
1717
use Symfony\Component\Notifier\Message\ChatMessage;
18+
use Symfony\Component\Notifier\Message\SentMessage;
1819
use Symfony\Component\Notifier\Transport\TransportInterface;
1920
use Symfony\Component\Notifier\Transport\Transports;
2021

@@ -30,9 +31,12 @@ public function testSendToTransportDefinedByMessage(): void
3031

3132
$one->method('supports')->with($message)->willReturn(true);
3233

33-
$one->expects($this->once())->method('send');
34+
$one->expects($this->once())->method('send')->willReturn(new SentMessage($message, 'one'));
3435

35-
$transports->send($message);
36+
$sentMessage = $transports->send($message);
37+
38+
$this->assertSame($message, $sentMessage->getOriginalMessage());
39+
$this->assertSame('one', $sentMessage->getTransport());
3640
}
3741

3842
public function testSendToFirstSupportedTransportIfMessageDoesNotDefineATransport(): void
@@ -47,10 +51,16 @@ public function testSendToFirstSupportedTransportIfMessageDoesNotDefineATranspor
4751
$one->method('supports')->with($message)->willReturn(false);
4852
$two->method('supports')->with($message)->willReturn(true);
4953

54+
$one->method('send')->with($message)->willReturn(new SentMessage($message, 'one'));
55+
$two->method('send')->with($message)->willReturn(new SentMessage($message, 'two'));
56+
5057
$one->expects($this->never())->method('send');
51-
$two->expects($this->once())->method('send');
58+
$two->expects($this->once())->method('send')->willReturn(new SentMessage($message, 'two'));
5259

53-
$transports->send($message);
60+
$sentMessage = $transports->send($message);
61+
62+
$this->assertSame($message, $sentMessage->getOriginalMessage());
63+
$this->assertSame('two', $sentMessage->getTransport());
5464
}
5565

5666
public function testThrowExceptionIfNoSupportedTransportWasFound(): void

Texter.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Messenger\MessageBusInterface;
1717
use Symfony\Component\Notifier\Event\MessageEvent;
1818
use Symfony\Component\Notifier\Message\MessageInterface;
19+
use Symfony\Component\Notifier\Message\SentMessage;
1920
use Symfony\Component\Notifier\Transport\TransportInterface;
2021
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2122

@@ -47,12 +48,10 @@ public function supports(MessageInterface $message): bool
4748
return $this->transport->supports($message);
4849
}
4950

50-
public function send(MessageInterface $message): void
51+
public function send(MessageInterface $message): SentMessage
5152
{
5253
if (null === $this->bus) {
53-
$this->transport->send($message);
54-
55-
return;
54+
return $this->transport->send($message);
5655
}
5756

5857
if (null !== $this->dispatcher) {

Transport/AbstractTransport.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Notifier\Event\MessageEvent;
1818
use Symfony\Component\Notifier\Exception\LogicException;
1919
use Symfony\Component\Notifier\Message\MessageInterface;
20+
use Symfony\Component\Notifier\Message\SentMessage;
2021
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2122
use Symfony\Contracts\HttpClient\HttpClientInterface;
2223

@@ -69,16 +70,16 @@ public function setPort(?int $port): self
6970
return $this;
7071
}
7172

72-
public function send(MessageInterface $message): void
73+
public function send(MessageInterface $message): SentMessage
7374
{
7475
if (null !== $this->dispatcher) {
7576
$this->dispatcher->dispatch(new MessageEvent($message));
7677
}
7778

78-
$this->doSend($message);
79+
return $this->doSend($message);
7980
}
8081

81-
abstract protected function doSend(MessageInterface $message): void;
82+
abstract protected function doSend(MessageInterface $message): SentMessage;
8283

8384
protected function getEndpoint(): ?string
8485
{

Transport/NullTransport.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
1616
use Symfony\Component\Notifier\Event\MessageEvent;
1717
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Message\SentMessage;
1819
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
1920

2021
/**
@@ -31,11 +32,13 @@ public function __construct(EventDispatcherInterface $dispatcher = null)
3132
$this->dispatcher = class_exists(Event::class) ? LegacyEventDispatcherProxy::decorate($dispatcher) : $dispatcher;
3233
}
3334

34-
public function send(MessageInterface $message): void
35+
public function send(MessageInterface $message): SentMessage
3536
{
3637
if (null !== $this->dispatcher) {
3738
$this->dispatcher->dispatch(new MessageEvent($message));
3839
}
40+
41+
return new SentMessage($message, (string) $this);
3942
}
4043

4144
public function __toString(): string

Transport/RoundRobinTransport.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Notifier\Exception\RuntimeException;
1616
use Symfony\Component\Notifier\Exception\TransportExceptionInterface;
1717
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Message\SentMessage;
1819

1920
/**
2021
* Uses several Transports using a round robin algorithm.
@@ -63,13 +64,11 @@ public function supports(MessageInterface $message): bool
6364
return false;
6465
}
6566

66-
public function send(MessageInterface $message): void
67+
public function send(MessageInterface $message): SentMessage
6768
{
6869
while ($transport = $this->getNextTransport($message)) {
6970
try {
70-
$transport->send($message);
71-
72-
return;
71+
return $transport->send($message);
7372
} catch (TransportExceptionInterface $e) {
7473
$this->deadTransports[$transport] = microtime(true);
7574
}

Transport/TransportInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Notifier\Exception\TransportExceptionInterface;
1515
use Symfony\Component\Notifier\Message\MessageInterface;
16+
use Symfony\Component\Notifier\Message\SentMessage;
1617

1718
/**
1819
* @author Fabien Potencier <[email protected]>
@@ -24,7 +25,7 @@ interface TransportInterface
2425
/**
2526
* @throws TransportExceptionInterface
2627
*/
27-
public function send(MessageInterface $message): void;
28+
public function send(MessageInterface $message): SentMessage;
2829

2930
public function supports(MessageInterface $message): bool;
3031

Transport/Transports.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
1515
use Symfony\Component\Notifier\Exception\LogicException;
1616
use Symfony\Component\Notifier\Message\MessageInterface;
17+
use Symfony\Component\Notifier\Message\SentMessage;
1718

1819
/**
1920
* @author Fabien Potencier <[email protected]>
@@ -51,17 +52,14 @@ public function supports(MessageInterface $message): bool
5152
return false;
5253
}
5354

54-
public function send(MessageInterface $message): void
55+
public function send(MessageInterface $message): SentMessage
5556
{
5657
if (!$transport = $message->getTransport()) {
5758
foreach ($this->transports as $transport) {
5859
if ($transport->supports($message)) {
59-
$transport->send($message);
60-
61-
return;
60+
return $transport->send($message);
6261
}
6362
}
64-
6563
throw new LogicException(sprintf('None of the available transports support the given message (available transports: "%s").', implode('", "', array_keys($this->transports))));
6664
}
6765

@@ -73,6 +71,6 @@ public function send(MessageInterface $message): void
7371
throw new LogicException(sprintf('The "%s" transport does not support the given message.', $transport));
7472
}
7573

76-
$this->transports[$transport]->send($message);
74+
return $this->transports[$transport]->send($message);
7775
}
7876
}

0 commit comments

Comments
 (0)