Skip to content

[Notifier] Add GatewayApi bridge #39568

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

Merged
merged 1 commit into from
Jan 14, 2021
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 @@ -105,6 +105,7 @@
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;
Expand Down Expand Up @@ -2236,6 +2237,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand Down Expand Up @@ -120,6 +121,10 @@
->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')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
7 changes: 7 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/GatewayApi/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

5.3
---

* Add the bridge
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/GatewayApi/LICENSE
Original file line number Diff line number Diff line change
@@ -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 changes: 25 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/GatewayApi/README.md
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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'];
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
34 changes: 34 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/GatewayApi/composer.json
Original file line number Diff line number Diff line change
@@ -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"
}
Loading