Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public function supports(MessageInterface $message): bool
return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof AllMySmsOptions);
}

/**
* @See https://doc.allmysms.com/api/en/#api-SMS-sendsimple
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @See https://doc.allmysms.com/api/en/#api-SMS-sendsimple
* @see https://doc.allmysms.com/api/en/#api-SMS-sendsimple

*/
protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof SmsMessage) {
Expand Down Expand Up @@ -83,7 +86,13 @@ protected function doSend(MessageInterface $message): SentMessage
throw new TransportException(\sprintf('Unable to send the SMS: "%s" (%s).', $success['description'], $success['code']), $response);
}

$sentMessage = new SentMessage($message, (string) $this);
$additionalInfo = [
'nbSms' => $success['nbSms'] ?? null,
'balance' => $success['balance'] ?? null,
'cost' => $success['cost'] ?? null,
];

$sentMessage = new SentMessage($message, (string) $this, $additionalInfo);
$sentMessage->setMessageId($success['smsId']);

return $sentMessage;
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/AllMySms/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Add `nbSms`, `balance`, and `cost` info into returned `SentMessage`

6.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Notifier\Bridge\AllMySms\Tests;

use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsOptions;
use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsTransport;
use Symfony\Component\Notifier\Message\ChatMessage;
Expand Down Expand Up @@ -44,4 +45,31 @@ public static function unsupportedMessagesProvider(): iterable
yield [new ChatMessage('Hello!')];
yield [new DummyMessage()];
}

public function testSentMessageInfo()
{
$smsMessage = new SmsMessage('0611223344', 'lorem ipsum');

$data = json_encode([
'code' => 100,
'description' => 'Your messages have been sent',
'smsId' => 'de4d766d-4faf-11e9-a8ef-0025907cf72e',
'invalidNumbers' => '',
'nbContacts' => 1,
'nbSms' => 1,
'balance' => 2.81,
'cost' => 0.19,
]);

$responses = [
new MockResponse($data, ['http_code' => 201]),
];

$transport = self::createTransport(new MockHttpClient($responses));
$sentMessage = $transport->send($smsMessage);

$this->assertSame(1, $sentMessage->getInfo('nbSms'));
$this->assertSame(2.81, $sentMessage->getInfo('balance'));
$this->assertSame(0.19, $sentMessage->getInfo('cost'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=8.2",
"symfony/http-client": "^6.4|^7.0",
"symfony/notifier": "^7.2"
"symfony/notifier": "^7.3"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\AllMySms\\": "" },
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/OvhCloud/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Add `totalCreditsRemoved` info into returned `SentMessage`

6.2
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public function supports(MessageInterface $message): bool
return $message instanceof SmsMessage;
}

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

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

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

$sentMessage = new SentMessage($message, (string) $this);
$additionalInfo = [
'totalCreditsRemoved' => $success['totalCreditsRemoved'] ?? null,
];

$sentMessage = new SentMessage($message, (string) $this, $additionalInfo);
$sentMessage->setMessageId($success['ids'][0]);

return $sentMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public function testValidSignature(string $message)
$time = time();

$data = json_encode([
'totalCreditsRemoved' => '1',
'totalCreditsRemoved' => 1,
'invalidReceivers' => [],
'ids' => [
'26929925',
26929925,
],
'validReceivers' => [
'0611223344',
Expand Down Expand Up @@ -96,7 +96,7 @@ public function testInvalidReceiver()
$smsMessage = new SmsMessage('invalid_receiver', 'lorem ipsum');

$data = json_encode([
'totalCreditsRemoved' => '1',
'totalCreditsRemoved' => 0,
'invalidReceivers' => ['invalid_receiver'],
'ids' => [],
'validReceivers' => [],
Expand All @@ -112,4 +112,29 @@ public function testInvalidReceiver()
$this->expectExceptionMessage('Attempt to send the SMS to invalid receivers: "invalid_receiver"');
$transport->send($smsMessage);
}

public function testSentMessageInfo()
{
$smsMessage = new SmsMessage('0611223344', 'lorem ipsum');

$data = json_encode([
'totalCreditsRemoved' => 1,
'invalidReceivers' => [],
'ids' => [
26929925,
],
'validReceivers' => [
'0611223344',
],
]);
$responses = [
new MockResponse(time()),
new MockResponse($data),
];

$transport = self::createTransport(new MockHttpClient($responses));
$sentMessage = $transport->send($smsMessage);

$this->assertSame(1, $sentMessage->getInfo('totalCreditsRemoved'));
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Notifier/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add `Dsn::getBooleanOption()`
* Add `info` property in `SentMessage`

7.2
---
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Notifier/Message/SentMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ class SentMessage
{
private ?string $messageId = null;

/**
* @param array $info attaches any Transport-related information to the sent message
*/
public function __construct(
private MessageInterface $original,
private string $transport,
private array $info = [],
) {
}

Expand All @@ -43,4 +47,18 @@ public function getMessageId(): ?string
{
return $this->messageId;
}

/**
* Returns extra info attached to the message.
*
* @param string|null $key if null, the whole info array will be returned, else returns the info value or null
*/
public function getInfo(?string $key = null): mixed
{
if (null !== $key) {
return $this->info[$key] ?? null;
}

return $this->info;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\Tests\Message;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Tests\Transport\DummyMessage;

class SentMessageTest extends TestCase
{
public function test()
{
$originalMessage = new DummyMessage();

$sentMessage = new SentMessage($originalMessage, 'any', ['foo' => 'bar']);
$sentMessage->setMessageId('the_id');

$this->assertSame($originalMessage, $sentMessage->getOriginalMessage());
$this->assertSame('any', $sentMessage->getTransport());
$this->assertSame('the_id', $sentMessage->getMessageId());
$this->assertSame(['foo' => 'bar'], $sentMessage->getInfo());
$this->assertSame('bar', $sentMessage->getInfo('foo'));
$this->assertNull($sentMessage->getInfo('not_existing'));
}
}
Loading