[Notifier] Add GatewayApi bridge
This commit is contained in:
parent
c01b032d7a
commit
6b9f721780
@ -105,6 +105,7 @@ use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
|
||||
@ -2236,6 +2237,7 @@ class FrameworkExtension extends Extension
|
||||
SendinblueNotifierTransportFactory::class => 'notifier.transport_factory.sendinblue',
|
||||
DiscordTransportFactory::class => 'notifier.transport_factory.discord',
|
||||
LinkedInTransportFactory::class => 'notifier.transport_factory.linkedin',
|
||||
GatewayApiTransportFactory::class => 'notifier.transport_factory.gatewayapi',
|
||||
];
|
||||
|
||||
foreach ($classToServices as $class => $service) {
|
||||
|
@ -15,6 +15,7 @@ use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
|
||||
@ -120,6 +121,10 @@ return static function (ContainerConfigurator $container) {
|
||||
->parent('notifier.transport_factory.abstract')
|
||||
->tag('chatter.transport_factory')
|
||||
|
||||
->set('notifier.transport_factory.gatewayapi', GatewayApiTransportFactory::class)
|
||||
->parent('notifier.transport_factory.abstract')
|
||||
->tag('texter.transport_factory')
|
||||
|
||||
->set('notifier.transport_factory.null', NullTransportFactory::class)
|
||||
->parent('notifier.transport_factory.abstract')
|
||||
->tag('chatter.transport_factory')
|
||||
|
4
src/Symfony/Component/Notifier/Bridge/GatewayApi/.gitattributes
vendored
Normal file
4
src/Symfony/Component/Notifier/Bridge/GatewayApi/.gitattributes
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
/Tests export-ignore
|
||||
/phpunit.xml.dist export-ignore
|
||||
/.gitattributes export-ignore
|
||||
/.gitignore export-ignore
|
@ -0,0 +1,7 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
5.3
|
||||
---
|
||||
|
||||
* Add the bridge
|
@ -0,0 +1,80 @@
|
||||
<?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\GatewayApi;
|
||||
|
||||
use Symfony\Component\Notifier\Exception\TransportException;
|
||||
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
|
||||
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;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
/**
|
||||
* @author Piergiuseppe Longo <piergiuseppe.longo@gmail.com>
|
||||
*/
|
||||
final class GatewayApiTransport extends AbstractTransport
|
||||
{
|
||||
protected const HOST = 'gatewayapi.com';
|
||||
|
||||
private $authToken;
|
||||
private $from;
|
||||
|
||||
public function __construct(string $authToken, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
|
||||
{
|
||||
$this->authToken = $authToken;
|
||||
$this->from = $from;
|
||||
|
||||
parent::__construct($client, $dispatcher);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return sprintf('gatewayapi://%s?from=%s', $this->getEndpoint(), $this->from);
|
||||
}
|
||||
|
||||
public function supports(MessageInterface $message): bool
|
||||
{
|
||||
return $message instanceof SmsMessage;
|
||||
}
|
||||
|
||||
protected function doSend(MessageInterface $message): SentMessage
|
||||
{
|
||||
if (!$message instanceof SmsMessage) {
|
||||
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
|
||||
}
|
||||
|
||||
$endpoint = sprintf('https://%s/rest/mtsms', $this->getEndpoint());
|
||||
|
||||
$response = $this->client->request('POST', $endpoint, [
|
||||
'auth_basic' => [$this->authToken, ''],
|
||||
'json' => [
|
||||
'sender' => $this->from,
|
||||
'recipients' => [['msisdn' => $message->getPhone()]],
|
||||
'message' => $message->getSubject(),
|
||||
],
|
||||
]);
|
||||
|
||||
$statusCode = $response->getStatusCode();
|
||||
if (200 !== $statusCode) {
|
||||
throw new TransportException(sprintf('Unable to send the SMS: error %d.', $statusCode), $response);
|
||||
}
|
||||
|
||||
$content = $response->toArray(false);
|
||||
|
||||
$sentMessage = new SentMessage($message, (string) $this);
|
||||
$sentMessage->setMessageId((string) $content['ids'][0]);
|
||||
|
||||
return $sentMessage;
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
<?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\GatewayApi;
|
||||
|
||||
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 Piergiuseppe Longo <piergiuseppe.longo@gmail.com>
|
||||
*/
|
||||
final class GatewayApiTransportFactory extends AbstractTransportFactory
|
||||
{
|
||||
/**
|
||||
* @return GatewayApiTransport
|
||||
*/
|
||||
public function create(Dsn $dsn): TransportInterface
|
||||
{
|
||||
$scheme = $dsn->getScheme();
|
||||
|
||||
if ('gatewayapi' !== $scheme) {
|
||||
throw new UnsupportedSchemeException($dsn, 'gatewayapi', $this->getSupportedSchemes());
|
||||
}
|
||||
|
||||
$authToken = $this->getUser($dsn);
|
||||
$from = $dsn->getRequiredOption('from');
|
||||
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
|
||||
$port = $dsn->getPort();
|
||||
|
||||
return (new GatewayApiTransport($authToken, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
|
||||
}
|
||||
|
||||
protected function getSupportedSchemes(): array
|
||||
{
|
||||
return ['gatewayapi'];
|
||||
}
|
||||
}
|
19
src/Symfony/Component/Notifier/Bridge/GatewayApi/LICENSE
Normal file
19
src/Symfony/Component/Notifier/Bridge/GatewayApi/LICENSE
Normal file
@ -0,0 +1,19 @@
|
||||
Copyright (c) 2021 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.
|
25
src/Symfony/Component/Notifier/Bridge/GatewayApi/README.md
Normal file
25
src/Symfony/Component/Notifier/Bridge/GatewayApi/README.md
Normal file
@ -0,0 +1,25 @@
|
||||
GatewayApi Notifier
|
||||
===============
|
||||
|
||||
Provides GatewayApi integration for Symfony Notifier.
|
||||
|
||||
DSN example
|
||||
-----------
|
||||
|
||||
```
|
||||
GATEWAYAPI_DSN=gatewayapi://TOKEN@default?from=FROM
|
||||
```
|
||||
|
||||
where:
|
||||
- `TOKEN` is API Token (OAuth)
|
||||
- `FROM` is sender name
|
||||
|
||||
See your account info at https://gatewayapi.com
|
||||
|
||||
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)
|
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Component\Notifier\Bridge\GatewayApi\Tests;
|
||||
|
||||
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransportFactory;
|
||||
use Symfony\Component\Notifier\Tests\TransportFactoryTestCase;
|
||||
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
|
||||
|
||||
/**
|
||||
* @author Piergiuseppe Longo <piergiuseppe.longo@gmail.com>
|
||||
* @author Oskar Stark <oskarstark@googlemail.com>
|
||||
*/
|
||||
final class GatewayApiTransportFactoryTest extends TransportFactoryTestCase
|
||||
{
|
||||
/**
|
||||
* @return GatewayApiTransportFactory
|
||||
*/
|
||||
public function createFactory(): TransportFactoryInterface
|
||||
{
|
||||
return new GatewayApiTransportFactory();
|
||||
}
|
||||
|
||||
public function createProvider(): iterable
|
||||
{
|
||||
yield [
|
||||
'gatewayapi://gatewayapi.com?from=Symfony',
|
||||
'gatewayapi://token@default?from=Symfony',
|
||||
];
|
||||
}
|
||||
|
||||
public function supportsProvider(): iterable
|
||||
{
|
||||
yield [true, 'gatewayapi://token@host.test?from=Symfony'];
|
||||
yield [false, 'somethingElse://token@default?from=Symfony'];
|
||||
}
|
||||
|
||||
public function incompleteDsnProvider(): iterable
|
||||
{
|
||||
yield 'missing token' => ['gatewayapi://host.test?from=Symfony'];
|
||||
}
|
||||
|
||||
public function missingRequiredOptionProvider(): iterable
|
||||
{
|
||||
yield 'missing option: from' => ['gatewayapi://token@host.test'];
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Component\Notifier\Bridge\GatewayApi\Tests;
|
||||
|
||||
use Symfony\Component\HttpClient\MockHttpClient;
|
||||
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransport;
|
||||
use Symfony\Component\Notifier\Message\ChatMessage;
|
||||
use Symfony\Component\Notifier\Message\MessageInterface;
|
||||
use Symfony\Component\Notifier\Message\SentMessage;
|
||||
use Symfony\Component\Notifier\Message\SmsMessage;
|
||||
use Symfony\Component\Notifier\Tests\TransportTestCase;
|
||||
use Symfony\Component\Notifier\Transport\TransportInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||
|
||||
/**
|
||||
* @author Piergiuseppe Longo <piergiuseppe.longo@gmail.com>
|
||||
* @author Oskar Stark <oskarstark@googlemail.com>
|
||||
*/
|
||||
final class GatewayApiTransportTest extends TransportTestCase
|
||||
{
|
||||
/**
|
||||
* @return GatewayApiTransport
|
||||
*/
|
||||
public function createTransport(?HttpClientInterface $client = null): TransportInterface
|
||||
{
|
||||
return new GatewayApiTransport('authtoken', 'Symfony', $client ?: $this->createMock(HttpClientInterface::class));
|
||||
}
|
||||
|
||||
public function toStringProvider(): iterable
|
||||
{
|
||||
yield ['gatewayapi://gatewayapi.com?from=Symfony', $this->createTransport()];
|
||||
}
|
||||
|
||||
public function supportedMessagesProvider(): iterable
|
||||
{
|
||||
yield [new SmsMessage('0611223344', 'Hello!')];
|
||||
}
|
||||
|
||||
public function unsupportedMessagesProvider(): iterable
|
||||
{
|
||||
yield [new ChatMessage('Hello!')];
|
||||
yield [$this->createMock(MessageInterface::class)];
|
||||
}
|
||||
|
||||
public function testSend()
|
||||
{
|
||||
$response = $this->createMock(ResponseInterface::class);
|
||||
$response->expects($this->exactly(2))
|
||||
->method('getStatusCode')
|
||||
->willReturn(200);
|
||||
$response->expects($this->once())
|
||||
->method('getContent')
|
||||
->willReturn(json_encode(['ids' => [42]]));
|
||||
|
||||
$client = new MockHttpClient(static function () use ($response): ResponseInterface {
|
||||
return $response;
|
||||
});
|
||||
|
||||
$message = new SmsMessage('3333333333', 'Hello!');
|
||||
|
||||
$transport = $this->createTransport($client);
|
||||
$sentMessage = $transport->send($message);
|
||||
|
||||
$this->assertInstanceOf(SentMessage::class, $sentMessage);
|
||||
$this->assertSame('42', $sentMessage->getMessageId());
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "symfony/gatewayapi-notifier",
|
||||
"type": "symfony-bridge",
|
||||
"description": "Symfony GatewayApi Notifier Bridge",
|
||||
"keywords": ["sms", "gatewayapi", "notifier"],
|
||||
"homepage": "https://gatewayapi.com",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Fabien Potencier",
|
||||
"email": "fabien@symfony.com"
|
||||
},
|
||||
{
|
||||
"name": "Piergiuseppe Longo",
|
||||
"email": "piergiuseppe.longo@gmail.com"
|
||||
},
|
||||
{
|
||||
"name": "Symfony Community",
|
||||
"homepage": "https://symfony.com/contributors"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=7.2.5",
|
||||
"symfony/http-client": "^4.3|^5.0",
|
||||
"symfony/notifier": "^5.3"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\GatewayApi\\": "" },
|
||||
"exclude-from-classmap": [
|
||||
"/Tests/"
|
||||
]
|
||||
},
|
||||
"minimum-stability": "dev"
|
||||
}
|
@ -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 GatewayApi 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>
|
@ -88,6 +88,10 @@ class UnsupportedSchemeException extends LogicException
|
||||
'class' => Bridge\Discord\DiscordTransportFactory::class,
|
||||
'package' => 'symfony/discord-notifier',
|
||||
],
|
||||
'gatewayapi' => [
|
||||
'class' => Bridge\GatewayApi\GatewayApiTransportFactory::class,
|
||||
'package' => 'symfony/gatewayapi-notifier',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
|
@ -15,6 +15,7 @@ use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
|
||||
@ -64,6 +65,7 @@ class Transport
|
||||
EsendexTransportFactory::class,
|
||||
SendinblueTransportFactory::class,
|
||||
DiscordTransportFactory::class,
|
||||
GatewayApiTransportFactory::class,
|
||||
];
|
||||
|
||||
private $factories;
|
||||
|
Reference in New Issue
Block a user