Skip to content

Commit e787b6f

Browse files
committed
feature symfony#59481 [Notifier] Add SentMessage additional info (mRoca)
This PR was merged into the 7.3 branch. Discussion ---------- [Notifier] Add SentMessage additional info | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | Fix symfony#49793 | License | MIT This PR allows adding additional data into [Notifier/Message/SentMessage.php](https://github.com/symfony/symfony/blob/7.3/src/Symfony/Component/Notifier/Message/SentMessage.php), based on (and using) [HttpClient Response's info](https://github.com/symfony/symfony/blob/7.3/src/Symfony/Component/HttpClient/Response/AsyncContext.php#L118-L145). The initial need behind this feature is to be able to access to some data of the Transport's response (e.g.: [`nbSms` and `cost` for allmysms](https://doc.allmysms.com/api/fr/#api-SMS-sendsimple), or [`totalCreditsRemoved` for OVH Cloud](https://eu.api.ovh.com/console/?section=%2Fsms&branch=v1#post-/sms/-serviceName-/jobs)). Original (closed) PR: symfony#51746 Some points to discuss: - It would be a lot easier to inject the eventual `ResponseInterface` object into the constructor as a readonly property, but not all Transports are using HTTP - [Having a free-form array where each transport can add anything looks like a bad idea](symfony#51746 (comment)), but [In HttpClient you documented the standard info keys, and decorators can add their own things there](symfony#51746 (comment)) Commits ------- a50f38c feat(notifier): add SentMessage additional info
2 parents 99984e9 + a50f38c commit e787b6f

File tree

10 files changed

+139
-7
lines changed

10 files changed

+139
-7
lines changed

src/Symfony/Component/Notifier/Bridge/AllMySms/AllMySmsTransport.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public function supports(MessageInterface $message): bool
4848
return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof AllMySmsOptions);
4949
}
5050

51+
/**
52+
* @See https://doc.allmysms.com/api/en/#api-SMS-sendsimple
53+
*/
5154
protected function doSend(MessageInterface $message): SentMessage
5255
{
5356
if (!$message instanceof SmsMessage) {
@@ -83,7 +86,13 @@ protected function doSend(MessageInterface $message): SentMessage
8386
throw new TransportException(\sprintf('Unable to send the SMS: "%s" (%s).', $success['description'], $success['code']), $response);
8487
}
8588

86-
$sentMessage = new SentMessage($message, (string) $this);
89+
$additionalInfo = [
90+
'nbSms' => $success['nbSms'] ?? null,
91+
'balance' => $success['balance'] ?? null,
92+
'cost' => $success['cost'] ?? null,
93+
];
94+
95+
$sentMessage = new SentMessage($message, (string) $this, $additionalInfo);
8796
$sentMessage->setMessageId($success['smsId']);
8897

8998
return $sentMessage;

src/Symfony/Component/Notifier/Bridge/AllMySms/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+
7.3
5+
---
6+
7+
* Add `nbSms`, `balance`, and `cost` info into returned `SentMessage`
8+
49
6.3
510
---
611

src/Symfony/Component/Notifier/Bridge/AllMySms/Tests/AllMySmsTransportTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Notifier\Bridge\AllMySms\Tests;
1313

1414
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\HttpClient\Response\MockResponse;
1516
use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsOptions;
1617
use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsTransport;
1718
use Symfony\Component\Notifier\Message\ChatMessage;
@@ -44,4 +45,31 @@ public static function unsupportedMessagesProvider(): iterable
4445
yield [new ChatMessage('Hello!')];
4546
yield [new DummyMessage()];
4647
}
48+
49+
public function testSentMessageInfo()
50+
{
51+
$smsMessage = new SmsMessage('0611223344', 'lorem ipsum');
52+
53+
$data = json_encode([
54+
'code' => 100,
55+
'description' => 'Your messages have been sent',
56+
'smsId' => 'de4d766d-4faf-11e9-a8ef-0025907cf72e',
57+
'invalidNumbers' => '',
58+
'nbContacts' => 1,
59+
'nbSms' => 1,
60+
'balance' => 2.81,
61+
'cost' => 0.19,
62+
]);
63+
64+
$responses = [
65+
new MockResponse($data, ['http_code' => 201]),
66+
];
67+
68+
$transport = self::createTransport(new MockHttpClient($responses));
69+
$sentMessage = $transport->send($smsMessage);
70+
71+
$this->assertSame(1, $sentMessage->getInfo('nbSms'));
72+
$this->assertSame(2.81, $sentMessage->getInfo('balance'));
73+
$this->assertSame(0.19, $sentMessage->getInfo('cost'));
74+
}
4775
}

src/Symfony/Component/Notifier/Bridge/AllMySms/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": ">=8.2",
2020
"symfony/http-client": "^6.4|^7.0",
21-
"symfony/notifier": "^7.2"
21+
"symfony/notifier": "^7.3"
2222
},
2323
"autoload": {
2424
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\AllMySms\\": "" },

src/Symfony/Component/Notifier/Bridge/OvhCloud/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+
7.3
5+
---
6+
7+
* Add `totalCreditsRemoved` info into returned `SentMessage`
8+
49
6.2
510
---
611

src/Symfony/Component/Notifier/Bridge/OvhCloud/OvhCloudTransport.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ public function supports(MessageInterface $message): bool
7272
return $message instanceof SmsMessage;
7373
}
7474

