Skip to content

Commit

Permalink
feature #36611 Add Notifier SentMessage (jeremyFreeAgent)
Browse files Browse the repository at this point in the history
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
-------

5a6f053 Add Notifier SentMessage
  • Loading branch information
fabpot committed Jun 23, 2020
2 parents a3900af + 5a6f053 commit ce8f8a5
Show file tree
Hide file tree
Showing 31 changed files with 241 additions and 51 deletions.
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand Down Expand Up @@ -50,7 +51,7 @@ public function supports(MessageInterface $message): bool
return $message instanceof ChatMessage;
}

protected function doSend(MessageInterface $message): void
protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof ChatMessage) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, get_debug_type($message)));
Expand Down Expand Up @@ -84,5 +85,12 @@ protected function doSend(MessageInterface $message): void
if ($jsonContents && isset($jsonContents['results']['error'])) {
throw new TransportException(sprintf('Unable to post the Firebase message: %s.', $jsonContents['error']), $response);
}

$success = $response->toArray(false);

$message = new SentMessage($message, (string) $this);
$message->setMessageId($success['results'][0]['message_id']);

return $message;
}
}
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=7.2.5",
"symfony/http-client": "^4.3|^5.0",
"symfony/notifier": "^5.0"
"symfony/notifier": "^5.2"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Firebase\\": "" },
Expand Down
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -51,7 +52,7 @@ public function supports(MessageInterface $message): bool
return $message instanceof SmsMessage && $this->phone === $message->getPhone();
}

protected function doSend(MessageInterface $message): void
protected function doSend(MessageInterface $message): SentMessage
{
if (!$this->supports($message)) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given) and configured with your phone number.', __CLASS__, SmsMessage::class, \get_class($message)));
Expand All @@ -75,5 +76,7 @@ protected function doSend(MessageInterface $message): void

throw new TransportException(sprintf('Unable to send the SMS: error %d: ', $response->getStatusCode()).($errors[$response->getStatusCode()] ?? ''), $response);
}

return new SentMessage($message, (string) $this);
}
}
Expand Up @@ -19,7 +19,7 @@
"require": {
"php": ">=7.2.5",
"symfony/http-client": "^4.3|^5.1",
"symfony/notifier": "^5.1"
"symfony/notifier": "^5.2"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\FreeMobile\\": "" },
Expand Down
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand Down Expand Up @@ -50,7 +51,7 @@ public function supports(MessageInterface $message): bool
/**
* @see https://api.mattermost.com
*/
protected function doSend(MessageInterface $message): void
protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof ChatMessage) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, get_debug_type($message)));
Expand All @@ -74,5 +75,12 @@ protected function doSend(MessageInterface $message): void

throw new TransportException(sprintf('Unable to post the Mattermost message: %s (%s).', $result['message'], $result['id']), $response);
}

$success = $response->toArray(false);

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

return $message;
}
}
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=7.2.5",
"symfony/http-client": "^4.3|^5.0",
"symfony/notifier": "^5.0"
"symfony/notifier": "^5.2"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Mattermost\\": "" },
Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Component/Notifier/Bridge/Nexmo/NexmoTransport.php
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -51,7 +52,7 @@ public function supports(MessageInterface $message): bool
return $message instanceof SmsMessage;
}

protected function doSend(MessageInterface $message): void
protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof SmsMessage) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, get_debug_type($message)));
Expand All @@ -73,5 +74,12 @@ protected function doSend(MessageInterface $message): void
throw new TransportException('Unable to send the SMS: '.$msg['error-text'].sprintf(' (code %s).', $msg['status']), $response);
}
}

$success = $response->toArray(false);

$message = new SentMessage($message, (string) $this);
$message->setMessageId($success['messages'][0]['message-id']);

return $message;
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Notifier/Bridge/Nexmo/composer.json
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=7.2.5",
"symfony/http-client": "^4.3|^5.0",
"symfony/notifier": "^5.0"
"symfony/notifier": "^5.2"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Nexmo\\": "" },
Expand Down
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -53,7 +54,7 @@ public function supports(MessageInterface $message): bool
return $message instanceof SmsMessage;
}

