Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Notifier] added Sinch texter transport #34654

Merged
merged 1 commit into from Feb 17, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -95,6 +95,7 @@
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
use Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory;
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
Expand Down Expand Up @@ -2007,6 +2008,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
TwilioTransportFactory::class => 'notifier.transport_factory.twilio',
FirebaseTransportFactory::class => 'notifier.transport_factory.firebase',
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovhcloud',
SinchTransportFactory::class => 'notifier.transport_factory.sinch',
];

foreach ($classToServices as $class => $service) {
Expand Down
Expand Up @@ -42,6 +42,10 @@
<tag name="texter.transport_factory" />
</service>

<service id="notifier.transport_factory.sinch" class="Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory" parent="notifier.transport_factory.abstract">
<tag name="texter.transport_factory" />
</service>

<service id="notifier.transport_factory.null" class="Symfony\Component\Notifier\Transport\NullTransportFactory" parent="notifier.transport_factory.abstract">
<tag name="notifier.transport_factory" />
</service>
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sinch/.gitattributes
@@ -0,0 +1,3 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitignore export-ignore
7 changes: 7 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sinch/CHANGELOG.md
@@ -0,0 +1,7 @@
CHANGELOG
=========

5.1
-----

* Added the bridge
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sinch/LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2019 Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
12 changes: 12 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sinch/README.md
@@ -0,0 +1,12 @@
Sinch Notifier
==============

Provides Sinch integration for Symfony Notifier.

Resources
---------

* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)
76 changes: 76 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sinch/SinchTransport.php
@@ -0,0 +1,76 @@
<?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\Bridge\Sinch;

use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Iliya Miroslavov Iliev <i.miroslavov@gmail.com>
*
* @experimental in 5.1
*/
final class SinchTransport extends AbstractTransport
{
protected const HOST = 'sms.api.sinch.com';

private $accountSid;
private $authToken;
private $from;

public function __construct(string $accountSid, string $authToken, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
{
$this->accountSid = $accountSid;
$this->authToken = $authToken;
$this->from = $from;

parent::__construct($client, $dispatcher);
}

public function __toString(): string
{
return sprintf('sinch://%s?from=%s', $this->getEndpoint(), $this->from);
}

public function supports(MessageInterface $message): bool
{
return $message instanceof SmsMessage;
}

protected function doSend(MessageInterface $message): void
{
if (!$message instanceof SmsMessage) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, \get_class($message)));
}

$endpoint = sprintf('https://%s/xms/v1/%s/batches', $this->getEndpoint(), $this->accountSid);
$response = $this->client->request('POST', $endpoint, [
'auth_bearer' => $this->authToken,
'json' => [
'from' => $this->from,
'to' => [$message->getPhone()],
'body' => $message->getSubject(),
],
]);

if (201 !== $response->getStatusCode()) {
$error = $response->toArray(false);

throw new TransportException(sprintf('Unable to send the SMS: %s (%s).', $error['text'], $error['code']), $response);
}
}
}
@@ -0,0 +1,46 @@
<?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\Bridge\Sinch;

use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;
use Symfony\Component\Notifier\Transport\TransportInterface;

/**
* @author Iliya Miroslavov Iliev <i.miroslavov@gmail.com>
*
* @experimental in 5.1
*/
final class SinchTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): TransportInterface
{
$scheme = $dsn->getScheme();
$accountSid = $this->getUser($dsn);
$authToken = $this->getPassword($dsn);
$from = $dsn->getOption('from');
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();

if ('sinch' === $scheme) {
return (new SinchTransport($accountSid, $authToken, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}

throw new UnsupportedSchemeException($dsn, 'sinch', $this->getSupportedSchemes());
}

protected function getSupportedSchemes(): array
{
return ['sinch'];
}
}
36 changes: 36 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sinch/composer.json
@@ -0,0 +1,36 @@
{
"name": "symfony/sinch-notifier",
"type": "symfony-bridge",
"description": "Symfony Sinch Notifier Bridge",
"keywords": ["sms", "sinch", "notifier"],
"homepage": "https://symfony.com",
"license": "MIT",
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"require": {
"php": "^7.2.5",
"ext-json": "*",
"symfony/http-client": "^4.3|^5.0",
"symfony/notifier": "^5.0"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Sinch\\": "" },
"exclude-from-classmap": [
"/Tests/"
]
},
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "5.1-dev"
}
}
}
31 changes: 31 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sinch/phpunit.xml.dist
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
failOnWarning="true"
>
<php>
<ini name="error_reporting" value="-1" />
</php>

<testsuites>
<testsuite name="Symfony Sinch Notifier Bridge Test Suite">
<directory>./Tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
Expand Up @@ -54,6 +54,10 @@ class UnsupportedSchemeException extends LogicException
'class' => Bridge\OvhCloud\OvhCloudTransportFactory::class,
'package' => 'symfony/ovhcloud-notifier',
],
'sinch' => [
'class' => Bridge\Sinch\SinchTransportFactory::class,
'package' => 'symfony/sinch-notifier',
],
];

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Notifier/Transport.php
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
use Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory;
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
Expand Down Expand Up @@ -46,6 +47,7 @@ class Transport
TwilioTransportFactory::class,
OvhCloudTransportFactory::class,
FirebaseTransportFactory::class,
SinchTransportFactory::class,
];

private $factories;
Expand Down