75+
/**
76+
* @see https://eu.api.ovh.com/console/?section=%2Fsms&branch=v1#post-/sms/-serviceName-/jobs
77+
*/
7578
protected function doSend(MessageInterface $message): SentMessage
7679
{
7780
if (!$message instanceof SmsMessage) {
@@ -122,7 +125,7 @@ protected function doSend(MessageInterface $message): SentMessage
122125
if (200 !== $statusCode) {
123126
$error = $response->toArray(false);
124127

125-
throw new TransportException(\sprintf('Unable to send the SMS: %s.', $error['message']), $response);
128+
throw new TransportException(\sprintf('Unable to send the SMS: "%s".', $error['message'] ?? 'Unknown error'), $response);
126129
}
127130

128131
$success = $response->toArray(false);
@@ -131,7 +134,11 @@ protected function doSend(MessageInterface $message): SentMessage
131134
throw new TransportException(\sprintf('Attempt to send the SMS to invalid receivers: "%s".', implode(',', $success['invalidReceivers'])), $response);
132135
}
133136

134-
$sentMessage = new SentMessage($message, (string) $this);
137+
$additionalInfo = [
138+
'totalCreditsRemoved' => $success['totalCreditsRemoved'] ?? null,
139+
];
140+
141+
$sentMessage = new SentMessage($message, (string) $this, $additionalInfo);
135142
$sentMessage->setMessageId($success['ids'][0]);
136143

137144
return $sentMessage;

src/Symfony/Component/Notifier/Bridge/OvhCloud/Tests/OvhCloudTransportTest.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ public function testValidSignature(string $message)
6464
$time = time();
6565

6666
$data = json_encode([
67-
'totalCreditsRemoved' => '1',
67+
'totalCreditsRemoved' => 1,
6868
'invalidReceivers' => [],
6969
'ids' => [
70-
'26929925',
70+
26929925,
7171
],
7272
'validReceivers' => [
7373
'0611223344',
@@ -96,7 +96,7 @@ public function testInvalidReceiver()
9696
$smsMessage = new SmsMessage('invalid_receiver', 'lorem ipsum');
9797

9898
$data = json_encode([
99-
'totalCreditsRemoved' => '1',
99+
'totalCreditsRemoved' => 0,
100100
'invalidReceivers' => ['invalid_receiver'],
101101
'ids' => [],
102102
'validReceivers' => [],
@@ -112,4 +112,29 @@ public function testInvalidReceiver()
112112
$this->expectExceptionMessage('Attempt to send the SMS to invalid receivers: "invalid_receiver"');
113113
$transport->send($smsMessage);
114114
}
115+
116+
public function testSentMessageInfo()
117+
{
118+
$smsMessage = new SmsMessage('0611223344', 'lorem ipsum');
119+
120+
$data = json_encode([
121+
'totalCreditsRemoved' => 1,
122+
'invalidReceivers' => [],
123+
'ids' => [
124+
26929925,
125+
],
126+
'validReceivers' => [
127+
'0611223344',
128+
],
129+
]);
130+
$responses = [
131+
new MockResponse(time()),
132+
new MockResponse($data),
133+
];
134+
135+
$transport = self::createTransport(new MockHttpClient($responses));
136+
$sentMessage = $transport->send($smsMessage);
137+
138+
$this->assertSame(1, $sentMessage->getInfo('totalCreditsRemoved'));
139+
}
115140
}

src/Symfony/Component/Notifier/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add `Dsn::getBooleanOption()`
8+
* Add `info` property in `SentMessage`
89

910
7.2
1011
---

src/Symfony/Component/Notifier/Message/SentMessage.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ class SentMessage
1818
{
1919
private ?string $messageId = null;
2020

21+
/**
22+
* @param array $info attaches any Transport-related information to the sent message
23+
*/
2124
public function __construct(
2225
private MessageInterface $original,
2326
private string $transport,
27+
private array $info = [],
2428
) {
2529
}
2630

@@ -43,4 +47,18 @@ public function getMessageId(): ?string
4347
{
4448
return $this->messageId;
4549
}
50+
51+
/**
52+
* Returns extra info attached to the message.
53+
*
54+
* @param string|null $key if null, the whole info array will be returned, else returns the info value or null
55+
*/
56+
public function getInfo(?string $key = null): mixed
57+
{
58+
if (null !== $key) {
59+
return $this->info[$key] ?? null;
60+
}
61+
62+
return $this->info;
63+
}
4664
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\Tests\Message;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Message\SentMessage;
16+
use Symfony\Component\Notifier\Tests\Transport\DummyMessage;
17+
18+
class SentMessageTest extends TestCase
19+
{
20+
public function test()
21+
{
22+
$originalMessage = new DummyMessage();
23+
24+
$sentMessage = new SentMessage($originalMessage, 'any', ['foo' => 'bar']);
25+
$sentMessage->setMessageId('the_id');
26+
27+
$this->assertSame($originalMessage, $sentMessage->getOriginalMessage());
28+
$this->assertSame('any', $sentMessage->getTransport());
29+
$this->assertSame('the_id', $sentMessage->getMessageId());
30+
$this->assertSame(['foo' => 'bar'], $sentMessage->getInfo());
31+
$this->assertSame('bar', $sentMessage->getInfo('foo'));
32+
$this->assertNull($sentMessage->getInfo('not_existing'));
33+
}
34+
}

0 commit comments

Comments
 (0)