protected function doSend(MessageInterface $message): void
protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof SmsMessage) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, get_debug_type($message)));
Expand Down Expand Up @@ -90,6 +91,13 @@ protected function doSend(MessageInterface $message): void

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

$success = $response->toArray(false);

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

return $message;
}

/**
Expand Down
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=7.2.5",
"symfony/http-client": "^4.3|^5.0",
"symfony/notifier": "^5.0"
"symfony/notifier": "^5.2"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\OvhCloud\\": "" },
Expand Down
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand Down Expand Up @@ -55,7 +56,7 @@ public function supports(MessageInterface $message): bool
/**
* @see https://rocket.chat/docs/administrator-guides/integrations/
*/
protected function doSend(MessageInterface $message): void
protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof ChatMessage) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, get_debug_type($message)));
Expand Down Expand Up @@ -86,5 +87,12 @@ protected function doSend(MessageInterface $message): void
if (!$result['success']) {
throw new TransportException(sprintf('Unable to post the RocketChat message: %s.', $result['error']), $response);
}

$success = $response->toArray(false);

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

return $message;
}
}
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=7.2.5",
"symfony/http-client": "^4.3|^5.0",
"symfony/notifier": "^5.0"
"symfony/notifier": "^5.2"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\RocketChat\\": "" },
Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Component/Notifier/Bridge/Sinch/SinchTransport.php
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -51,7 +52,7 @@ public function supports(MessageInterface $message): bool
return $message instanceof SmsMessage;
}

protected function doSend(MessageInterface $message): void
protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof SmsMessage) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, get_debug_type($message)));
Expand All @@ -72,5 +73,12 @@ protected function doSend(MessageInterface $message): void

throw new TransportException(sprintf('Unable to send the SMS: %s (%s).', $error['text'], $error['code']), $response);
}

$success = $response->toArray(false);

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

return $message;
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Notifier/Bridge/Sinch/composer.json
Expand Up @@ -19,7 +19,7 @@
"php": ">=7.2.5",
"ext-json": "*",
"symfony/http-client": "^4.3|^5.0",
"symfony/notifier": "^5.0"
"symfony/notifier": "^5.2"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Sinch\\": "" },
Expand Down
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand Down Expand Up @@ -58,7 +59,7 @@ public function supports(MessageInterface $message): bool
return $message instanceof ChatMessage && (null === $message->getOptions() || $message->getOptions() instanceof SlackOptions);
}

protected function doSend(MessageInterface $message): void
protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof ChatMessage) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, get_debug_type($message)));
Expand Down Expand Up @@ -86,5 +87,7 @@ protected function doSend(MessageInterface $message): void
if ('ok' !== $result) {
throw new TransportException('Unable to post the Slack message: '.$result, $response);
}

return new SentMessage($message, (string) $this);
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Notifier/Bridge/Slack/composer.json
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=7.2.5",
"symfony/http-client": "^4.3|^5.0",
"symfony/notifier": "^5.1"
"symfony/notifier": "^5.2"
},
"require-dev": {
"symfony/event-dispatcher": "^4.3|^5.0"
Expand Down
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand Down Expand Up @@ -60,7 +61,7 @@ public function supports(MessageInterface $message): bool
/**
* @see https://core.telegram.org/bots/api
*/
protected function doSend(MessageInterface $message): void
protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof ChatMessage) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, get_debug_type($message)));
Expand All @@ -82,5 +83,12 @@ protected function doSend(MessageInterface $message): void

throw new TransportException('Unable to post the Telegram message: '.$result['description'].sprintf(' (code %s).', $result['error_code']), $response);
}

$success = $response->toArray(false);

$message = new SentMessage($message, (string) $this);
$message->setMessageId($success['result']['message_id']);

return $message;
}
}

0 comments on commit ce8f8a5

Please sign in to comment.