10000 Added Sinch transport · symfony/symfony@70bdac8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 70bdac8

Browse files
committed
Added Sinch transport
1 parent ad522a9 commit 70bdac8

File tree

12 files changed

+227
-0
lines changed

12 files changed

+227
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
use Symfony\Component\Mime\MimeTypeGuesserInterface;
9090
use Symfony\Component\Mime\MimeTypes;
9191
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
92+
use Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory;
9293
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
9394
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
9495
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
@@ -1954,6 +1955,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
19541955
TelegramTransportFactory::class => 'notifier.transport_factory.telegram',
19551956
NexmoTransportFactory::class => 'notifier.transport_factory.nexmo',
19561957
TwilioTransportFactory::class => 'notifier.transport_factory.twilio',
1958+
SinchTransportFactory::class => 'notifier.transport_factory.sinch',
19571959
];
19581960

19591961
foreach ($classToServices as $class => $service) {

src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
<tag name="texter.transport_factory" />
2727
</service>
2828

29+
<service id="notifier.transport_factory.sinch" class="Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory" parent="notifier.transport_factory.abstract">
30+
<tag name="texter.transport_factory" />
31+
</service>
32+
2933
<service id="notifier.transport_factory.null" class="Symfony\Component\Notifier\Transport\NullTransportFactory" parent="notifier.transport_factory.abstract">
3034
<tag name="notifier.transport_factory" />
3135
</service>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitignore export-ignore
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.0.0
5+
-----
6+
7+
* Added the bridge
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1< F438 /code>+
Copyright (c) 2019 Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Sinch Notifier
2+
==============
3+
4+
Provides Sinch integration for Symfony Notifier.
5+
6+
Resources
7+
---------
8+
9+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
10+
* [Report issues](https://github.com/symfony/symfony/issues) and
11+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
12+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Symfony\Component\Notifier\Bridge\Sinch;
4+
5+
use Symfony\Component\Notifier\Exception\LogicException;
6+
use Symfony\Component\Notifier\Exception\TransportException;
7+
use Symfony\Component\Notifier\Message\MessageInterface;
8+
use Symfony\Component\Notifier\Message\SmsMessage;
9+
use Symfony\Component\Notifier\Transport\AbstractTransport;
10+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
11+
use Symfony\Contracts\HttpClient\HttpClientInterface;
12+
13+
/**
14+
* @author Iliya Miroslavov Iliev <i.miroslavov@gmail.com>
15+
*
16+
* @experimental in 5.0
17+
*/
18+
class SinchTransport extends AbstractTransport
19+
{
20+
protected const HOST = 'sms.api.sinch.com';
21+
22+
private $accountSid;
23+
private $authToken;
24+
private $from;
25+
26+
public function __construct(string $accountSid, string $authToken, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
27+
{
28+
$this->accountSid = $accountSid;
29+
$this->authToken = $authToken;
30+
$this->from = $from;
31+
32+
parent::__construct($client, $dispatcher);
33+
}
34+
35+
public function __toString(): string
36+
{
37+
return sprintf('sinch://%s?from=%s', $this->getEndpoint(), $this->from);
38+
}
39+
40+
public function supports(MessageInterface $message): bool
41+
{
42+
return $message instanceof SmsMessage;
43+
}
44+
45+
protected function doSend(MessageInterface $message): void
46+
{
47+
if (!$message instanceof SmsMessage) {
48+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, \get_class($message)));
49+
}
50+
51+
$endpoint = sprintf('https://%s/xms/v1/%s/batches', $this->getEndpoint(), $this->accountSid);
52+
$response = $this->client->request('POST', $endpoint, [
53+
'auth_bearer' => $this->authToken,
54+
'headers' => [
55+
'Content-Type' => 'application/json',
56+
],
57+
'body' => json_encode([
58+
'from' => $this->from,
59+
'to' => [ $message->getPhone() ],
60+
'body' => $message->getSubject(),
61+
]),
62+
]);
63+
64+
if (201 !== $response->getStatusCode()) {
65+
$error = $response->toArray(false);
66+
67+
throw new TransportException(sprintf('Unable to send the SMS: %s (%s).', $error['text'], $error['code']), $response);
68+
}
69+
}
70+
}
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\Bridge\Sinch;
4+
5+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
6+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
7+
use Symfony\Component\Notifier\Transport\Dsn;
8+
use Symfony\Component\Notifier\Transport\TransportInterface;
9+
10+
/**
11+
* @author Iliya Miroslavov Iliev <i.miroslavov@gmail.com>
12+
*
13+
* @experimental in 5.0
14+
*/
15+
final class SinchTransportFactory extends AbstractTransportFactory
16+
{
17+
public function create(Dsn $dsn): TransportInterface
18+
{
19+
$scheme = $dsn->getScheme();
20+
$accountSid = $this->getUser($dsn);
21+
$authToken = $this->getPassword($dsn);
22+
$from = $dsn->getOption('from');
23+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
24+
$port = $dsn->getPort();
25+
26+
if ('sinch' === $scheme) {
27+
return (new SinchTransport($accountSid, $authToken, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
28+
}
29+
30+
throw new UnsupportedSchemeException($dsn, 'sinch', $this->getSupportedSchemes());
31+
}
32+
33+
protected function getSupportedSchemes(): array
34+
{
35+
return ['sinch'];
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "symfony/sinch-notifier",
3+
"type": "symfony-bridge",
4+
"description": "Symfony Sinch Notifier Bridge",
5+
"keywords": ["sms", "sinch", "notifier"],
6+
"homepage": "https://symfony.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Fabien Potencier",
11+
"email": "fabien@symfony.com"
12+
},
13+
{
14+
"name": "Symfony Community",
15+
"homepage": "https://symfony.com/contributors"
16+
}
17+
],
18+
"require": {
19+
"php": "^7.2.5",
20+
"ext-json": "*",
21+
"symfony/http-client": "^4.3|^5.0",
22+
"symfony/notifier": "^5.0"
23+
},
24+
"autoload": {
25+
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Sinch\\": "" },
26+
"exclude-from-classmap": [
27+
"/Tests/"
28+
]
29+
},
30+
"minimum-stability": "dev",
31+
"extra": {
32+
"branch-alias": {
33+
"dev-master": "5.1-dev"
34+
}
35+
}
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
5+
backupGlobals="false"
6+
colors="true"
7+
bootstrap="vendor/autoload.php"
8+
failOnRisky="true"
9+
failOnWarning="true"
10+
>
11+
<php>
12+
<ini name="error_reporting" value="-1" />
13+
</php>
14+
15+
<testsuites>
16+
<testsuite name="Symfony Sinch Notifier Bridge Test Suite">
17+
<directory>./Tests/</directory>
18+
</testsuite>
19+
</testsuites>
20+
21+
<filter>
22+
<whitelist>
23+
<directory>./</directory>
24+
<exclude>
25+
<directory>./Resources</directory>
26+
<directory>./Tests</directory>
27+
<directory>./vendor</directory>
28+
</exclude>
29+
</whitelist>
30+
</filter>
31+
</phpunit>

src/Symfony/Component/Notifier/Exception/UnsupportedSchemeException.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class UnsupportedSchemeException extends LogicException
3838
'class' => Bridge\Twilio\TwilioTransportFactory::class,
3939
'package' => 'symfony/twilio-notifier',
4040
],
41+
'sinch' => [
42+
'class' => Bridge\Sinch\SinchTransportFactory::class,
43+
'package' => 'symfony/sinch-notifier',
44+
],
4145
];
4246

4347
public function __construct(Dsn $dsn, string $name = null, array $supported = [])

src/Symfony/Component/Notifier/Transport.php

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

1414
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
15+
use Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory;
1516
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
1617
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
1718
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
@@ -38,6 +39,7 @@ class Transport
3839
TelegramTransportFactory::class,
3940
NexmoTransportFactory::class,
4041
TwilioTransportFactory::class,
42+
SinchTransportFactory::class,
4143
];
4244

4345
private $factories;

0 commit comments

Comments
 (0)
0